Sign in to follow this  
dadads

Threading and UDP questions

Recommended Posts

Hi all, As a beginner, I got a lot of questions to ask: 1. If I have 10 threads read/write to a single data structure (possibly at the same time), is this a problem? If it is, how do I prevent it from happening? 2. I've read in a tutorial that UDP packets may arrive at its destination out of order. Does it mean that when I send "apple", it might be received as "apelp"? 3. When I send an set of bytes through UDP, are there any chances that it will be segmented into separate packets before it gets sent? How do I prevent, or cope with this? 4. If an UDP packet gets damaged along the way, does the transmission end there or does it still arrive at its destination even though it's damaged? 5. How do people usually check the correctness of UDP packets that their program might receive? Thanks in advance, - dadads -

Share this post


Link to post
Share on other sites
Quote:
Original post by dadads
Hi all,

As a beginner, I got a lot of questions to ask:

1. If I have 10 threads read/write to a single data structure (possibly at the same time), is this a problem? If it is, how do I prevent it from happening?


Yes, that is a problem. You fix it by some synchronization primitive or other method.

Quote:

2. I've read in a tutorial that UDP packets may arrive at its destination out of order. Does it mean that when I send "apple", it might be received as "apelp"?


No. There's a certain size (which can be different from network to network) under which you can be fairly certain that stuff will arrive as is (~1500 bytes iirc). That said, if you sent each letter as it's own packet with some sort of padding, then yes it might be received in some mangled order.

Quote:

3. When I send an set of bytes through UDP, are there any chances that it will be segmented into separate packets before it gets sent? How do I prevent, or cope with this?


Yes. You include some sort of size header or message delimiter or other mechanism to arrange the data on the receiving end. I'm not too sure about arrangement, I avoid UDP.

Quote:

4. If an UDP packet gets damaged along the way, does the transmission end there or does it still arrive at its destination even though it's damaged?


IIRC, your network driver will toss the packet if it fails its checksum.



Anyways, the networking forum has a good/useful faq. Also UDP ordering problems are well known, with well known solutions made by folks far more skilled than I.

Share this post


Link to post
Share on other sites
Quote:
1. If I have 10 threads read/write to a single data structure (possibly at the same time), is this a problem? If it is, how do I prevent it from happening?


Why 10 threads? Do you expect to have 1000 concurrent client? Unless you allocate one receive and one send buffer for each thread, you might as well use a single thread - synchronizing at this point will limit you to single-thread, while others will wait.

Quote:
2. I've read in a tutorial that UDP packets may arrive at its destination out of order. Does it mean that when I send "apple", it might be received as "apelp"?


If you send 3 packets: "apple", "orange" and "peach", they might arrive as "apple", "peach", "peach". Or, "apple", "peach", "orange". Order in which the packets are received depends on routing, some packets may be duplicated.

Quote:
3. When I send an set of bytes through UDP, are there any chances that it will be segmented into separate packets before it gets sent? How do I prevent, or cope with this?


By sending less than 536 bytes per packet (minimum MTU - UDP/IP header size).

Quote:
4. If an UDP packet gets damaged along the way, does the transmission end there or does it still arrive at its destination even though it's damaged?


Yes. Or no. It can get lost in the first place.

Quote:
5. How do people usually check the correctness of UDP packets that their program might receive?


By manually applying some form of checksum to the packet, and testing each packet received for that value. Calculate CRC for the data, the append that to the packet.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this