CSocket problem

Started by
2 comments, last by CJ 24 years, 7 months ago
I have the following code


code:
int CChatboxDlg::StartWinsock(){	WSADATA d_winsockData;	unsigned short d_winsockVersion;	char buffer[128];  	int error;		d_winsockVersion = MAKEWORD( WINSOCK_MAJOR_VERSION, WINSOCK_MINOR_VERSION );  	error = WSAStartup( d_winsockVersion, &d_winsockData );	if( error == SOCKET_ERROR ) 	{    		if( error == WSAVERNOTSUPPORTED ) 		{			sprintf( buffer, "WSAStartup error.\nRequested Winsock v%d.%d, found v%d.%d.", WINSOCK_MAJOR_VERSION, WINSOCK_MINOR_VERSION, LOBYTE( d_winsockData.wVersion ), HIBYTE( d_winsockData.wVersion ) );			WSACleanup();		} 		else 		{			sprintf( buffer, "WSAStartup error (%d)", WSAGetLastError() );		}		MessageBox(buffer, "Chatter", MB_OK );    		return 1;  	}	else	{		sprintf(buffer, "Requested Winsock v%d.%d found", WINSOCK_MAJOR_VERSION, WINSOCK_MINOR_VERSION);		MessageBox(buffer, "Chatter", MB_OK);	}	_setmode(_fileno(stdout), _O_BINARY);	return 0;  // no problems!}void CChatboxDlg::GetChat(LPCSTR lpServerName, LPCSTR lpFileName){	IN_ADDR		iaHost;	LPHOSTENT	lpHostEntry;	iaHost.s_addr = inet_addr(lpServerName);	if (iaHost.s_addr == INADDR_NONE)	{		// Wasn't an IP address string, assume it is a name		lpHostEntry = gethostbyname(lpServerName);	}	else	{		// It was a valid IP address string		lpHostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);	}		if (lpHostEntry == NULL)	{		AfxMessageBox("No host found", MB_OK);		return;	}	SOCKET	Socket;		Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	if (Socket == INVALID_SOCKET)	{		AfxMessageBox("Invalid Socket", MB_OK);		return;	}	//	// Find the port number for the HTTP service on TCP	//	LPSERVENT lpServEnt;	SOCKADDR_IN saServer;	lpServEnt = getservbyname("http", "tcp");		if (lpServEnt == NULL)		saServer.sin_port = htons(80);	else		saServer.sin_port = lpServEnt->s_port;	//	// Fill in the rest of the server address structure	//	saServer.sin_family = AF_INET;	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);	int nRet;	nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));	if (nRet == SOCKET_ERROR)	{		AfxMessageBox("Connection failed", MB_OK);		closesocket(Socket);		return;	}	char szBuffer[1024];	sprintf(szBuffer, "GET %s\n", lpFileName);	nRet = send(Socket, szBuffer, strlen(szBuffer), 0);	if (nRet == SOCKET_ERROR)	{		AfxMessageBox("Sending request failed", MB_OK);		closesocket(Socket);			return;	}	while(1)	{		// Wait to receive, nRet = NumberOfBytesReceived		nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);		if (nRet == SOCKET_ERROR)		{			AfxMessageBox("Receiving failed", MB_OK);			return;		}		fprintf(stderr,"\nrecv() returned %d bytes", nRet);		// Did the server close the connection?		if (nRet == 0)			break;		// Write to stdout        fwrite(szBuffer, nRet, 1, stdout);	}	closesocket(Socket);	}

and something is going wrong.......

I ask for GetChat like


code:
GetChat("205.147.005.037", "/cgi-bin/livechat.pl");

Now......I've found that the problem lies within the while-loop. So if anybody can help me with this. I'd be grateful.

This is just to get familiar with programming multiplayer......so don't refer me to another board. Thanks in advance.....
the address is just for general purpose....to see if it's working, since my server will look like that (though THAT address is real)

------------------
Dance with me......

[This message has been edited by CJ (edited September 12, 1999).]

Advertisement
Sorry about that......I put a LOT of spaces between them but it just doesn't copy and pastes it right. Anyway...

how can I use that blocking stuff? I have little experience with Sockets. So if you could please tell me what to change, I'd be grateful. (WOW, this time I'm BEGGING!!!!!)

------------------
Dance with me......

This topic is closed to new replies.

Advertisement