nodejs net模块实现socket
var net = require('net'); var client = net.connect({port: 8080}, function() { console.log('连接到服务器!'); }); client.on('data', function(data) { console.log(data.toString()); client.end(); }); client.on('end', function() { console.log('断开与服务器的连接'); });
客户端代码
var net = require('net'); var server = net.createServer(function(connection) { console.log('client connected'); connection.on('end', function() { console.log('客户端关闭连接'); }); connection.write('Hello World!\r\n'); connection.pipe(connection); }); server.listen(8080, function() { console.log('server is listening'); });
服务器端代码
实现socket
posted on 2015-11-29 12:33 liuwenbohhh 阅读(801) 评论(0) 编辑 收藏 举报