在C客户端上,我执行以下操作:
socket()
connect() on port 6969
send()
//I have seen that I didn't call recv so nodejs try me to send data but My program was gone
and finally closesocket()
在nodejs服务器上,我收到了消息,因此连接已经建立:
const port = 6969;
var net = require('net');
var server = net.createServer(function(connection) {
console.log('client connected');
connection.on('close', function() {
console.log('conn closed');
});
connection.on('end', function() {
console.log('conn ended');// that is not called
});
connection.on("error", function(err) {
console.log("Caught flash policy server socket error: ");
console.log(err.stack);
});
connection.on('data', function(data) {
data = data.toString();
console.log('client sended the folowing string:' + data);
connection.write("Response");
console.log('Sended response to client');
});
});
server.listen(port, function() {
console.log('server is listening');
});
这是我的终端的结果:
server is listening
client connected
client sended the folowing string:err404
Sended response to client
Caught flash policy server socket error:
Error: read ECONNRESET
at exports._errnoException (util.js:1018:11)
at TCP.onread (net.js:568:26)
conn closed
我已经阅读了Node js ECONNRESET,但我不明白为什么我的Node.js服务器会因此崩溃是否属于正常情况?
编辑:我找到了这段代码:
connection.on("error", function(err) {
console.log("Caught flash policy server socket error: ");
console.log(err.stack);
});
客户端的这段代码会产生同样的错误:
#ifdef WIN32
Sleep(5000);
int iResult = shutdown(sock, SD_BOTH);
printf("shutdown is called\n");
Sleep(5000);
#elif defined (linux)
sleep(5);
int iResult = shutdown(sock, SHUT_RDWR);
printf("shutdown is called\n");
sleep(5);
#endif // WIN32
if (iResult == SOCKET_ERROR) {closesocket(sock);printf("SOCKET_ERROR");}
printf("iResult=%d",iResult);
编辑: 现在我捕获了关闭事件和结束事件:但是仍然抛出相同的错误。
编辑:我已更新代码。
我发现问题所在:NodeJs正试图向我发送数据,但我已经调用了shutdown()
。
#if defined (linux)#define closesocket(s) close(s)#endif
,所以在 Linux 上我必须调用shutdown(socket,SHUT_RDWR);
。 - Et7f3XIVif (iResult != SOCKET_ERROR) closesocket(sock);
而不是if (iResult == SOCKET_ERROR) closesocket(sock);
,对吗? - Andre Kamplingprintf(“%d”,iResult);
,它输出0……所以shutdown成功了,但对于NodeJs来说出了些问题。我没有改变==为!=因为这个错误是在之前产生的。 - Et7f3XIV