Advertisement

sockets +server select func + multiple packets?

Started by February 12, 2018 05:47 PM
3 comments, last by hplus0603 6 years, 9 months ago

Guess i use select to find whenever one peer fd is ready for some action, as far as i understand sockets whenevrr client sends some packet and read func returns end of file that means that server received a packet from a client, now what about when client send two packrts one after another, does select will return ready for that client to process first packet and after calling select again (and read) will it make the same client filedescriptor ready to receive that second one?

Select() only indicates that there is something that needs action. 

There can be more than one message available. You should generally read all the messages available from the stream until no more messages are available.

In stream-based protocols like TCP it is also possible to retrieve a partial message, as small as a single byte.  Or it can mean full messages plus some bytes of a subsequent message. If it is possible to read partial messages then you also need to be careful about buffering the data so the partial data is not lost, and the complete message is put together when the entire contents are available.

Advertisement

Thanks

The definition of recv() and send() are different from the definition of read() and write(), for the very important reason that recv() and send() are implemented to work well with select().

Thus, recv() and send() guarantee that, if select() have previously returned "available," they will make at least some progress (for TCP, this means one byte, for UDP, this means one packet) without blocking. They may of course make more than a single byte of progress, depending on how much buffered data or space is available in the kernel.

For UDP, multiple datagrams will never be "merged" -- one datagram is returned per call. (If you pass in a buffer that is smaller than the datagram, the excess is lost!)

Typically, you use recvfrom() for UDP, so you know who sent the datagram, and you can pass in a buffer that is 65536 bytes in size, which is the maximum possible size for a UDP datagram.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement