nodejs使用tcp协议编程(nodejs服务器端,java客户端)
var net = require ('net'); var HOST = '192.168.1.50'; var PORT = 8809; net.createServer(function(sock){ console.log('CONNECTED:'+sock.remoteAddress + ":"+ sock.remotePort); sock.on('data',function(data){ console.log('DATA'+sock.remoteAddress +":" +data); sock.write('You said "'+data+'"'); }); sock.on('close',function(data){ console.log('CLOSED:'+sock.remoteAddress +''+sock.remotePort); }); }).listen(PORT,HOST); console.log('Server listening on '+HOST+':'+PORT);
public static void main(String[] args) throws Exception, IOException { Socket socket = new Socket("192.168.1.50", 8809); BufferedReader sin = new BufferedReader( new InputStreamReader(System.in)); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream())); String readline; readline = sin.readLine(); while (!readline.equals("bye")) { os.println(readline); os.flush(); System.out.println("Client:" + readline); System.out.println("Server:" + is.readLine()); readline = sin.readLine(); } os.close(); is.close(); socket.close(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。