49

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?

2
  • Have you tried client.close()? Commented Feb 8, 2012 at 11:03
  • 10
    There's no client.close method Commented Feb 8, 2012 at 11:37

3 Answers 3

46

This is the default TCP connection termination sequence,

enter image description here

By calling client.end() the node js will send the FIN packet to the server, and the server will response with a FIN packet to the client to accept the socket termination.

As of nodejs documentation what socket.end does is,

Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.

When the FIN packet is received the connection to the server from client is closed automatically and the socket.on('close', .. ) is fired and the ACK is sent back.

So the connection is terminated by agreeing both server and client so the server is able to send data that may require before closing the connection.

But when calling socket.destroy, the client connection will be destroyed without receiving the FIN packet forcefully. It is a best practice to avoid calling socket.destroy if possible.

Reference:

Sign up to request clarification or add additional context in comments.

Comments

42

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

6 Comments

What's the difference between end and destroy? After destroy I also see state FIN_WAIT2 in netstat.
@mgamer end() only closes the writing stream of the socket, the remote host can keep his writing stream open and send you data
Althrough docs say "Only necessary in case of errors" it seems the only method to close a connection. For myself I use something like socket.close = function(data) { var Self = this; if (data) this.write(data, function(){ Self.destroy(); }); else this.destroy(); };
destroy is not a proper way to close the connection and will leave the socket in the FIN_WAIT2 state, which takes something like 2 minutes to recover from.
@jorisw See Janith's answer. You should call client.end or client.destroySoon, which internally calls client.end.
|
8

I have nodejs version 11.15.0 installed under GNU/Linux; the only way to disconnect a telnet-like client in this configuration is to call

socket.destroy()

Other methods on socket simply do not exist any more (eg: close() or disconnect()); also emitting close or end events does not work.

1 Comment

That worked for me. A simple end() would require the other end to react which in my case it doesn't...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.