ai didi

sockets - node.js 的 "net"包中的 TCP socket.write 函数未写入套接字

转载 作者:可可西里 更新时间:2023-11-01 02:30:47 24 4
gpt4 key购买 nike

我在使用 node.js 的 net 包将 2 条消息写入 TCP 套接字时遇到了一些问题。

代码:

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...) 函数,但没有成功。

我可以在我的服务器端看到与套接字的连接按预期进行,但服务器没有收到任何数据,这就是为什么我怀疑我使用套接字的方式有问题。写函数。也许第一个字符串字符引起了混淆?

如有任何帮助,我们将不胜感激。

非常感谢。

最佳答案

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

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

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

关于sockets - node.js 的 "net"包中的 TCP socket.write 函数未写入套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9328737/

24 4 0
文章推荐: TCP 发送不返回导致进程崩溃
文章推荐: javascript - 如何检索 Ember.js 模型的所有属性
文章推荐: java - 在用于 Java 代码的 Perl 脚本中指定堆大小
文章推荐: tcp - HAProxy 负载均衡 TCP 流量
可可西里
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com