Sockets node.js'&引用;净额;包未写入套接字
我在使用node.js的网络包向TCP套接字写入2条消息时遇到一些问题 守则:Sockets node.js'&引用;净额;包未写入套接字,sockets,node.js,tcp,Sockets,Node.js,Tcp,我在使用node.js的网络包向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 m
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函数的方式有问题。也许第一个字符串会引起混淆
任何帮助都将不胜感激
非常感谢。这显然取决于您正在与哪个服务器通话,但您可能应该使用换行符来分隔数据
\n
:
socket.write('@!>\n');
socket.write('RIG,test,test,3.1\n');
对于某些服务器,您可能需要
\r\n
我尝试了您的代码,使用nc-l 5555
作为我的服务器,它在节点0.6.10下似乎工作正常。非常感谢您的尝试!问题是我可以用nc或我自己编写的Node.js服务器让它工作(使用Node 0.5.11-pre)。。。这就是我无法连接的特定服务器(用java编写)。我可以很好地使用telnet连接和发送消息/数据,但在使用Socket.write()时不行。正如我所说,当我对服务器日志文件进行跟踪时,我可以清楚地看到我已连接,但无论我尝试向tcp套接字写入什么(使用上述代码),它都不会显示。谢谢你的回复,我很感激。在这种情况下,我猜你没有正确格式化你的数据。例如,您可能希望尝试发送换行符(telnet可以这样做)。尝试socket.write('@!>\n')代码>。谢谢你!虽然我可能应该知道断线的事,但你让我很开心。Cheers Scool-我提供了与真实答案相同的信息,以供将来参考,并为您提供甜点-请接受我的答案。