Node.js 中的 TCP socket.write 函数“网”包未写入套接字
我在使用 node.js 的 net 包向 TCP 套接字写入 2 条消息时遇到一些问题。
代码:
var net = require('net');
var HOST = '20.100.2.62';
var PORT = '5555';
var socket = new net.Socket();
socket.connect (PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
socket.write('@!>');
socket.write('RIG,test,test,3.1');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
socket.on('data', function(data) {
console.log('DATA: ' + data);
// Close the client socket completely
// client.destroy();
});
socket.on('error', function(exception){
console.log('Exception:');
console.log(exception);
});
socket.on('drain', function() {
console.log("drain!");
});
socket.on('timeout', function() {
console.log("timeout!");
});
// Add a 'close' event handler for the client socket
socket.on('close', function() {
console.log('Connection closed');
});
我还尝试了 net 包中据称更正确的 net.createConnection(arguments...) 函数,但没有成功。
我可以在服务器端看到与套接字的连接按预期发生,但服务器没有收到数据,这就是为什么我怀疑我使用 socket.write 函数的方式有问题。也许第一个字符串字符引起了混乱?
任何帮助将不胜感激。
非常感谢。
I am having some trouble writing 2 messages to a TCP socket using node.js' net package.
The code:
var net = require('net');
var HOST = '20.100.2.62';
var PORT = '5555';
var socket = new net.Socket();
socket.connect (PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
socket.write('@!>');
socket.write('RIG,test,test,3.1');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
socket.on('data', function(data) {
console.log('DATA: ' + data);
// Close the client socket completely
// client.destroy();
});
socket.on('error', function(exception){
console.log('Exception:');
console.log(exception);
});
socket.on('drain', function() {
console.log("drain!");
});
socket.on('timeout', function() {
console.log("timeout!");
});
// Add a 'close' event handler for the client socket
socket.on('close', function() {
console.log('Connection closed');
});
I've also tried the supposedly more correct net.createConnection(arguments...) function from the net package with no luck.
I can see on my server side that the connection to the socket happens just as expected but there is no data received by the server which is why I'm suspecting that something is wrong with the way I'm using the socket.write function. Perhaps the first strings characters are causing confusion?
Any help would be greatly appreciated.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,这取决于您正在与哪个服务器通信,但您可能应该使用换行符
\n
来分隔数据:对于某些服务器,您可能需要
\r\n
。It depends on what server you are speaking to, obviously, but you should probably delimit your data with newlines
\n
:For some servers, you might need
\r\n
.