Client error or server

Started by
2 comments, last by Sfpiano 19 years, 2 months ago
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."
Advertisement
I couldn't find any obvious errors in the code, but that doesn't say much.
Are you sure that the client can reach the server? Have you tried running it locally (i.e. on loopback only)?
You could also try substituting the client for telnet or use some public web server as server.
Ok, I tried telnet-ing into the server and it worked, so I guess the problem lies with the client, but I don't know where. How can I test to see if the client can reach the server? Also, as a clarification, the client and the server should use the same port correct?
//------------------------------------------------------------------------------------------------------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."
Apparently if I set the address of the server from the client as
inet_addr("xxx.xxx.xxx.xxx");

instead of:
hostEntry = gethostbyname("server.com");*((ULONG*)hostEntry->h_addr_list);


it works; can anyone explain this to me?
//------------------------------------------------------------------------------------------------------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."

This topic is closed to new replies.

Advertisement