home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / socket / tcpc.c < prev    next >
Text File  |  1999-05-11  |  4KB  |  134 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 1990,1991.                              */
  6. /*                                                                          */
  7. /*   All rights reserved.                                                   */
  8. /*                                                                          */
  9. /*   US Government Users Restricted Rights -                                */
  10. /*   Use, duplication or disclosure restricted by GSA ADP Schedule          */
  11. /*   Contract with IBM Corp.                                                */
  12. /*                                                                          */
  13. /*--------------------------------------------------------------------------*/
  14. /*                                                                          */
  15. /*  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is             */
  16. /*  sample code created by IBM Corporation. This sample code is not         */
  17. /*  part of any standard or IBM product and is provided to you solely       */
  18. /*  for  the purpose of assisting you in the development of your            */
  19. /*  applications.  The code is provided "AS IS", without                    */
  20. /*  warranty of any kind.  IBM shall not be liable for any damages          */
  21. /*  arising out of your use of the sample code, even if they have been      */
  22. /*  advised of the possibility of such damages.                             */
  23. /*--------------------------------------------------------------------------*/
  24. /*
  25.  * Include Files.
  26.  */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <types.h>
  30. #include <netinet\in.h>
  31. #include <sys\socket.h>
  32. #include <netdb.h>
  33. #include <sys/time.h>
  34. #include <unistd.h>
  35. #include <string.h>
  36.  
  37. /*
  38.  * Client Main.
  39.  */
  40. main(int argc, char *argv[])
  41. {
  42.     unsigned short port;       /* port client will connect to              */
  43.     char buf[12];              /* data buffer for sending and receiving    */
  44.     struct hostent *hostnm;    /* server host name information             */
  45.     struct sockaddr_in server; /* server address                           */
  46.     int s;                     /* client socket                            */
  47.  
  48.     /*
  49.      * Check Arguments Passed. Should be hostname and port.
  50.      */
  51.     if (argc != 3)
  52.     {
  53.         fprintf(stderr, "Usage: %s hostname port\n", argv[0]);
  54.         exit(1);
  55.     }
  56.  
  57.     /*
  58.      * Initialize with sockets.
  59.      */
  60.     sock_init();
  61.  
  62.     /*
  63.      * The host name is the first argument. Get the server address.
  64.      */
  65.     hostnm = gethostbyname(argv[1]);
  66.     if (hostnm == (struct hostent *) 0)
  67.     {
  68.         fprintf(stderr, "Gethostbyname failed\n");
  69.         exit(2);
  70.     }
  71.  
  72.     /*
  73.      * The port is the second argument.
  74.      */
  75.     port = (unsigned short) atoi(argv[2]);
  76.  
  77.     /*
  78.      * Put a message into the buffer.
  79.      */
  80.     strcpy(buf, "the message");
  81.  
  82.     /*
  83.      * Put the server information into the server structure.
  84.      * The port must be put into network byte order.
  85.      */
  86.     server.sin_family      = AF_INET;
  87.     server.sin_port        = htons(port);
  88.     server.sin_addr.s_addr = *((unsigned long *)hostnm->h_addr);
  89.  
  90.     /*
  91.      * Get a stream socket.
  92.      */
  93.     if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  94.     {
  95.         psock_errno("Socket()");
  96.         exit(3);
  97.     }
  98.  
  99.     /*
  100.      * Connect to the server.
  101.      */
  102.     if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
  103.     {
  104.         psock_errno("Connect()");
  105.         exit(4);
  106.     }
  107.  
  108.     if (send(s, buf, sizeof(buf), 0) < 0)
  109.     {
  110.         psock_errno("Send()");
  111.         exit(5);
  112.     }
  113.  
  114.     /*
  115.      * The server sends back the same message. Receive it into the buffer.
  116.      */
  117.     if (recv(s, buf, sizeof(buf), 0) < 0)
  118.     {
  119.         psock_errno("Recv()");
  120.         exit(6);
  121.     }
  122.  
  123.     /*
  124.      * Close the socket.
  125.      */
  126.     soclose(s);
  127.  
  128.     printf("Client Ended Successfully\n");
  129.     exit(0);
  130.  
  131. }
  132.  
  133.  
  134.