WinSock, connect, and WSAENOTSOCK

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I've been playing with asynchronous sockets a bit recently and I've run into the following problem. If I call connect, and then WSAAsyncSelect, everything works just fine. My client and server send and recv data just fine, just I never get the FD_CONNECT message at all. On the other hand, if I choose to call WSAAsyncSelect and then connect, like I'm supposed to I get the FD_CONNECT message but immediately thereafter the server says the client disconnects, but the client seems to think it's still doing OK. Also, the messages sent between the client and server either seem to not reach the other end at all, or the message will arrive with no data attached. Also, if I call WSAASyncSelect and then connect without the server running, I still get the FD_CONNECT message, only now it has the error code WSAENOTSOCK (10038, I believe), and then disconnects. Essential client code follows:
bool StartNetworking(char* hostname)
{
WORD sockVersion;
int nret;

sockVersion = MAKEWORD(1, 1);

LPHOSTENT hostEntry;

hostEntry = gethostbyname(hostname);

if (!hostEntry)
{
nret = WSAGetLastError();
cout << "gethostbyname() error: " << nret << "\n";

WSACleanup();
return false;
}

theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (theSocket == INVALID_SOCKET)
{
nret = WSAGetLastError();
cout << "socket() error: " << nret << "\n";

WSACleanup();
return false;
}

serverInfo.sin_family = AF_INET;
serverInfo.sin_port = htons(8888);

if(WSAAsyncSelect(theSocket, g_hWnd, WM_WSAASYNC, FD_WRITE | FD_CONNECT | FD_READ | FD_CLOSE) == SOCKET_ERROR)
{
nret = WSAGetLastError();
cout << "WSAAsyncSelect() error: " << nret << "\n";

WSACleanup();
return false;
}

if (nret == SOCKET_ERROR)
{
nret = WSAGetLastError();
cout << "connect() error: " << nret << "\n";

WSACleanup();
return false;
}
else
{
cout << "Connected\n";
}

cout << "Success\n";
return 1;
}


Please note that the above code invariable outputs "Connected" and then "Success" and returns 1 (true). The only problems are the disconnecting and the odd error code in FD_CONNECT. Thanks for your time and help! ms291052

Share on other sites
Socket operation on nonsocket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.

error is with server code i'm guessing as it cant find the socket.

Share on other sites
Thank you for the definition of the error. That is exactly why I think the problem is so peculiar, since simply changing the order of the two functions seems to invalidate the socket.

Also, the server code connects to the client with the two function calls reversed just fine, and I've just rechecked it and it looks fine. In addition I get weird client errors when I just run the client and no server at all (see OP)!

Thanks for the reply. Any further comment would be appreciated!

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 41
• Forum Statistics

• Total Topics
634837
• Total Posts
3019561
×