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;
}
为什么我在调用Create()并重新启动WSAEINVAL时得到了CResourceException? 接受有时返回WSAEINVAL,根据msdn,我应该绑定套接字 到一个特定的端口和IP地址,但CSocket会在Create调用时绑定,不是吗?
答案 0 :(得分:0)
答案可能是因为Socket.Create()不是线程安全的
因此,如果您从多个线程创建套接字,则可以获得异常
尝试使用Mutex保护对Socket.Create()的调用。你可以在上面找到例子:
http://www.codeproject.com/Articles/7953/Thread-Synchronization-for-Beginners