home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / manual / examples / inetclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-05  |  1.1 KB  |  59 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9.  
  10. #define PORT        5555
  11. #define MESSAGE        "Yow!!! Are we having fun yet?!?"
  12. #define SERVERHOST     "churchy.gnu.ai.mit.edu"
  13.  
  14. void 
  15. write_to_server (int filedes)
  16. {
  17.   int nbytes;
  18.  
  19.   nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
  20.   if (nbytes < 0)
  21.     {
  22.       perror ("write");
  23.       exit (EXIT_FAILURE);
  24.     }
  25. }
  26.  
  27.  
  28. int
  29. main (void)
  30. {
  31.   extern void init_sockaddr (struct sockaddr_in *name,
  32.                  const char *hostname, unsigned short int port);
  33.   int sock;
  34.   struct sockaddr_in servername;
  35.  
  36.   /* Create the socket.  */
  37.   sock = socket (PF_INET, SOCK_STREAM, 0);
  38.   if (sock < 0)
  39.     {
  40.       perror ("socket (client)");
  41.       exit (EXIT_FAILURE);
  42.     }
  43.  
  44.   /* Connect to the server.  */
  45.   init_sockaddr (&servername, SERVERHOST, PORT);
  46.   if (0 > connect (sock,
  47.            (struct sockaddr *) &servername,
  48.            sizeof (servername)))
  49.     {
  50.       perror ("connect (client)");
  51.       exit (EXIT_FAILURE);
  52.     }
  53.  
  54.   /* Send data to the server.  */
  55.   write_to_server (sock);
  56.   close (sock);
  57.   exit (EXIT_SUCCESS);
  58. }
  59.