home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / SERVER.ZIP / CLIENT.C < prev    next >
C/C++ Source or Header  |  1993-03-08  |  4KB  |  91 lines

  1. /*
  2.  * Program : CLIENT.C
  3.  *
  4.  * Purpose : This is the CLIENT portion of the named pipes demonstration
  5.  *   program.  This program is started by the SERVER process and is passed
  6.  *   the name of the pipe to read.  It then reads from the pipe information
  7.  *   about a file that will be sent by the SERVER process.  This data is
  8.  *   received and statistics are displayed.  The program then ends itself.
  9.  *
  10.  *
  11.  * NOTICE : This program is placed into the Public Domain.  This program is
  12.  *   intended ONLY as a demonstration of using Named Pipes between OS/2 and
  13.  *   DOS VDM's.  Use this program at your own risk.  There are absolutely
  14.  *   no warranties on this program.
  15.  *
  16.  */
  17.  
  18. #include <stdlib.h>
  19. #include <malloc.h>
  20. #include <time.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <sys\types.h>
  24. #include <sys\stat.h>
  25.  
  26. #define USHORT       unsigned short
  27. #define ULONG        unsigned long
  28. #define CHAR         char
  29. #define BYTE         unsigned char
  30.  
  31. main ( int argc, char **argv )
  32.  
  33. { int     hfile;
  34.   CHAR    PipeName [ 256 ], HandShake[256];
  35.   CHAR    Buffer [ 8192 ], FName [ 256 ];
  36.   ULONG   ulAction, BytesToRead, TotBytes, ulFileSize, ulFNameLen;
  37.   ULONG   OpenFlag, BytesRead, BytesWritten;
  38.   USHORT  rc;
  39.   double  beg_time, end_time;
  40.  
  41.   printf ( "CLIENT: Client Process has now started!\n" );
  42.   if ( argc != 2 ) {
  43.     printf ( "CLIENT ERROR: Invalid number of arguements. Pipe Name must be\n" );
  44.     printf ( "              specified on the commnad line.\n" );
  45.     exit(-1);
  46.   } /* endif */
  47.  
  48.   strcpy ( PipeName, *++argv );
  49.   printf ( "CLIENT: Wait for the Instance of the pipe %s \n", PipeName );
  50.  
  51.   hfile = open ( PipeName, O_RDWR | O_BINARY, S_IREAD | S_IWRITE );
  52.   if ( hfile <= 0 ) {
  53.     printf ( "CLIENT: Error opening the Pipe in Client \n" );
  54.     printf ( "CLIENT: rc = %x \n", rc );
  55.     exit ( -1 );
  56.   } /* endif */
  57.  
  58.   write ( hfile, "READY", 6 );
  59.   printf ( "CLIENT: Sent handshake to the Server! \n" );
  60.   read ( hfile, &ulFNameLen, sizeof(ULONG));
  61.   printf ( "CLIENT: Length of the File Name = %d \n", ulFNameLen );
  62.   read ( hfile, FName, (USHORT) ulFNameLen );
  63.   printf ( "CLIENT: File to transfer is %s \n", FName );
  64.   read ( hfile, &ulFileSize, sizeof(ULONG));
  65.   printf ( "CLIENT: Size of file to transfer in bytes is %ld \n", ulFileSize );
  66.  
  67. /********************************************************************************/
  68. /* Loop and read the data in 4K blocks until there will be less than 4K         */
  69. /* left to receive and then read the appropriate size block.  We are            */
  70. /* reading 4K blocks because the buffer has been set to 4K.  This can be        */
  71. /* changed to a larger size in the DosCreateNPipe() call in the SERVER process. */
  72. /********************************************************************************/
  73.   BytesToRead = 8192;
  74.   TotBytes = ulFileSize;
  75.   beg_time = (double) clock ();
  76.   while ( BytesToRead <= TotBytes ) {
  77.     read ( hfile, Buffer, (USHORT) BytesToRead );
  78.     TotBytes -= BytesToRead;
  79.   } /* end while */
  80.   if ( TotBytes > 0 )
  81.     read ( hfile, Buffer, (USHORT) TotBytes );
  82.   end_time = (double) clock ();
  83.  
  84.   printf ( "CLIENT: Done! %ld Bytes were transferred by the pipe \n", ulFileSize );
  85.   printf ( "CLIENT: Transfer took %2.4f seconds\n",
  86.            ( end_time - beg_time ) / (double) CLOCKS_PER_SEC );
  87.   printf ( "CLIENT: Transfer speed was %10.4f bytes/second\n",
  88.            (double) ulFileSize / ((end_time-beg_time) / (double) CLOCKS_PER_SEC));
  89.   exit(0);
  90. } /* end of CLIENT() */
  91.