Archived

This topic is now archived and is closed to further replies.

JMing

Asyconous socket on Winsock2

Recommended Posts

i can create CreateSocket( Socket for request connect ) in not useAsynconous !!BUT when i use Asyconous it error. Error on my server is 10038 (WSAENOTSOCK).!! what can i do??? int G3DSock::CreateClient(char *IP) { sockaddr_in Target; CSocket = socket( AF_INET, SOCK_STREAM, 0 ); //////This Line//////// WSAAsyncSelect(CSocket, App->m_hWnd, WM_CSOCKET, (FD_CLOSE|FD_READ|FD_CONNECT)); /////////////////////////////// Target.sin_family = AF_INET; Target.sin_port = htons (5555); Target.sin_addr.s_addr = inet_addr(IP); //request to connect if( connect(CSocket, (sockaddr*)&Target, sizeof(Target)) == SOCKET_ERROR) { if (WSAGetLastError()==WSAEWOULDBLOCK) { Sleep(750); connect(CSocket, (LPSOCKADDR)&Target, sizeof(Target) ); return 1; } sprintf(App->SockBuffer,"Error: %d",WSAGetLastError() ); MessageBox(App->m_hWnd,App->SockBuffer,"Error", MB_OK); WSACleanup (); geEngine_Printf(App->Engine,300,300,"Can not connect"); return 1; } return 0; }

Share this post


Link to post
Share on other sites
okay, WSAWOULDBLOCK is not *really* an error in this case. Simply
pass over it (set up a catch if statement to ignore it)...

new connect statement...
  
if ( connect(CSocket, (sockaddr*)&Target, sizeof(Target)) == SOCKET_ERROR )
{
if (WSAGetLastError()!=WSAEWOULDBLOCK)
{
sprintf(App->SockBuffer,"Error: %d",WSAGetLastError() );
MessageBox(App->m_hWnd,App->SockBuffer,"Error", MB_OK);

WSACleanup ();
geEngine_Printf(App->Engine,300,300,"Can not connect");
return 1;
}
}


Once you make the connect() call, you have to wait for a
FD_CONNECT message. Then test for connect errors. (use WSAGETSELECTERROR())

so in short- ignore the WSAWOULDBLOCK error, since it''s simply
telling you that the connect() has blocked the socket from
further activity (until an FD_CONNECT).

Jason

ill-lusion.com

ziggy@ill-lusion.com

Share this post


Link to post
Share on other sites