Copy-edit NEWS and fixup ChangeLog entries.
[glibc.git] / manual / examples / filesrv.c
blobbcc7a2081ae47d179bf3259013583354fa447d06
1 /* Datagram Socket Example
2 Copyright (C) 1991-2013 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
24 #define SERVER "/tmp/serversocket"
25 #define MAXMSG 512
27 int
28 main (void)
30 int sock;
31 char message[MAXMSG];
32 struct sockaddr_un name;
33 size_t size;
34 int nbytes;
36 /* Remove the filename first, it's ok if the call fails */
37 unlink (SERVER);
39 /* Make the socket, then loop endlessly. */
40 sock = make_named_socket (SERVER);
41 while (1)
43 /* Wait for a datagram. */
44 size = sizeof (name);
45 nbytes = recvfrom (sock, message, MAXMSG, 0,
46 (struct sockaddr *) & name, &size);
47 if (nbytes < 0)
49 perror ("recfrom (server)");
50 exit (EXIT_FAILURE);
53 /* Give a diagnostic message. */
54 fprintf (stderr, "Server: got message: %s\n", message);
56 /* Bounce the message back to the sender. */
57 nbytes = sendto (sock, message, nbytes, 0,
58 (struct sockaddr *) & name, size);
59 if (nbytes < 0)
61 perror ("sendto (server)");
62 exit (EXIT_FAILURE);