Skip to content

Instantly share code, notes, and snippets.

@zhfnjust
Created May 9, 2019 13:15
Show Gist options
  • Save zhfnjust/a066c4dda5b4624aeda58b746fd0de34 to your computer and use it in GitHub Desktop.
Save zhfnjust/a066c4dda5b4624aeda58b746fd0de34 to your computer and use it in GitHub Desktop.
testclient.js
/**
* 构建TCP客户端
*/
const fs = require('fs');
/* 引入net模块 */
var net = require("net");
/* 创建TCP客户端 */
var client = net.Socket();
/* 设置连接的服务器 */
client.connect(8000, 'x.x.x.x', function () {
console.log("connect the server");
/* 向服务器发送数据 */
var sync = fs.createReadStream('./tmp');
sync.on('error', function(e) {
console.error(e);
});
sync.on('open', function() {
sync.pipe(client);
});
sync.on('finish', function() {
client.end();
});
})
/* 监听服务器传来的data数据 */
client.on("data", function (data) {
console.log("the data of server is " + data.toString());
})
/* 监听end事件 */
client.on("end", function () {
console.log("data end");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment