Node.js 中的 TCP socket.write 函数“网”包未写入套接字

发布于 2025-01-06 04:43:22 字数 1283 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2025-01-13 04:43:22

显然,这取决于您正在与哪个服务器通信,但您可能应该使用换行符 \n 来分隔数据:

socket.write('@!>\n');       
socket.write('RIG,test,test,3.1\n');

对于某些服务器,您可能需要 \r\n

It depends on what server you are speaking to, obviously, but you should probably delimit your data with newlines \n:

socket.write('@!>\n');       
socket.write('RIG,test,test,3.1\n');

For some servers, you might need \r\n.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文