码迷,mamicode.com
首页 > 编程语言 > 详细

nodejs使用tcp协议编程(nodejs服务器端,java客户端)

时间:2014-07-16 17:26:22      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:nodejs   tcp   server   socket   

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();
	}


nodejs使用tcp协议编程(nodejs服务器端,java客户端),布布扣,bubuko.com

nodejs使用tcp协议编程(nodejs服务器端,java客户端)

标签:nodejs   tcp   server   socket   

原文地址:http://blog.csdn.net/maskdfe/article/details/37813771

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!