Packets not fully coming through

Started by
8 comments, last by Terminatore3 16 years, 10 months ago
Hello, I am fairly new to socket programming and I am building my first game using winsock. I am using the UDP protocol for my game and the problem I am having comes when I the server sends a client the positions of all the other players. I have 14 players with fairly large structs. I have found that when I send the positions of all 14 players at once, the client does not receive the full packet, and I'm guessing this has something to do with bandwidth. So my question is, how do I send and receive the entire packet using the UDP protocol. Would I have to loop through recvfrom(); function until I receive all the bytes or is there another way I have to do it? All replies are greatly appreciated.
Advertisement
Is the size of the receiving buffer large enough to handle a complete packet with the 14 large structs in it?
yes, the client buffer and the recvfrom(); function both have the same size as the server buffer and sendto(); function.
Quote:I have 14 players with fairly large structs.

How large? If larger than 536 (give or take) bytes, you may run into problems.

More importantly, how do you write these structs? Do you use proper serialization, or just do (char*)my_struct? Unless you use proper data type aware serialization, you are playing lottery in trying to write reliable code.

Quote:Would I have to loop through recvfrom();

No. Once you call recvfrom, the packet that you were trying to read is lost. Whatever you read, that's it, you can't bring it back, since network stack has already discarded the data from its own buffer.

Keep in mind that UDP makes no delivery guarantees. Some clients might not receive the packets, they might not arrive in order, some might be duplicated, yet others could be corrupted.

Also, there is a limit on network buffer. If you send packets in bursts, especially on local machine or across lan, you may exhaust this buffer, since your call to recv will not be able to process the enough bytes in that short burst.

But if none of these conditions are met, then it's likely a bug in your code.
alright, yes my packets are greater than 536 bytes. And no, i did no serialization, do you have a link to where I could learn how to do that? I also know that UDP is unreliable, I expect that, this is a first person shooter and not looking for reliability; however, I have this problem in all of my packets, not just one.
Quote:Original post by Terminatore3
And no, i did no serialization, do you have a link to where I could learn how to do that? I also know that UDP is unreliable,


Can you store your data structure into an array, then read it immediatelly, and get correct result?

How are you writing your structures into packets?

Quote:Original post by Terminatore3
alright, yes my packets are greater than 536 bytes. And no, i did no serialization, do you have a link to where I could learn how to do that? I also know that UDP is unreliable, I expect that, this is a first person shooter and not looking for reliability; however, I have this problem in all of my packets, not just one.

Your packets are too large, and are thus getting fragmented and dropped. If you need to send that much data, break it into multiple packets.
Well, I did set it up to send the position of each player seperatly to fix the problem; however, I'm not sure if this would work if you were on a low bandwidth connection, I am afraid it may start dropping them again so am trying to find a fool proof method to make sure I don't lose my packets due to large packet size.

As for sending my packets, I do setup my player positions into an array and send it at once. Below is part of my source.


struct players{
int ID;//player ID (duh)
float x;//position
float y;
float z;
float th;//theta horizontal
float tv;//theta vertical
float speed;//speed of the player
int team;//team assigned (0= spectator, 1= team 1, 2= team 2)
bool conn;//connected?
};
.....


players playerarray[14];
sendto(Socket,(char *)playerarray,sizeof(playerarray),0,addr,length);//send data
[\code]

As I said above, I can seperate them into 14 seperate packets, but I want to make sure the client will receive the packet on all bandwidths.
That's not big enough that it should cause trouble.

In general, a UDP packet makes it whole to recvfrom(), or it doesn't make it at all. If you're seeing "half" packets, then one of the following is true:

1) You have a bug in your send code.
2) You have a bug in your receive code.
3) You have a bug in the underlying network implementation.

My bet is on #1 or #2.
enum Bool { True, False, FileNotFound };
alright, thank you for all your help, I have recently rewritten my code so I am unable to test this at this time (i'm a master of breaking code :), but I'm sure with all your help I can figure out how to keep this from being an issue. Thank you very much for all your help.

This topic is closed to new replies.

Advertisement