Debian Codegraph @

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

raw
1 /* Example from the glibc manual (16.10.4). */
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 <unistd.h>
8 #include <stdlib.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include "fd-glibc-make_named_socket.h"
12
13 #define SERVER "/tmp/serversocket"
14 #define CLIENT "/tmp/mysocket"
15 #define MAXMSG 512
16 #define MESSAGE "Yow!!! Are we having fun yet?!?"
17
18 int
19 main (void)
20 {
21 int sock;
22 char message[MAXMSG];
23 struct sockaddr_un name;
24 size_t size;
25 int nbytes;
26
27 /* Make the socket. */
28 sock = make_named_socket (CLIENT);
29
30 /* Initialize the server socket address. */
31 name.sun_family = AF_LOCAL;
32 strcpy (name.sun_path, SERVER);
33 size = strlen (name.sun_path) + sizeof (name.sun_family);
34
35 /* Send the datagram. */
36 nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
37 (struct sockaddr *) & name, size);
38 if (nbytes < 0)
39 {
40 perror ("sendto (client)");
41 exit (EXIT_FAILURE);
42 }
43
44 /* Wait for a reply. */
45 nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
46 if (nbytes < 0)
47 {
48 perror ("recfrom (client)");
49 exit (EXIT_FAILURE);
50 }
51
52 /* Print a diagnostic message. */
53 fprintf (stderr, "Client: got message: %s\n", message);
54
55 /* Clean up. */
56 remove (CLIENT);
57 close (sock);
58 }