Update.
[glibc.git] / manual / examples / filecli.c
blob9f64445fa95d7fd303e7451faf3902b2576a6f5a
1 #include <stdio.h>
2 #include <errno.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
8 #define SERVER "/tmp/serversocket"
9 #define CLIENT "/tmp/mysocket"
10 #define MAXMSG 512
11 #define MESSAGE "Yow!!! Are we having fun yet?!?"
13 int
14 main (void)
16 extern int make_named_socket (const char *name);
17 int sock;
18 char message[MAXMSG];
19 struct sockaddr_un name;
20 size_t size;
21 int nbytes;
23 /* Make the socket. */
24 sock = make_named_socket (CLIENT);
26 /* Initialize the server socket address. */
27 name.sun_family = AF_LOCAL;
28 strcpy (name.sun_path, SERVER);
29 size = strlen (name.sun_path) + sizeof (name.sun_family);
31 /* Send the datagram. */
32 nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
33 (struct sockaddr *) & name, size);
34 if (nbytes < 0)
36 perror ("sendto (client)");
37 exit (EXIT_FAILURE);
40 /* Wait for a reply. */
41 nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
42 if (nbytes < 0)
44 perror ("recfrom (client)");
45 exit (EXIT_FAILURE);
48 /* Print a diagnostic message. */
49 fprintf (stderr, "Client: got message: %s\n", message);
51 /* Clean up. */
52 remove (CLIENT);
53 close (sock);