uocis  CIS 432/532 Introduction to Computer Networks - Fall 2003


Using recvfrom(2)

The recvfrom() function receives a datagram from a bound socket. If the socket is also connected, you can just use recv() which takes fewer arguments.


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

...

ssize_t bytes;
char buf[65536];
struct sockaddr_un from;
socklen_t fromlen;

fromlen = sizeof (from);
bytes = recvfrom (s, buf, sizeof buf, 0, (struct sockaddr *) &from, &fromlen);
if (bytes < 0) perror ("recvfrom failed");
else printf ("Received %d bytes from %s\n", bytes, from.sun_path);


Created by: Daniel Stutzbach December 15, 2003