Back to index

glibc  2.9
inetcli.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <errno.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <sys/types.h>
00006 #include <sys/socket.h>
00007 #include <netinet/in.h>
00008 #include <netdb.h>
00009 
00010 #define PORT         5555
00011 #define MESSAGE             "Yow!!! Are we having fun yet?!?"
00012 #define SERVERHOST   "mescaline.gnu.org"
00013 
00014 void 
00015 write_to_server (int filedes)
00016 {
00017   int nbytes;
00018 
00019   nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
00020   if (nbytes < 0)
00021     {
00022       perror ("write");
00023       exit (EXIT_FAILURE);
00024     }
00025 }
00026 
00027 
00028 int
00029 main (void)
00030 {
00031   extern void init_sockaddr (struct sockaddr_in *name,
00032                           const char *hostname,
00033                           uint16_t port);
00034   int sock;
00035   struct sockaddr_in servername;
00036 
00037   /* Create the socket.  */
00038   sock = socket (PF_INET, SOCK_STREAM, 0);
00039   if (sock < 0)
00040     {
00041       perror ("socket (client)");
00042       exit (EXIT_FAILURE);
00043     }
00044 
00045   /* Connect to the server.  */
00046   init_sockaddr (&servername, SERVERHOST, PORT);
00047   if (0 > connect (sock,
00048                  (struct sockaddr *) &servername,
00049                  sizeof (servername)))
00050     {
00051       perror ("connect (client)");
00052       exit (EXIT_FAILURE);
00053     }
00054 
00055   /* Send data to the server.  */
00056   write_to_server (sock);
00057   close (sock);
00058   exit (EXIT_SUCCESS);
00059 }