home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tcps.zip / tcptst-timer.c < prev    next >
C/C++ Source or Header  |  2001-05-24  |  4KB  |  153 lines

  1. #define INCL_DOS
  2. #define INCL_DOSPROCESS
  3. #define INCL_DOSERRORS
  4. #include <os2.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <types.h>
  9. #include <utils.h>
  10. #include <netinet\in.h>
  11. #include <sys\socket.h>
  12. #include <sys\time.h>
  13. #include <sys\select.h>
  14. #include <netdb.h>
  15. #include <nerrno.h>
  16.  
  17. int iSocket = 0;
  18.  
  19. //To get thread information.
  20. PTIB ptib = NULL;     // Thread information block structure.
  21. PPIB ppib = NULL;     // Process information block structre.
  22.  
  23. LONG InitService( int * piSocket,ushort usPort );
  24. BOOL Terminate( int * piSocket );
  25. VOID _System ServerAcceptConn( ULONG socket );
  26.  
  27. main( int argc, char* argv[] )
  28. {
  29.    APIRET rc = NO_ERROR ;// Return code.
  30.  
  31.    rc = DosGetInfoBlocks(&ptib, &ppib);
  32.    printf("Main Thread: Tid %ld\n",ptib->tib_ordinal);
  33.  
  34.     if( !InitService( &iSocket,17701 ) )
  35.     {
  36.         DosSleep( 3000 );
  37.         Terminate( &iSocket );
  38.         DosSleep( 3000 );
  39.     }
  40. }
  41.  
  42. //  InitService()
  43. LONG InitService( int* piSocket,ushort usPort )
  44. {
  45.     APIRET  rc;
  46.     TID     tid;
  47.     struct sockaddr_in  server;
  48.  
  49.     if((sock_init()) !=0)
  50.     {
  51.         return sock_errno();
  52.     }
  53.  
  54.     if(( *piSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  55.     {
  56.         return sock_errno();
  57.     }
  58.  
  59.     server.sin_family      = AF_INET;
  60.     server.sin_port        = htons( usPort );
  61.     server.sin_addr.s_addr = INADDR_ANY;
  62.     if( bind(*piSocket,(struct sockaddr *)&server, sizeof(server)) < 0)
  63.     {
  64.         rc = sock_errno();
  65.         soclose( *piSocket );
  66.         *piSocket = 0;
  67.         return rc;
  68.     }
  69.  
  70.    rc = DosGetInfoBlocks(&ptib, &ppib);
  71.     printf("Main Thread: InitService Thread: Tid %ld\n",ptib->tib_ordinal);
  72.  
  73.     if( listen( *piSocket,10 ) != 0)
  74.     {
  75.         rc = sock_errno();
  76.         soclose( *piSocket );
  77.         *piSocket = 0;
  78.         return rc;
  79.     }
  80.  
  81.     if ( NO_ERROR != ( rc = DosCreateThread( &tid,
  82.                                              (PFNTHREAD)ServerAcceptConn,
  83.                                              *piSocket,
  84.                                              0,
  85.                                              4 * 4096) ) )
  86.     {
  87.         soclose( *piSocket );
  88.         *piSocket = 0;
  89.     }
  90.     return rc ;
  91. }
  92.  
  93. //  Terminate()
  94. BOOL Terminate( int* piSocket )
  95. {
  96.    APIRET rc = NO_ERROR ;// Return code.
  97.    rc = DosGetInfoBlocks(&ptib, &ppib);
  98.  
  99.     if( *piSocket )
  100.     {
  101.         printf( "Terminate: Socket %d will be closed.Thread id %ld \n",*piSocket,ptib->tib_ordinal);
  102.         soclose( *piSocket );
  103.         printf( "Terminate: Socket %d closed.Thread id %ld \n",*piSocket,ptib->tib_ordinal);
  104.         *piSocket = 0;
  105.     }
  106.     return TRUE;
  107. }
  108.  
  109. // ServerAcceptConn
  110. VOID _System ServerAcceptConn(ULONG socket)
  111. {
  112.     int iNameLen;
  113.     struct sockaddr_in client;
  114.     int ns = 0,i;
  115.     APIRET rc;
  116.  
  117.     rc = DosGetInfoBlocks(&ptib, &ppib);
  118.     printf("ServerAcceptConn: Thread id - %ld \n",ptib->tib_ordinal);
  119.  
  120.     for(i=0;i<2;i++) /* I am going to do accept only twice.Earlier it was
  121.                       * for done indefintely in code supplied by customer.
  122.                       */
  123.     {
  124.         iNameLen = sizeof(client);
  125.         /* Ensuring SoClose happens before accept call wait for 5secs */
  126.         /* Tcptst-timerset.exe has DosSleep enabled                  */
  127.         /* Tcptst-timernotset.exe has DosSleep commented(disabled)   */
  128.         DosSleep(5000);
  129.  
  130.         ns = accept((int)socket, (struct sockaddr *)&client, &iNameLen);
  131.         if( -1 == ns )
  132.         {
  133.             ns = sock_errno();
  134.  
  135.             printf( "ServerAcceptConn: Accept returns %d.\n", ns);
  136.             // check if socket closed
  137.             //-----------------------
  138.             if( ns == ENOTSOCK )
  139.             {
  140.                 break;
  141.             }
  142.             else
  143.             {
  144.             }
  145.         }
  146.         else
  147.         {
  148.             printf("Accept succeded \n");
  149.         }
  150.     } // end for
  151.     printf( "ServerAcceptConn: Thread for acept connetctions terminated.\n");
  152. }
  153.