Back to index

glibc  2.9
filecli.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <errno.h>
00003 #include <unistd.h>
00004 #include <stdlib.h>
00005 #include <sys/socket.h>
00006 #include <sys/un.h>
00007 
00008 #define SERVER       "/tmp/serversocket"
00009 #define CLIENT       "/tmp/mysocket"
00010 #define MAXMSG       512
00011 #define MESSAGE      "Yow!!! Are we having fun yet?!?"
00012 
00013 int
00014 main (void)
00015 {
00016   extern int make_named_socket (const char *name);
00017   int sock;
00018   char message[MAXMSG];
00019   struct sockaddr_un name;
00020   size_t size;
00021   int nbytes;
00022 
00023   /* Make the socket. */
00024   sock = make_named_socket (CLIENT);
00025 
00026   /* Initialize the server socket address. */
00027   name.sun_family = AF_LOCAL;
00028   strcpy (name.sun_path, SERVER);
00029   size = strlen (name.sun_path) + sizeof (name.sun_family);
00030 
00031   /* Send the datagram. */
00032   nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
00033                  (struct sockaddr *) & name, size);
00034   if (nbytes < 0)
00035     {
00036       perror ("sendto (client)");
00037       exit (EXIT_FAILURE);
00038     }
00039 
00040   /* Wait for a reply. */
00041   nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
00042   if (nbytes < 0)
00043     {
00044       perror ("recfrom (client)");
00045       exit (EXIT_FAILURE);
00046     }
00047 
00048   /* Print a diagnostic message. */
00049   fprintf (stderr, "Client: got message: %s\n", message);
00050 
00051   /* Clean up. */
00052   remove (CLIENT);
00053   close (sock);
00054 }