Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/fd-glibc-datagram-socket.c

raw
1 /* Example from glibc manual (16.10.3). */
2 /* { dg-require-effective-target sockets } */
3 /* { dg-skip-if "" { hppa*-*-hpux* powerpc*-*-aix* } } */
4
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <unistd.h>
11 #include "fd-glibc-make_named_socket.h"
12
13 #define SERVER "/tmp/serversocket"
14 #define MAXMSG 512
15
16 int
17 main (void)
18 {
19 int sock;
20 char message[MAXMSG];
21 struct sockaddr_un name;
22 socklen_t size;
23 int nbytes;
24
25 /* Remove the filename first, its ok if the call fails */
26 unlink (SERVER);
27
28 /* Make the socket, then loop endlessly. */
29 sock = make_named_socket (SERVER);
30 while (1)
31 {
32 /* Wait for a datagram. */
33 size = sizeof (name);
34 nbytes = recvfrom (sock, message, MAXMSG, 0,
35 (struct sockaddr *) & name, &size);
36 if (nbytes < 0)
37 {
38 perror ("recfrom (server)");
39 exit (EXIT_FAILURE);
40 }
41
42 /* Give a diagnostic message. */
43 fprintf (stderr, "Server: got message: %s\n", message);
44
45 /* Bounce the message back to the sender. */
46 nbytes = sendto (sock, message, nbytes, 0,
47 (struct sockaddr *) & name, size);
48 if (nbytes < 0)
49 {
50 perror ("sendto (server)");
51 exit (EXIT_FAILURE);
52 }
53 }
54 }