bool StartNetworking(char* hostname)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData);
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;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
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;
}
nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(SOCKADDR_IN));
if (nret == SOCKET_ERROR)
{
nret = WSAGetLastError();
cout << "connect() error: " << nret << "\n";
WSACleanup();
return false;
}
else
{
cout << "Connected\n";
}
cout << "Success\n";
return 1;
}
WinSock, connect, and WSAENOTSOCK
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: 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
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.
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.
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!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement