①通过调用net.connect()创建套接字客户端
②在回调函数里建立连接行为,你可以添加超时时间或编码
③需要添加处理程序来处理data,end、error、timeout、close事件
④把数据写到服务器,如果写入失败就可能需要实现drain事件处理程序
这个客户端只是发送一点数据到服务器并接收回一点数据,注意,有3个单独的套接字打开到服务器,而它们同一时间进行通信。
/**
* @description TODO
* @author suiyue
* @date 2019/9/4 21:35
*/
var net = require('net');
function getConnection(connName) {
var client = net.connect({port:8107,host:'localhost'},function () {
console.log(connName + 'Connected:');
console.log(' local= %s:%s',this.localAddress,this.localPort);
console.log(' remote= %s:%s',this._remoteAddress,this._remoteAddress);
this.setTimeout(500);
this.setEncoding('utf8');
this.on('data', function (data) {
console.log(connName+" From Server:"+data.toString());
this.end();
});
this.on('end', function () {
console.log(connName + 'Client disconnection');
});
this.on('error',function (err) {
console.log('Socket Error:' + JSON.stringify(err));
});
this.on('timeout',function () {
console.log('Socket Timed Out');
});
this.on('close', function () {
console.log('Socket Closed');
});
});
return client;
}
function writeData(socket, data) {
var success = !socket.write(data);
if(!success) {
(function (socket, data) {
socket.once('drain', function () {
writeData(socket, data);
});
})(socket, data);
}
}
var Dwarves = getConnection("Dwarves");
var Elves = getConnection("Elves");
var Hobbits = getConnection("Hobbits");
writeData(Dwarves, "More Axes");
writeData(Elves, "More Arrows");
writeData(Hobbits, "More Pipe Weed");
①通过调用net.createServer()创建socket服务器,还需要提供一个连接回调处理程序,然后调用listen()在端口8107监听
②在listen回调处理程序内添加处理程序支持server对象上的close和error事件
③在connection事件回调函数里面建立连接的行为
④添加处理程序处理客户端连接上的data、end、error、timeout和close事件
⑤把数据写入服务器
服务器在端口8107上接受连接,读取数据,把数据放回给客户端
/**
* @description TODO
* @author suiyue
* @date 2019/9/4 21:52
*/
var net = require('net');
var server = net.createServer(function (client) {
console.log(' Client connection:');
console.log(' local= %s:%s',client.localAddress,client.localPort);
console.log(' remote= %s:%s',client._remoteAddress,client._remoteAddress);
client.setTimeout(500);
client.setEncoding('utf8');
client.on('data', function (data) {
console.log('Received data from client on port %d: %s', client.remotePort, data.toString());
console.log('Bytes received:', client.bytesRead);
writeData(client,'Sending:'+data.toString());
console.log(' Bytes sent:' + client.bytesWritten);
});
client.on('end', function () {
console.log('Client disconnected');
server.getConnections(function (err, count) {
console.log('Remaining Connection:' + count);
});
});
client.on('error', function (err) {
console.log('Socket Error:', JSON.stringify(err));
});
client.on('timeout', function () {
console.log('Socket Timed Out');
});
});
server.listen(8107, function () {
console.log('Server listening: ' + JSON.stringify(server.address()));
server.on('close', function () {
console.log('Server Terminated');
});
server.on('error', function (err) {
console.log('Server Error:', JSON.stringify(err));
});
});
function writeData(socket,data) {
var success = !socket.write(data);
if (!success) {
(function(socket, data){
socket.once('drain', function () {
writeData(socket, data);
});
})(socket,data);
}
}