C unix domain sockets, recvfrom() doesn't set struct sockaddr* src_addr C unix domain sockets, recvfrom() doesn't set struct sockaddr* src_addr unix unix

C unix domain sockets, recvfrom() doesn't set struct sockaddr* src_addr


The solution is binding the socket on the client side when using Unix domain sockets. Otherwise the transient pathname created for sending the UDP packet immediately disappears after sendto(), which explains why the client's address information is not available on the server side.

See Stevens Network Programming page 419 or see this for an example client implementation that solves this issue: libpix.org/unp/unixdgcli01_8c_source.html

#include    "unp.h"intmain(int argc, char **argv){    int                 sockfd;    struct sockaddr_un  cliaddr, servaddr;    sockfd = Socket(AF_LOCAL, SOCK_DGRAM, 0);    bzero(&cliaddr, sizeof(cliaddr));       /* bind an address for us */    cliaddr.sun_family = AF_LOCAL;    strcpy(cliaddr.sun_path, tmpnam(NULL));    Bind(sockfd, (SA *) &cliaddr, sizeof(cliaddr));    bzero(&servaddr, sizeof(servaddr)); /* fill in server's address */    servaddr.sun_family = AF_LOCAL;    strcpy(servaddr.sun_path, UNIXDG_PATH);    dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));    exit(0);}

Note: unp.h defines Bind() which is simply bind() with some error checking(commonly used throughout Stevens Network Programming). In the same manner, (SA *) is the equivalent to (struct sockaddr *).