Harry
Harry

Reputation: 2969

source port number in the recvfrom socket function

I'm wondering on what basis src_addr.sin_port while receiving socket data gets filled up in the last but one argument of recvfrom function, syntax of which is

#include <sys/types.h>
#include <sys/socket.h>

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
                 struct sockaddr *src_addr, socklen_t *addrlen);

And also what's the significance of it, does it help the receiver in any way?

Upvotes: 1

Views: 1073

Answers (1)

dbush
dbush

Reputation: 223689

The IP and port that is populated after a successful call to recvfrom is the source IP and port of the received datagram. This can be logged so the user knows where the datagram came from.

Also, if a datagram needs to be sent back to this endpoint, src_addr can be passed to sendto to specify the destination IP and port.

Upvotes: 2

Related Questions