Skip to content

Instantly share code, notes, and snippets.

@wjx0912
Created April 17, 2018 07:20
Show Gist options
  • Save wjx0912/b7cc0d03222618c3b188f756ac724b00 to your computer and use it in GitHub Desktop.
Save wjx0912/b7cc0d03222618c3b188f756ac724b00 to your computer and use it in GitHub Desktop.
node.js client test
var net = require('net');
var port = 9922;
var host = '192.168.7.160';
var client= new net.Socket();
//创建socket客户端
client.setEncoding('binary');
//连接到服务端
client.connect(port,host,function(){
client.write('hello server');
//向端口写入数据到达服务端
});
client.on('data',function(data){
console.log('from server:'+ data);
//得到服务端返回来的数据
});
client.on('error',function(error){
//错误出现之后关闭连接
console.log('error:'+error);
client.destory();
});
client.on('close',function(){
//正常关闭连接
console.log('Connection closed');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment