Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disconnect from tcp socket in NodeJs

I connect to socket server in NodeJs using this command:

client = net.createConnection() 

How can I then properly disconnect from the server?

I tried client.end() and even client.destroy()

but when I check the connection status using netstat it shows that the connection is in state FIN_WAIT2.

How can I close and destroy the connection altogether?

like image 533
mgamer Avatar asked Feb 08 '12 10:02

mgamer


People also ask

How do I close a TCP socket?

close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.

How do I use sockets in Node JS?

// make a connection with the user from server side io. on('connection', (socket)=>{ console. log('New user connected'); }); Similarly, from the client-side, we need to add a script file and then make a connection to a server through which users send data to a server.

Which statement is used to stop transmission from the socket?

Use the *CANCEL,TCP command to stop the connection to a node or on a socket.

How do I disconnect a TCP connection in python?

If you want to close the connection in a timely fashion, call shutdown() before close(). The "bad file description" error means (most likely) that the socket is already closed (at least from Python side).


1 Answers

net.createConnection() returns a Socket object. client.destroy() is what you want to do.

Source: http://nodejs.org/docs/latest/api/net.html#socket.destroy

like image 50
JP Richardson Avatar answered Oct 14 '22 01:10

JP Richardson