home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / accxrecv / recv.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  4KB  |  124 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 1990,1996.                              */
  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. /*  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is             */
  15. /*  sample code created by IBM Corporation. This sample code is not         */
  16. /*  part of any standard or IBM product and is provided to you solely       */
  17. /*  for  the purpose of assisting you in the development of your            */
  18. /*  applications.  The code is provided "AS IS", without                    */
  19. /*  warranty of any kind.  IBM shall not be liable for any damages          */
  20. /*  arising out of your use of the sample code, even if they have been      */
  21. /*  advised of the possibility of such damages.                             */
  22. /*--------------------------------------------------------------------------*/
  23. /************************** RECV.C *****************************************/
  24. /* TCP client */
  25.  
  26. /*
  27.  * Include Files.
  28.  */
  29. #define INCL_DOSPROCESS
  30. #define INCL_32.h
  31. #define INCL_DOS.h
  32. #include <types.h>
  33. #include <netinet\in.h>
  34. #include <sys\socket.h>
  35. #include <netdb.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. /*
  41.  * Client Main.
  42.  */
  43. void main(int argc, char *argv[])
  44. {
  45.     unsigned short port;       /* port client will connect to              */
  46.     char buf[1024];              /* data buffer for sending and receiving    */
  47.     struct hostent *hostnm;    /* server host name information             */
  48.     struct sockaddr_in server; /* server address                           */
  49.     int s , i,p;                     /* client socket                            */
  50.     int err;
  51.     char tempstr[100];
  52.  
  53.     /*
  54.      * Check arguments. Should be only one: the port number to bind to.
  55.      */
  56.  
  57.     if (argc != 2)
  58.     {
  59.         fprintf(stderr, "Usage: %s [server port]\n", argv[0]);
  60.         exit(1);
  61.     }
  62.  
  63.     sock_init();
  64.  
  65.     port = (unsigned int  ) atoi(argv[1]);
  66.     printf("port of server is %d\n", port);
  67.  
  68.     /*
  69.      * Put a message into the buffer.
  70.      */
  71.     strcpy(buf, "messages");
  72.  
  73.     /*
  74.      * Put the server information into the server structure.
  75.      * The port must be put into network byte order.
  76.      */
  77.  
  78.     server.sin_family      = AF_INET;
  79.     server.sin_port        = htons(port);
  80.     server.sin_addr.s_addr = INADDR_ANY;
  81.  
  82.     /*
  83.      * Get a stream socket.
  84.      */
  85.  
  86.      if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  87.  
  88.     {
  89.         psock_errno("Socket()");
  90.         exit(3);
  91.     }
  92.  
  93.     /*
  94.      * Connect to the server.
  95.      */
  96.    printf("connecting\n");
  97.     if ( (err=connect(s, (struct sockaddr *)&server, sizeof(server))) < 0)
  98.     {
  99.         printf("\nThe Socket Error Number is %d\n",sock_errno());
  100.         psock_errno("Err Msg thru psock_errno:");
  101.         exit(4);
  102.     }
  103.  
  104.     printf("Connected\n:ENTER int VARIAABLE:\n");
  105.     scanf("%d",&p);
  106.  
  107.           if (send(s, buf, 1024, 0) < 0)
  108.             {
  109.                 psock_errno("Send()");
  110.                 exit(5);
  111.             }
  112.             printf("sent\n");
  113.  
  114.  
  115.     /*
  116.      * Close the socket.
  117.      */
  118.     soclose(s);
  119.     printf("Client Ended Successfully\n");
  120.     exit(0);
  121.  
  122. }
  123.  
  124.