Closing socket in client nodejs server crash

On client C, I do:

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()

      

On the nodejs server, I get a message to establish a connection:

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');
});

      

This is the output from my terminal:

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

      

So, I already read Node js ECONNRESET , but I don't understand if it is ok why my nodejs node is crashing?

Edit: I found this snippet:

connection.on("error", function(err) {
    console.log("Caught flash policy server socket error: ");
    console.log(err.stack);
});

      

This client side code will produce the same error:

#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);

      

Edit: Now I will catch the close and complete event: but this same error is still being thrown.

Edit: I've updated my code. And I found where the problem is: NodeJs is trying to send me data, but I already called shutdown()

.

+3


source to share


1 answer


There are 2 things to consider, one in your server and one in your client code.

Server code:

You should use event end

instead of event close

, see https://nodejs.org/api/net.html#net_event_end :

connection.on('end', function (){
   console.log('client disconnected');
});

      

end event:

Fired when the other end of a socket sends a FIN packet, thereby ending the read side of the socket.

close event:

Excluded when the socket is completely closed. The has_error argument is a boolean that says that the socket was closed due to a transmission error.

Thus, it means that the event close

will happen after the event end

.


Client code:

You must call shutdown

before closing the socket to prevent further reads or writes, which then throw an error because the socket is already disconnected.



The shutdown

Windows version is described here on MSDN and the Linux version here in the manpage .

Window:

int ret = shutdown(sockfd, SD_BOTH); /* Shutdown both send and receive operations. */

      

Linux:

int ret = shutdown(sockfd, SHUT_RDWR); /* Disables further send and receive operations. */

      


Importance of the data sent:

The function shutdown

does not guarantee that data that is already in the buffer does not receive a send. All data must be flushed prior to calling close

. In this answer on SO wrote a good way to do it and not just sleep 20ms or so.
To check if this is your problem, you can use both Sleep(2000)

Windows and sleep(2)

Linux to sleep 2 seconds between shutdown

and close

.


On this page is a good comparison between close/closesocket

and shutdown

:

You are ready to close the connection with the socket descriptor. It's easy. You can just use the regular Unix file descriptor function:

close(sockfd); 

      

This will prevent further reading and writing to the socket. Anyone trying to read or write a socket on the remote end will receive an error.

Just in case you need a little more control over how the socket is closed, you can use the shutdown () function. This allows you to disconnect a connection in a specific direction or both directions (as close () does.) Synopsis:

int shutdown(int sockfd, int how);

      

[...]

shutdown () returns 0 on success and -1 on error (with errno set.)

+3


source







All Articles