Archived

This topic is now archived and is closed to further replies.

Recv buffer not liking 3+ digits for int values.

This topic is 5112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently have a non-blocking server that is working great and sends 1 digit and 2 digit int values like a champ, but as soon as you go to 3 digits or more it seems to completely screw up. The first 3 digit value can be pulled from the buffer but the 2nd displays junk. Here is my send function:
        Player Send; - just a struct that holds 2 ints and a bool
	char *sendbuffer;
	sendbuffer = new char[200];
        //Populate the buffer.

	sendbuffer[0] = (char)Send.x;
	sendbuffer[2] = (char)Send.y;
	sendbuffer[4] = (char)Send.fight;
	sendbuffer[6] = ''\0'';
	// Hi-Ho Packet! AWAYYYYYYYYY!

	send(socketinfo, &sendbuffer[0], strlen(sendbuffer),0);
	if (socketinfo == SOCKET_ERROR)
	{
		
		send(socketinfo, &sendbuffer[0], strlen(sendbuffer),0);
	}
	ZeroMemory(sendbuffer,200);
        delete [] sendbuffer;
and my Recv call:
        char* recvbuffer;
	recvbuffer = new char[200];
	ZeroMemory(recvbuffer,200);
	recv(Clients[a].Client,recvbuffer,200,0);
	if (Clients[a].Client == SOCKET_ERROR)
	{
		recv(Clients[a].Client,recvbuffer,200,0);
		
	}
	if((int)recvbuffer[0] == -51 || (int)recvbuffer[0] == 0)
	{
		con.Clear();
		Disconnect(a);
	}
        con << "Byte size : " << sizeof(recvbuffer) << "\n";
	con << "X : " << (int)recvbuffer[0] << "\n";
	con << "Y : " << (int)recvbuffer[2] << "\n";
	con << "Fight : " << (int)recvbuffer[4] << "\n";
        delete [] recvbuffer;
It works great for the first value if it''s greater than 2 digits but the others after the first are completely off from what they should be. Any ideas? Also my disconnect call(you can see it in the recv source) doesn''t disconnect 2 clients if the clients try to disconnect at the same time, only one gets disconnected and the other is killed client side but the server''s disconnect is not called. Any help would be appreciated

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your code is the poster child for trying to run before you can walk. I would suggest you carefully read a networking tutorial on non-blocking sockets and how to implement them along with perhaps some tutorials on general C programming.

In your send code:
-you truncate what are likely 32-bit ints into 8-bit chars and stuff them into an array.
-you leave garbage in your array at entries 1, 3 and 5.
-you do not check how much was actually sent by calls to send() nor do you attempt to resend any remaining data.
-you check socketinfo (presumably a socket descriptor) for SOCKET_ERROR instead of checking the return value of send().
-you attempt to resend the entire data chunk upon error.
-there is no reason to zero memory you are going to delete.

In your recv code:
-you attempt to read up to 200 bytes when your send code is only transmitting a few bytes (variable based on what garbage was at values 1, 3 and 5 of the original send array).
-you do not check how much you actually received by the call to recv().
-you check the socket descriptor for SOCKET_ERROR again instead of the return value of recv().
-you overwrite your previous receive buffer upon error and again attempt to receive too many bytes.
-values of -51 or 0 for player.x indicate you should disconnect?

Share this post


Link to post
Share on other sites