Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does TCP/UDP protocol always send or receive file packets sequentially? Would it possible to make a choice which data packets will be sent first using sockets API?

share|improve this question
Packets are sent sequentially. UDP however does not guarantee nor the order of received packets nor delivery itself ( Why do you need to reorder stream at socket level (after you written the data)? That seem weird since sending data in order you need is most natural choice anyway. – Petr Gladkikh Dec 19 '11 at 7:42
up vote 9 down vote accepted

Neither TCP nor UDP can guarantee packets arrive at their destination in sequence because they both use IP packets, and IP doesn't do any sequencing.

With UDP, there are two separate issues. One is the order of the packets that comprise a single datagram. You can't control this, and every implementation I know of will send them in order.

However, you can send your datagrams in whatever order you want. The individual fragments that comprise a datagram will be re-ordered by the receiver to reconstruct the original datagram. Datagrams received out of order (not in the order they were sent) will be passed to the application in the order they were received. A datagram is considered received when the last fragment required to re-construct it is received. Datagrams will be passed to the application as they are completed, regardless of the order they were sent.

With TCP, the implementation controls the formation of packets. You can't really control it at all. Every implementation I know of will send them in order. They are required to re-order them if they are received out of order.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .