Go to the first, previous, next, last section, table of contents.

Receiving Datagrams

The recvfrom function reads a packet from a datagram socket and also tells you where it was sent from. This function is declared in `sys/socket.h'.

Function: int recvfrom (int socket, void *buffer, size_t size, int flags, struct sockaddr *addr, socklen_t *length-ptr)
The recvfrom function reads one packet from the socket socket into the buffer buffer. The size argument specifies the maximum number of bytes to be read.

If the packet is longer than size bytes, then you get the first size bytes of the packet, and the rest of the packet is lost. There's no way to read the rest of the packet. Thus, when you use a packet protocol, you must always know how long a packet to expect.

The addr and length-ptr arguments are used to return the address where the packet came from. See section Socket Addresses. For a socket in the file domain, the address information won't be meaningful, since you can't read the address of such a socket (see section The File Namespace). You can specify a null pointer as the addr argument if you are not interested in this information.

The flags are interpreted the same way as for recv (see section Socket Data Options). The return value and error conditions are also the same as for recv.

You can use plain recv (see section Receiving Data) instead of recvfrom if you know don't need to find out who sent the packet (either because you know where it should come from or because you treat all possible senders alike). Even read can be used if you don't want to specify flags (see section Input and Output Primitives).

Go to the first, previous, next, last section, table of contents.