Why does .Net Socket.Disconnect take two minutes?

I am using the .Net socket class and I am using the following code:

socket.Shutdown(SocketShutdown.Both); socket.Disconnect(true); 

Then it blocks exactly two minutes. I specified true because I am going to reuse the socket immediately and reconnect. Whether I have a Shutdown call or not, it blocks for two minutes. The only thing I can do is pass false to disable. But I want to reuse a socket.

Any ideas?

UPDATE:

I read the codes. I set the DontLinger parameter. It does not help.

UPDATE 2:

I added a network trace for the query:

Trace 1: using the DontLinger option

 System.Net.Sockets Verbose: 0 : [4668] Socket#5009246::Socket(InterNetwork#2) System.Net.Sockets Verbose: 0 : [4668] Exiting Socket#5009246::Socket() System.Net.Sockets Verbose: 0 : [4668] Socket#5009246::Connect(eee:nnnn#-2063562120) System.Net.Sockets Information: 0 : [4668] Socket#5009246 - Created connection from aaa.bbb.ccc.ddd.eee:nnnnn to www.xxx.yyy.zzz.:mmmm. System.Net.Sockets Verbose: 0 : [4668] Exiting Socket#5009246::Connect() System.Net.Sockets Verbose: 0 : [4668] Socket#5009246::Disconnect() System.Net.Sockets Verbose: 0 : [4668] Exiting Socket#5009246::Disconnect() 

Trace 2: using shutdown (SocketShutdown.Both);

 System.Net.Sockets Verbose: 0 : [0300] Socket#5009246::Socket(InterNetwork#2) System.Net.Sockets Verbose: 0 : [0300] Exiting Socket#5009246::Socket() System.Net.Sockets Verbose: 0 : [0300] Socket#5009246::Connect(ddd:eeeee#-2063562120) System.Net.Sockets Information: 0 : [0300] Socket#5009246 - Created connection from aaa.bbb.ccc.ddd:eeee to www.xxx.yyyy.zzzz:nnnnn. System.Net.Sockets Verbose: 0 : [0300] Exiting Socket#5009246::Connect() System.Net.Sockets Verbose: 0 : [0300] Socket#5009246::Shutdown(Both#2) System.Net.Sockets Verbose: 0 : [0300] Exiting Socket#5009246::Shutdown() System.Net.Sockets Verbose: 0 : [0300] Socket#5009246::Disconnect() System.Net.Sockets Verbose: 0 : [0300] Exiting Socket#5009246::Disconnect() 
+7
sockets
source share
4 answers

It turns out there is some registry entry that determines how long the shutdown takes. This has something to do with the WinSock2 api.

-2
source share

A timeout in case of a shutdown, followed by Disconnect or BeginDisconnect, will occur if another Socket does not receive.

Here's how it works: Shutdown (SocketShutdown.Send) (or both) send a zero byte to the other side. And then, if you call Disconnect, it will block until the other side receives this packet with a zero byte. This is why a socket always gets a zero byte at the time of acceptance when gracefully disconnecting from Socket. The Linger option and other settings do not affect this 2 minute delay. You can check the status of the connection to TCPView. Thus, the correct way is to make sure that the other side is either in receive mode, or physically disconnected, or the socket is actually destroyed - for example, the application is terminated (you will get an immediate exception in this case without a 2-minute delay).

http://vadmyst.blogspot.ru/2008/04/proper-way-to-close-tcp-socket.html

+3
source share

This may be what you are looking for.

If you need to call Disconnect without first calling Shutdown, you can set the DontLinger Socket parameter to false and specify a non-zero waiting interval to ensure that data is queued for outgoing transmission. Then turn off the blocks until the data is sent or until the specified timeout expires. If you set DontLinger to false and specify a zero timeout interval, Close will release the connection and automatically drop outgoing data in the queue.

From docs .

+1
source share

This may be a variant of Linger. See here for more information: Graceful shutdown, delay settings and slot closing

in .NET, you can change it using the Socket.SetSocketOption method.

EDIT : if this is not a Linger option, you should try to enable full socket traces, here is a .config example:

 <configuration> <system.diagnostics> <trace autoflush="true" /> <sources> <source name="System.Net.Sockets"> <listeners> <add name="SocketsTrace"/> </listeners> </source> </sources> <sharedListeners> <add name="SocketsTrace" type="System.Diagnostics.TextWriterTraceListener" initializeData="SocketsTrace.log" /> </sharedListeners> <switches> <add name="System.Net.Sockets" value="Verbose" /> </switches> </configuration> 

EDIT : or you can click on what is called TIME_WAIT described here: Please explain the TIME_WAIT state , which is 120 ms. It is usually better to use Close () with SocketOptionName.ReuseAddress rather than using Disconnect (true). See Comments here (in the comments section).

0
source share

All Articles