0

I build UDP server or TCP server which use recv() or recvfrom for receiving packets from clients

but it seems to me that the mechanism is: the kernel receive packets from network and stripe the IP/TCP/UDP header and then put the data payload part in the kernel buffer, then recv() or recvfrom() read the data in from the kernel buffer

so this means there are only bytes in the buffer, and the bytes are not divided into parts, each of which corresponds to the payload of a UDP datagram/TCP segment

if I hope each call of recv() or recvfrom() only receives one TCP segment or UDP datagram(note, one TCP or UDP packet may includes several IP packets due to IP fragmentation)

is it possible or not?

if so, how? thanks!

1 Answer 1

1

I hope each call of recv() or recvfrom() only receives one TCP segment

No. It may return anything from one byte to the length you supplied (or zero bytes in non-blocking mdoe), and the data may cross TCP segment boundaries, not that you have any way of telling where a TCP segment boundary is in the first place. You have to regard a TCP connection as a byte stream, nothing more.

or UDP datagram

Yes.

Sign up to request clarification or add additional context in comments.

5 Comments

you mean for UDP, by default, each call of recv() and recvfrom() only get 1 udp datagram? so is it possible for each call to get payload bytes of two more more UDP datagram?
besides, if I want to measure TCP segment loss rate, TCP segment arrival order, are there any ways with socket programming?
@misteryes (1) Yes you get exactly one datagram per recv()/recvfrom(), and it follows immediately therefore that (2) no you don't get two at once. I don't know why you had to ask the same question twice. (3) Not without using a packet driver, e.g. libpcap.
sorry, I forgot to ask, so if there are ip fragmentation for one UDP datagram, the recv or recvfrom will wait all ip fragmentations of the same udp datagram arrives? what if a particular IP datagram is lost? are there any timeout mechism for 'recv' to wait for a full udp datagram?
@misteryes If there has been IP fragmentation the datagram won't be delivered to the application at all unless and until all fragments have been received. It will be delivered intact or not at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.