Okay, so I'm using WinSockets2 to make a pretty simple server-client application. I have my sockets initialized and I'm trying to allow the server to be multi-user. I am almost 100% sure that up to this point, the server socket is correctly formed and I am 100% sure that the client socket is correct as I used it with another server. Though, the recv
function fails with error WSAENOTSOCK
. The code in question:
unsigned __stdcall client(void *data) {
SOCKET clientSocket = (SOCKET)data;
char inBuffer[DEFAULT_BUFLEN];
int inBytes = SOCKET_ERROR;
ZeroMemory(inBuffer, sizeof(inBuffer)); //make sure the buffer is 0
inBytes = recv(clientSocket, inBuffer, sizeof(inBuffer), 0);
printf("Client said: %s and there's this also %d\n", inBuffer, inBytes);
if (inBytes == SOCKET_ERROR) {
printf("eek! Something went wrong! %ld\n", WSAGetLastError());
}
return 0;
}
and a little bit below it...
bool serverOn = true;
while (clientSocket = accept(listenSocket, NULL, NULL) && serverOn) {
//http://stackoverflow.com/questions/15185380/tcp-winsock-accept-multiple-connections-clients
unsigned threadID;
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &client, (void*)clientSocket, 0, &threadID);
}
if (iResult = shutdown(clientSocket, SD_SEND) == SOCKET_ERROR) { //clean up and end everything
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
If any other code is needed, please ask. Thanks!