Created
May 9, 2019 13:15
-
-
Save zhfnjust/a066c4dda5b4624aeda58b746fd0de34 to your computer and use it in GitHub Desktop.
testclient.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 构建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