home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / accxrecv / accept.c next >
C/C++ Source or Header  |  1999-05-11  |  6KB  |  172 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. /************************** ACCEPT.C **************************************/
  24. /*   TCP Server   */
  25.  
  26. /* Include Files. */
  27. #define INCL_32
  28. #define INCL_DOS
  29. #include <os2.h>
  30. #include <stdio.h>
  31. #include <types.h>
  32. #include <sys\socket.h>
  33. #include <netinet\in.h>
  34. #include <utils.h>
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <sys\time.h>
  39.  
  40. void acceptex_thread();
  41.  
  42.  
  43. int sockfd;
  44. int total_threads = 0;
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.    int port;
  49.    int accexcnt=0, count;
  50.    int tid=0;
  51.    int rv;
  52.    struct sockaddr_in serv_addr;
  53.    char q;
  54.    struct timeval tv;
  55.  
  56.  
  57. if( argc < 3) {
  58.    printf("Usage: %s [server port] [number of acceptex threads]\n", argv[0]);
  59.    return -1;
  60. }
  61.  
  62.    port = (unsigned int) atoi(argv[1]);
  63.    accexcnt = atoi(argv[2]);
  64.  
  65.    if ( (sockfd = socket(AF_INET,SOCK_STREAM,0))<0)
  66.    {
  67.      printf("server : can't open stream socket");
  68.      exit(0);
  69.    }
  70.  
  71.    printf("Server: socket opened %d\n",sockfd);
  72.    memset((char *) &serv_addr,0, sizeof(serv_addr));
  73.    serv_addr.sin_family      = AF_INET ;
  74.    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  75.    serv_addr.sin_port        = htons(port);
  76.  
  77.  
  78.   if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)  {
  79.        printf("server : can't bind local address");
  80.        exit (-1);
  81.   }
  82.   printf("Server: socket bound %d\n",sockfd);
  83.  
  84.   tv.tv_sec = 10;
  85.   tv.tv_usec = 0;
  86.  
  87.   rv = setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(char *)&tv,sizeof(struct timeval));
  88.   if( rv < 0) psock_errno( " Set SO_RCVTIMEO");
  89.  
  90.  
  91.   listen(sockfd, 5);
  92.   printf("now listening\n");
  93.  
  94.   printf("Creating threads\n");
  95.   printf("ENTER THE VARIABLE Q \n");
  96.   scanf("%c",&q);
  97.  
  98.   for( count =0; count < accexcnt; count++)
  99.   {
  100.     if( (tid = _beginthread(acceptex_thread, NULL, 8192, NULL)) < 0)
  101.     {
  102.        printf("error\n");
  103.        exit(1);
  104.     }
  105.     printf("Acceptex Thread Id is: %x\n",tid);
  106.   }
  107.  
  108.  
  109.   while( total_threads < accexcnt )
  110.      DosSleep( 1 );
  111.  
  112.     soclose(sockfd);
  113.    return 1;
  114.  
  115. }
  116.  
  117. void acceptex_thread()
  118. {
  119.       long sock_accex, recvd = 0;
  120.       long locallen,clilen;
  121.       char *Outbuff;
  122.       int alloc = 1024;
  123.       struct sockaddr_in local;
  124.       struct sockaddr_in cli_addr;
  125.       locallen = sizeof(local);
  126.       clilen = sizeof(cli_addr);
  127.  
  128.       Outbuff = (char*) malloc( alloc );
  129.       if (Outbuff == NULL) {
  130.         printf("malloc error");
  131.         total_threads++;
  132.         exit(0);
  133.       }
  134. Begin:
  135.       sock_accex = -1;
  136.       recvd = accept_and_recv(sockfd,&sock_accex,(struct sockaddr *)&cli_addr, &clilen,(struct sockaddr *)&local,&locallen,Outbuff,alloc);
  137.       if(recvd < 0)
  138.         {
  139.            printf("error in acceptex recv= %d\n",sock_errno());
  140.            total_threads++;
  141.            goto Begin;
  142.            locallen = sizeof(local);
  143.            clilen = sizeof(cli_addr);
  144.            alloc =1024;
  145.            recvd = accept_and_recv(sockfd,&sock_accex,(struct sockaddr *)&cli_addr, &clilen,(struct sockaddr *)&local,&locallen,Outbuff,alloc);
  146.               if(recvd < 0)
  147.                 {
  148.                    printf("error in acceptex recv= %d\n",sock_errno());
  149.         }
  150.         else
  151.         {
  152.               locallen = sizeof(local);
  153.               clilen = sizeof(cli_addr);
  154.               alloc =1024;
  155.               recvd = accept_and_recv(sockfd,&sock_accex,(struct sockaddr *)&cli_addr, &clilen,(struct sockaddr *)&local,&locallen,Outbuff,alloc);
  156.               if(recvd < 0)
  157.                    printf("error in acceptex recv= %d\n",sock_errno());
  158.         }
  159. }
  160.  
  161.        printf(" Local addr :%x, Local_addr_len :%d\n",local.sin_addr.s_addr,locallen);
  162.        printf(" Bytes recieved with acceptex :%d\n",recvd);
  163.        printf(" Msg recvd with acceptex :%s\n",Outbuff);
  164.        printf(" Recv socket Id :%d\n",sock_accex);
  165.        printf("Before soclose\n");
  166.        soclose(sock_accex);
  167.        printf("after soclose\n");
  168.        total_threads++;
  169.        return ;
  170. }
  171.  
  172.