0

I have created a TCP socket on Linux using C. Client connect()'s to server and server accept()'s it. When the client request has been served - or a timeout has occured - I want that socket to be totally closed.

But, although both sides call the close() function, I see in the terminal that the socket lives on for a few more minutes in the TIME_WAIT state.

The question is, how can I totally kill it?

Keep in mind that I do not need any handshake with FIN and ACK flags, that I have seen while Googling.

EDIT: I have seen this Avoiding TIME_WAIT thread, but it's 9 years old, that's why I do not fully trust it.

Also, the communication I want to achieve is inter-process communication in the local host, and it must be possible to open and close up to 100 connections per second. That's why, I do not need any handshaking. And I do not want the sockets to remain at TIME_WAIT state for neither a minute, because the OS fills with sockets and the performance is significantly decreased.

EDIT 2: Finally I used SO_REUSEPORT flag and there is no lag trying to open new connection, if the OS is alread filled up...

UDP sockets do not satisfy my program's other specs...

maria
  • 467
  • 1
  • 5
  • 19
  • 1
    Please expand on "Keep in mind that I do not need any handshake with FIN and ACK flags" because one of the reasons you can see a long TIME_WAIT is the closing handshaking was not completed and the system will not return the socket for reuse in case late packets drift in and interfere with the next connection. – user4581301 May 24 '18 at 20:20
  • 1
    The only way would be to send a RST, so the TIME_WAIT state is never entered. Many browsers (used to?) do so when closing a connection to the web server for that reason. – C. Gonzalez May 24 '18 at 20:42
  • Related: https://stackoverflow.com/questions/4160347/close-vs-shutdown-socket – SergeyA May 24 '18 at 20:48
  • 1
    This is normal for TCP. The only way I know of is to tune your system to cycle through time_wait state more quickly. – Hitobat May 24 '18 at 21:22
  • You *do* need the FIN/ACK handshake, whether you believe it or not. – user207421 May 24 '18 at 22:27
  • Unfortunately, *Avoiding TIME_WAIT* is still valid information. But if you need to open and close 100 connections per second, you probably want UDP and not TCP... Without more detail this post is indeed a duplicate (same question and same answer). If you only send single messages per connection without any handshake, then you should use UDP - but this is not what this question asks for... – Serge Ballesta May 25 '18 at 14:23
  • It leave the *local port* alive at one end. The sockets at both ends are closed and of no further use. – user207421 Apr 03 '22 at 04:52

0 Answers0