I'm trying to getup a basic client-server chat program in winsock, and my client program keeps timging out. Is this a server error, or a client error? Client:
int nret;
WSADATA wsaData;
LPHOSTENT hostEntry;
if( WSAStartup(MAKEWORD(2,0), &wsaData) != NULL ) {
exit(0);
}
hostEntry = gethostbyname("sfpiano.rh.rit.edu");
fprintf(stdout, "Connecting to: %s", inet_ntoa (*(struct in_addr *)*hostEntry->h_addr_list) );
if( !hostEntry ) {
exit(0);
}
SOCKET listenSock;
listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( listenSock == INVALID_SOCKET ) {
exit(0);
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr= *((ULONG*)hostEntry->h_addr_list);
serverInfo.sin_port = htons(5505);
if( NETFAIL(connect(listenSock, (LPSOCKADDR)&serverInfo, sizeof(sockaddr))) ) {
exit(0);
}
Server
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
if( WSAStartup(MAKEWORD(1,1), &wsaData) != NULL ) {
exit(0);
}
SOCKET listenSock;
listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( listenSock == INVALID_SOCKET ) {
exit(0);
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(5505);
if( NETFAIL( bind(listenSock, (LPSOCKADDR)&serverInfo, sizeof(sockaddr))) ) {
exit(0);
}
if( NETFAIL( listen(listenSock, 10) ) ) {
exit(0);
}
SOCKET client;
client = accept(listenSock, NULL, NULL);
if( client == INVALID_SOCKET ) {
exit(0);
}
cout<<"User Connected\n";
closesocket(client);
closesocket(listenSock);
WSACleanup();
}
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."