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


Using sendto(2)

The sendto() function is used to send a datagram. If the socket is connected, then send() can be used instead. For Unix Domain sockets, the socket must be bound. If it is not bound, there will be no error, but the receiver will not know who the datagram came from.


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

...

ssize_t bytes;
void *data;
size_t len;
struct sockaddr_un remote;

/* Set "data" to point to something here.  Set "len" to be the size of
 * that data in bytes. */

remote.sun_family = AF_UNIX;
strcpy (remote.sun_path, "/tmp/some-socket");

bytes = sendto (s, data, len, 0, (struct sockaddr *) &remote, sizeof remote);
if (bytes < 0) perror ("sendto() failed");


Created by: Daniel Stutzbach December 15, 2003