Advertisement

Connecting to Non-Blocking Server

Started by December 12, 2003 12:19 AM
14 comments, last by ChaosPhoenix 21 years, 1 month ago
Oy, new problem has arose.

Here is my send function:
bytecount = sizeof Player;	bytecount = htonl(bytecount);		send(socketinfo, (char*)&bytecount, sizeof(bytecount), 0);	if (socketinfo == SOCKET_ERROR)	{		send(socketinfo, (char*)&bytecount, sizeof(bytecount), 0);	}	con << bytecount << "\n";	bytecount = ntohl(bytecount);	Player* ptr = &Send	send(socketinfo, (char *)ptr, bytecount ,0);	if (socketinfo == SOCKET_ERROR)	{		send(socketinfo, (char*)ptr, bytecount ,0);	}


and here is my recieve function:
        Player X;	Player *ptr = &X	int bytecount = sizeof(Player);	bytecount = htonl(bytecount);	//recv(Clients[a].Client,buffer,1,0);	recv(Clients[a].Client, (char*) &bytecount, sizeof(bytecount), 0);	if (Clients[a].Client == SOCKET_ERROR)	{		recv(Clients[a].Client, (char*) &bytecount, sizeof(bytecount), 0);	}	recv(Clients[a].Client, (char*)ptr, bytecount, 0);	if (Clients[a].Client == SOCKET_ERROR)	{		recv(Clients[a].Client, (char*)ptr, bytecount, 0);	}	con.Goto(12,0);	if(ptr->fight == 204)	{		con.Goto(5,0);		con << "Client Disconnected!\n";		Disconnect(a);	} 	else	{	con << "Message Recieved!\n";	con << "Name : " << ptr->name << "\n";	con << "X : " << ptr->x << "\n";	con << "Y : " << ptr->y << "\n";	con << "Fight : " << ptr->fight << "\n";	}


Now this way works great for my machine and only my machine due to the pointer being caught since it points to a valid memory location on my computer, for anyone else that connects and sends the struct pointer it just shows bad values on the servers end. I''ve been told I need to pack the entire thing into a character buffer and then some how parse the buffer based on the byte size of each variable and then display those.

Can anyone show me the proper way to send a struct or any other datatype within a character buffer using send and recv?
The
C++ FAQ Lite
describes how to do serialization on data structures.
Advertisement
Heh, SiCrane is my hero. :D

Happen to have an example or two that show serialization at work for networking? :D
Here are a couple of links that are specifically about networking:

Beej''s guide to network programming describes briefly encapsulation for networking:
http://www.ecst.csuchico.edu/~beej/guide/net/html/advanced.html

Mason McClusky wrote an article on pluggable factories for networked code here on gamedev:
http://www.gamedev.net/reference/articles/article841.asp

If you''re concerned about more advanced forms of serialization, you might want to look up tutorials about doing save/load in games. The prinicples are the same, it just that instead of reading/writing to a file, you''re reading/writing to a network stream.
Well luckily it turns out Im just dealing with int''s so I can just place those into a character array and then send that off and just pick off what I want.

//Pseudo code -
//Client
char buffer[256];
Players Player; //structure holding character info.

buffer[0] = Player.X
buffer[1] = Player.Y
etc...
send(socketinfo, &buffer, strlen(sendbuffer),0);

works great on client side, but when I try to recv it using:

char recvbuffer[256];
recv(ClientSocket,&recvbuffer,256,0);

it just displays garbage when I try to read back the parts of the character buffer.
Are Player.X and Player.Y chars? If not, then you might be getting some clobbering effects when you assign them into the char array.

Also, if Player.X or Player.Y, or really any value in your Player class is equal to, or contains a 0 in its binary representation, strlen() might truncate early, so you aren''t sending the whole buffer. (On the other hand, if nothing contains a 0, and you don''t manually null terminate it, strlen() might be returning something much greater than 256.) Check the return values of send() and recv() to double check just how much data is being moved over the wire.

Otherwise, without more context it''s hard to tell if you''re send()/recv() calls are being made correctly.

As an aside, I''m going on vacation for the week, so if you have more questions, it might be a good idea to start a new topic. I''m sure most other forum goers have stopped looking into this particular thread.

This topic is closed to new replies.

Advertisement