Back to index

glibc  2.9
filesrv.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <errno.h>
00003 #include <stdlib.h>
00004 #include <sys/socket.h>
00005 #include <sys/un.h>
00006 
00007 #define SERVER       "/tmp/serversocket"
00008 #define MAXMSG       512
00009 
00010 int
00011 main (void)
00012 {
00013   int sock;
00014   char message[MAXMSG];
00015   struct sockaddr_un name;
00016   size_t size;
00017   int nbytes;
00018 
00019   /* Remove the filename first, it's ok if the call fails */
00020   unlink (SERVER);
00021 
00022   /* Make the socket, then loop endlessly. */
00023   sock = make_named_socket (SERVER);
00024   while (1)
00025     {
00026       /* Wait for a datagram. */
00027       size = sizeof (name);
00028       nbytes = recvfrom (sock, message, MAXMSG, 0,
00029                       (struct sockaddr *) & name, &size);
00030       if (nbytes < 0)
00031        {
00032          perror ("recfrom (server)");
00033          exit (EXIT_FAILURE);
00034        }
00035 
00036       /* Give a diagnostic message. */
00037       fprintf (stderr, "Server: got message: %s\n", message);
00038 
00039       /* Bounce the message back to the sender. */
00040       nbytes = sendto (sock, message, nbytes, 0,
00041                      (struct sockaddr *) & name, size);
00042       if (nbytes < 0)
00043        {
00044          perror ("sendto (server)");
00045          exit (EXIT_FAILURE);
00046        }
00047     }
00048 }