Advertisement

recv returns Zero Bytes

Started by July 22, 2003 03:10 PM
5 comments, last by Xgkkp 21 years, 6 months ago
Well, I am writing a system thing you can telnet into, this is only my test, So I''ll probably only turn it into a really simple chat. I''m using Asynchronous sockets, The problem is that I get a call to my (Dialog Box) Window procedure saying that I have Data, but when I call recv it returns zero, and doesn''t fill my buffer. It can''t be the wrong socket, because I only have one client while testing (me!) I''m not sure exactly whihc code to post for so far, but I''ll give it a shot: The actual Procedure calling recv:

BOOL cClient::RecieveData ( )
{
	char DataBuffer[512];
	int BytesRcvd;
	// Receive all the data we have on this socket

	if (BytesRcvd = recv (Socket, DataBuffer, 511, NULL) == SOCKET_ERROR)
                   return FALSE;
	if (BytesRcvd == 0)
		return FALSE; // It always does this


	// ''Fix'' the end of the data stream

	DataBuffer[BytesRcvd] = ''\0'';

	// Process the DAta

	ProcessData( DataBuffer );
	return TRUE;
}
This is where I initialise Winsock:

BOOL WinsockInitialise ()
{
	// Local Variables

	WSADATA wsaData;
	// Set up Address Structure

	Server_Address.sin_family = AF_INET;
	Server_Address.sin_addr.s_addr = INADDR_ANY;
	Server_Address.sin_port = htons(PORT);

	// Start Up Winsock

	if (WSAStartup(MAKEWORD(1, 0), &wsaData))
	    return FALSE;

	// Create a Socket

	Listening_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	// make the socket asynchronous

	WSAAsyncSelect(Listening_Socket, hDialogue, WM_WSAASYNC, FD_READ | FD_WRITE | FD_ACCEPT | FD_CLOSE);
	
	// Bind the Socket to Port

	bind(Listening_Socket, (struct sockaddr *)&Server_Address, sizeof(Server_Address));

	// Listen at the Binded Socket for Connections

	listen(Listening_Socket, SOMAXCONN);

	return TRUE;
}
and... er.. this is the message procedure that accepts:

BOOL Winsock_Message ( WPARAM wParam, LPARAM lParam )
{
	cClient *NewClient;
	switch (WSAGETSELECTEVENT(lParam))
	{
	case FD_ACCEPT:
		SetState("Connecting");
		if (WSAGETSELECTERROR(lParam))
                         return FALSE;
		// Create a new Client

		NewClient = new cClient;
		NewClient->Connect (accept(wParam, NULL, NULL));
		AddClient(NewClient); // Adds the client to the client list

		return TRUE;
	case FD_CLOSE: // A client is remotely disconnecting

		RemoteDisconnect(wParam);
		return TRUE;
	case FD_READ: // Data has arrived

		Assert(GetClientfromSocket(wParam));
		GetClientfromSocket(wParam)->RecieveData();
	}
	return TRUE;
}
I hate posting loads of code, but if anyone could spend a minute looking through I would greatly appreciate it!
Uhh okay, my bad. For some reason, recv return The Number of bytes recieved minus 1 Instead of just the number. I didn't see this anywhere in the documentation, is it wrong?

[EDIT:] I was wrong, see below....

[edited by - Xgkkp on July 28, 2003 10:36:51 AM]
Advertisement
No, recv returns 0 when socket disconnects gracefully, otherwise it returns the number of bytes received.
.
quote:
Original post by Mastaba
No, recv returns 0 when socket disconnects gracefully, otherwise it returns the number of bytes received.


Well, It''s getting the bytes but returning zero, and accepting furthur input. What''s going on?
Change this:-
	if (BytesRcvd = recv (Socket, DataBuffer, 511, NULL) == SOCKET_ERROR)                   return FALSE; 


To this:-
	if ((BytesRcvd = recv (Socket, DataBuffer, 511, NULL)) == SOCKET_ERROR)                   return FALSE; 


Simple...
Okay I think I got it now.

[edited by - Xgkkp on July 28, 2003 10:39:00 AM]
Advertisement
Heh,

This is why nesting logic in if's like that is unfavorable.

Sure it saves space, but ultimately it can slow down the code and lead to errors that aren't readily apparent. Unlearn the unfounded 'coolness' of it, because there is pretty much nothing too useful about complex evaluations typically.

I whole-heartedly recommend doing the following ( just an example ):

      sfd = socket(AF_INET, SOCK_STREAM, 0);   if ( sfd < 0 )      print_error_and_exit_function("Socket problemo");   ....   ....  



.zfod


[edited by - zfod on July 29, 2003 11:01:56 AM]

[edited by - zfod on July 29, 2003 3:42:56 PM]

This topic is closed to new replies.

Advertisement