I created a while loop with the winsock accept() method in it but it throws error 10093 (WSAData not yet initialized) every time it loops. WSAData IS initialized in the main thread that starts the accept thread.

I don't know if this is anything thread related. The code to start the WSAData and the thread is this:

iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed with error: %d\n", iResult);
    return 1;
}

// Things in between (bind, listen...)

std::thread acceptThread(Accept);

And here is the Accept() method I made (well, the actual accept method that is called):

SOCKET temp = accept(ListenSocket, NULL, NULL);

After that I check "temp" and that's when the error occurs

The WSAStartup does work because it doesn't go in the if.

有帮助吗?

解决方案

Sockets do not have a thread affinity, so you can freely create a socket in one thread and use it in another thread. You do not need to call WSAStartup() on a per-thread basis. If accept() reports WSANOTINITIALISED then either WSAStartup() really was not called beforehand, or else WSACleanup() was called prematurely.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top