home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / tcpiptk / socket / tcpc.c < prev    next >
Text File  |  1996-11-19  |  4KB  |  131 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.  
  34. /*
  35.  * Client Main.
  36.  */
  37. main(int argc, char *argv[])
  38. {
  39.     unsigned short port;       /* port client will connect to              */
  40.     char buf[12];              /* data buffer for sending and receiving    */
  41.     struct hostent *hostnm;    /* server host name information             */
  42.     struct sockaddr_in server; /* server address                           */
  43.     int s;                     /* client socket                            */
  44.  
  45.     /*
  46.      * Check Arguments Passed. Should be hostname and port.
  47.      */
  48.     if (argc != 3)
  49.     {
  50.         fprintf(stderr, "Usage: %s hostname port\n", argv[0]);
  51.         exit(1);
  52.     }
  53.  
  54.     /*
  55.      * Initialize with sockets.
  56.      */
  57.     sock_init();
  58.  
  59.     /*
  60.      * The host name is the first argument. Get the server address.
  61.      */
  62.     hostnm = gethostbyname(argv[1]);
  63.     if (hostnm == (struct hostent *) 0)
  64.     {
  65.         fprintf(stderr, "Gethostbyname failed\n");
  66.         exit(2);
  67.     }
  68.  
  69.     /*
  70.      * The port is the second argument.
  71.      */
  72.     port = (unsigned short) atoi(argv[2]);
  73.  
  74.     /*
  75.      * Put a message into the buffer.
  76.      */
  77.     strcpy(buf, "the message");
  78.  
  79.     /*
  80.      * Put the server information into the server structure.
  81.      * The port must be put into network byte order.
  82.      */
  83.     server.sin_family      = AF_INET;
  84.     server.sin_port        = htons(port);
  85.     server.sin_addr.s_addr = *((unsigned long *)hostnm->h_addr);
  86.  
  87.     /*
  88.      * Get a stream socket.
  89.      */
  90.     if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  91.     {
  92.         psock_errno("Socket()");
  93.         exit(3);
  94.     }
  95.  
  96.     /*
  97.      * Connect to the server.
  98.      */
  99.     if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
  100.     {
  101.         psock_errno("Connect()");
  102.         exit(4);
  103.     }
  104.  
  105.     if (send(s, buf, sizeof(buf), 0) < 0)
  106.     {
  107.         psock_errno("Send()");
  108.         exit(5);
  109.     }
  110.  
  111.     /*
  112.      * The server sends back the same message. Receive it into the buffer.
  113.      */
  114.     if (recv(s, buf, sizeof(buf), 0) < 0)
  115.     {
  116.         psock_errno("Recv()");
  117.         exit(6);
  118.     }
  119.  
  120.     /*
  121.      * Close the socket.
  122.      */
  123.     soclose(s);
  124.  
  125.     printf("Client Ended Successfully\n");
  126.     exit(0);
  127.  
  128. }
  129.  
  130.  
  131.