Is winsock2 safe?

I am writing a small 3 server and 1 client program. 2 servers send tcp messages, and the latter sends updated datagrams using winsock2.

I am wondering if I can do simulanious recvfrom () using threads (OpenMP or boost :: threads) so that 2 threads listen on the same socket in the same port at the same time.

I am using VC ++ 2010 on windows7.

Thank you for your help.

+4
source share
2 answers

Yes, sockets are thread safe, but you have to be careful. One common pattern (when using IO blocking) is to have one stream that receives data on the socket, and another stream that sends data to the same socket. Having multiple streams that receive data from a socket is usually great for a UDP socket, but in most cases it doesn't make much sense for TCP sockets. In the documentation for WSARecv :

WSARecv cannot be used to call different threads simultaneously on the same socket, because this can lead to an unpredictable order buffer.

But this is usually not a concern if you use UDP, and the protocol has no status.

Also note that the WSAEINPROGRESS error code mainly refers to Winsock 1.1:

WSAEINPROGRESS: Windows Sockets 1.1 calls are being blocked or the service provider is still processing the callback function.

And the WSAEINPROGRESS description further states:

The operation is in progress.

The lock operation is in progress. On Windows Sockets, only one lock operation is allowed per task or thread, and if any other function call is made (regardless of whether it refers to this or any other socket), the function fails with a WSAEINPROGRESS error.

Note that this indicates one lock operation for each task or thread.

In addition, the documentation for WSARecv has an additional warning:

Issuing another Winsock blocking call within the APC that interrupted the current Winsock blocking call in the same thread will result in undefined behavior and will never be attempted by Winsock clients.

But besides these warnings you should be fine.

Update: to add some external links: alt.winsock.programming: Does it contain a ceiling? and Frequently Asked Questions for Winsock Programmers: Is Winsock Safe?

+5
source

Winsock only allows one blocking I / O call on a socket. More than one blocking call from another thread will result in a "WSAEINPROGRESS" error. http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668%28v=vs.85%29.aspx#WSAEINPROGRESS .

If you want to make a parallel I / O request, you can try using asynchronous I / O or I / O overlap (in Windows). But, I think you need parallel data processing more than simultaneous read data. In this case, you may have one thread issuing I / O requests and others for processing.

+1
source

All Articles