您可以使用node.js中的内置套接字来执行类似的 *** 作(在java和node.js中都非常容易,但是您会明白这一点):
Java的:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class Test { public static void main(String[] args) { ServerSocket server; Socket client; InputStream input; try { server = new ServerSocket(1010); client = server.accept(); input = client.getInputStream(); String inputString = Test.inputStreamAsString(input); System.out.println(inputString); client.close(); server.close(); } catch (Exception e) { e.printStackTrace(); } } public static String inputStreamAsString(InputStream stream) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(stream)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "n"); } br.close(); return sb.toString(); }}
Node.js:
var net = require('net');var client = net.connect(1010, 'localhost');client.write('Hello from node.js');client.end();
以及有关套接字的node.js文档的链接:http :
//nodejs.org/docs/latest/api/net.html
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)