0

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.

MD XF
  • 7,860
  • 7
  • 40
  • 71
firstaccount
  • 155
  • 2
  • 13
  • Did you [read the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms742213.aspx) yet? The best option is to call `WSAStartup()` **one time** at program start, and `WSACleanup()` **one time** at program end. If you are going to call `WSAStartup()` in your constructor, DON'T call `WSACleanup()` immediately after creating the socket. That is riping Winsock out from behind the socket's back. Call `WSACleanup()` in the destructor instead, after you have closed the socket. Winsock needs to remain initialized for as long as you are using Winsock APIs. – Remy Lebeau Oct 29 '16 at 04:35
  • @RemyLebeau I just tried your solution, call WSAStartup in the main function, but it displays the same assertion fail as the screenshot. – firstaccount Oct 29 '16 at 20:18
  • @RemyLebeau I found that every time when I call AfxSocketInit();, it will definitely appear debug assertion failed warning . – firstaccount Oct 29 '16 at 22:42
  • I don't use MFC, I don't know how `AfxSocketInit()` works. But you have not shown your `Create()` code, or the code that uses `MyClient`. Have you tried using the debugger to find the actual line of code that is displaying the error? What exactly is your code doing at the time of the error? I suspect the actual assertion is not in the code you have shown, but is happening in later code, which could explain why it still happens no matter how you initialize Winsock. Maybe `Create()` is failing and you are not handling it. Or maybe your misuse of `WSACleanup()` is causing problems. – Remy Lebeau Oct 30 '16 at 02:16
  • You are using MFC classes in a non-MFC application. In particular it doesn't have a global `CWinApp` object required for all sorts of global state management. That's what the assertion dialog is telling you. That's unrelated to your other error (for which Remy Lebeau provided a solution). – IInspectable Oct 30 '16 at 02:19

0 Answers0