C# TCP Socket "Blocking" Property Inconsistency
I use a tcp socket in the following way:
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.ReceiveTimeout = 15;
The general flow is that I run the m_socket.Receive in an infinite while loop and at some point the socket becomes empty for a long period but I don't want to close it. Instead I want to keep trying to read and timeout with exception every 15 seconds.
What happens is that in the first time the socket becomes empty I get a timeout socket exception after 15 seconds as expected. Then, because I'm still in the loop, m_socket.Receive is called again - however this time a non-blocking socket operation could not be completed exception is thrown (which is not what I expect, it supposes to block for another 15 seconds) even though when I query m_socket.Blocking one line above, it says TRUE.
The interesting thing is that if I perform m_socket.Blocking = true; just before m_socket.Receive, everything runs as expected (only timeout exceptions are thrown every 15 seconds).
The only possible explanation is that the timeout exception somehow changed the Receive operation or the Socket operations in general to work in a non-blocking way while not changing the "Blocking开发者_StackOverflow中文版" property accordingly.
I've found this
Apparently, there's a bug regarding this issue that was fixed in .NET 4.0
Are you blocking on another winsock function call, on the same thread?
from msdn:
Note When issuing a blocking Winsock call, such as send, recv, select, accept, or connect function calls, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread, and thereby create unspecified results. Do not issue blocking Winsock function calls without being certain that the current thread is not waiting inside another blocking Winsock function call; doing so results in unpredictable behavior.
ReceiveTimeout is specified in milliseconds. You should set 15000 to make it work. Look at http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.receivetimeout%28v=vs.110%29.aspx
精彩评论