CSocket listSock,lisRecvSock;
if(!listSock.Create(pwd->m_ServerPort))//sometimes i got a CResourceException,why?
{
pwd->GetSocketError();
return -1;
}
if(!lisRecvSock.Create(pwd->m_ServerPortRecv))
{
pwd->GetSocketError();
return -1;
}
lisRecvSock.Listen(3);
listSock.Listen(3);
//and the Accept sometimes return WSAEINVAL,accordingto msdn,I should Bind the socket
//to a specific port and IP address,but the CSocket would Bind when Create invoked,isn't it?
if(!listSock.Accept(pwd->SendSock))
{
pwd->GetSocketError();
return -1;
}
why i got CResourceException when Create() is invoked and WSAEINVAL was retruned?
the Accept sometimes return WSAEINVAL,accordingto msdn,I should Bind the socket
to a specific port and IP address,but the CSocket would Bind when Create invoked,isn’t it?
The answer is probably because Socket.Create() is not thread safe.
So if you create the socket from more than one thread it can get the exception.
Try to protect the call to Socket.Create() with Mutex. You can find the example on:
http://www.codeproject.com/Articles/7953/Thread-Synchronization-for-Beginners