CAsyncSocket 10093 error

760 Views Asked by At

I inherited from CAsyncSocket, implement my own class. Firstly, it starts like:

MyClient::MyClient()//this is the constructor, I will create the socket in this constructor 
{
   if (!Create(0, SOCK_DGRAM, FD_READ | FD_WRITE))
  {
     UINT errCode = GetLastError();
     printf("Create Client socket failed! Errorcode is %d\n", errCode);
  }
}

But it displays Create Client socket failed! Errorcode is 10093.

I searched online, it shows 10093 is because of:

Successful WSAStartup not yet performed.

Either the application has not called WSAStartup or WSAStartup failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or WSACleanup has been called too many times.

Then I revise my code to

MyClient::MyClient()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
    /* Tell the user that we could not find a usable */
    /* Winsock DLL.                                  */
    printf("WSAStartup failed with error: %d\n", err);

}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater    */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we      */
/* requested.                                        */

if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    printf("Could not find a usable version of Winsock.dll\n");
    WSACleanup();

}
else
    printf("The Winsock 2.2 dll was found okay\n");


/* The Winsock DLL is acceptable. Proceed to use it. */

/* Add network programming using Winsock here */

/* then call WSACleanup when done using the Winsock dll */


if (!Create(0, SOCK_DGRAM, FD_READ | FD_WRITE))
{
    UINT errCode = GetLastError();
    printf("Create Client socket failed! Errorcode is %d\n", errCode);
}
WSACleanup();
}

Then run it, it displays:

image

I also tried to add

if (!AfxSocketInit()) 
{ 
AfxMessageBox(IDP_SOCKETS_INIT_FAILED); 
return FALSE; 
}

But it still has the same error.

0

There are 0 best solutions below