server 端代码
const net = require('net');
let path = require('path');
let server = net.createServer(function (connect) {
connect.setEncoding('binary');
connect.on('error',function(exception){
console.log('socket error:' + exception);
connect.end();
});
//客户端关闭事件
connect.on('close',function(data){
console.log('client closed!');
});
connect.on("data",function (data) {
//server接受到client发送的数据
console.log(data);
//server给client发送数据
connect.write("hello");
})
}).listen(
path.join('\\\\?\\pipe','\\getAppListDesktop'));
server.on("error",function(exception){
console.log("server error:" + exception);
});
client 代码
let net = require('net');
let path = require('path');
let client= new net.Socket();
client.setEncoding('binary');
client.connect(path.join('\\\\?\\pipe','\\getAppListDesktop'),function () {
//client给server发送数据
client.write("hello");
});
client.on('data',function(data){
console.log("getApplistDesktopById "+data);
//此处接受到数据后就可以进行合适的处理了
client.end();
});
client.on('close',function(){
console.log('Connection closed');
});
client.on('error',function(error){
console.log('error:'+error);
client.destory();
});