Update.
[glibc.git] / manual / examples / filesrv.c
blob32507c6555acc6b87306ce3bd4c8e54d91a5c9d5
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <sys/socket.h>
5 #include <sys/un.h>
7 #define SERVER "/tmp/serversocket"
8 #define MAXMSG 512
10 int
11 main (void)
13 int sock;
14 char message[MAXMSG];
15 struct sockaddr_un name;
16 size_t size;
17 int nbytes;
19 /* Remove the filename first, it's ok if the call fails */
20 unlink (SERVER);
22 /* Make the socket, then loop endlessly. */
23 sock = make_named_socket (SERVER);
24 while (1)
26 /* Wait for a datagram. */
27 size = sizeof (name);
28 nbytes = recvfrom (sock, message, MAXMSG, 0,
29 (struct sockaddr *) & name, &size);
30 if (nbytes < 0)
32 perror ("recfrom (server)");
33 exit (EXIT_FAILURE);
36 /* Give a diagnostic message. */
37 fprintf (stderr, "Server: got message: %s\n", message);
39 /* Bounce the message back to the sender. */
40 nbytes = sendto (sock, message, nbytes, 0,
41 (struct sockaddr *) & name, size);
42 if (nbytes < 0)
44 perror ("sendto (server)");
45 exit (EXIT_FAILURE);