home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / r0lib32 / client.c next >
C/C++ Source or Header  |  1999-05-11  |  5KB  |  151 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. /************************** CLIENT.C ****************************************/
  24. #include <os2.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. #include <sys\types.h>
  30. #include <netinet\in.h>
  31. #include <sys\socket.h>
  32. #include <sys\ioctl.h>
  33.  
  34. #include <errno.h>
  35. #include <utils.h>
  36.  
  37. # define BUFF_SIZE      1024
  38.  
  39. void mysendmsg();
  40. void myrecvmsg();
  41.  
  42. struct sockaddr_in serv_addr ;
  43. int sockfd;
  44.  
  45.  
  46. main(int argc, char *argv[])
  47. {
  48.         char Sendbuff[BUFF_SIZE];
  49.         char Recvbuff[BUFF_SIZE];
  50.         int count,port;
  51.  
  52.         if (argc != 2)
  53.         {
  54.                 fprintf(stderr, "Usage: %s ip address \n", argv[0]);
  55.                 exit(1);
  56.         }
  57.         for(count= 0;count < BUFF_SIZE;count++)
  58.                 Sendbuff[count] = 'a';
  59.  
  60.         if ( (sockfd = socket(AF_INET,SOCK_STREAM,0))<0)
  61.         {
  62.                 printf("server : can't open stream socket");
  63.                 exit(0);
  64.         }
  65.  
  66.         memset((char *) &serv_addr,'\0', sizeof(serv_addr));
  67.  
  68.         serv_addr.sin_family      = AF_INET ;
  69.         serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  70.         serv_addr.sin_port        = htons(5000);
  71.  
  72.         if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
  73.         {
  74.                 psock_errno("connect()");
  75.                 exit(1);
  76.         }
  77.  
  78.         if( (send(sockfd,Sendbuff,BUFF_SIZE,0)  < 0 )  )
  79.         {
  80.                 psock_errno("send()");
  81.                 exit(1);
  82.         }
  83.  
  84.         if( recv(sockfd,Recvbuff,BUFF_SIZE,0) < 0 )
  85.         {
  86.                 psock_errno("recv()");
  87.                 exit(1);
  88.         }
  89.         for(count= 0;count < BUFF_SIZE;count++)
  90.                 printf("%c",Recvbuff[count]);
  91.  
  92.         mysendmsg();
  93.         myrecvmsg();
  94.  
  95. }
  96.  
  97. void myrecvmsg()
  98. {
  99.         struct msghdr msg;
  100.         struct iovec iov;
  101.         char buff[50];
  102.         int i;
  103.  
  104.         iov.iov_base = &buff[0];
  105.         iov.iov_len = sizeof(buff) -1;
  106.  
  107.         msg.msg_name = NULL;
  108.         msg.msg_namelen = 0;
  109.  
  110.         msg.msg_iov = &iov;
  111.         msg.msg_iovlen = 1;
  112.  
  113.         if( recvmsg(sockfd,(struct msghdr *)&msg,0) < 0)
  114.         {
  115.                 printf("\n recv msg error \n");
  116.                 psock_errno("recvmsg()");
  117.                 exit(1);
  118.         }
  119.         printf("\n data received by recvmsg \n");
  120.         printf("%s",buff);
  121.  
  122. }
  123. void mysendmsg()
  124. {
  125.         struct msghdr msg;
  126.         struct iovec iov;
  127.         char buff[50] = "SHIVA";
  128.  
  129.         iov.iov_base = buff;
  130.         iov.iov_len = sizeof(buff) -1;
  131.  
  132.         /******* if connect is used *********/
  133.         msg.msg_name = NULL;
  134.         msg.msg_namelen = 0;
  135.  
  136.         /****************************************
  137.          if connect is not used
  138.         msg.msg_name = (caddr_t)&server;
  139.         msg.msg_namelen = sizeof(server);
  140.         *****************************************/
  141.  
  142.         msg.msg_iov = &iov;
  143.         msg.msg_iovlen = 1;
  144.         if( sendmsg(sockfd,&msg,0) < 0)
  145.         {
  146.                 printf("\n sendmsg error \n");
  147.                 exit(1);
  148.         }
  149.  
  150. }
  151.