home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / Chapter7 / 7-4 / server.c < prev   
Encoding:
C/C++ Source or Header  |  2000-03-08  |  1.2 KB  |  59 lines

  1.  
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. #define PIPESIZE 512
  6. #define PIPE    "\\\\.\\pipe\\mypipe"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     HANDLE hPipe;
  11.     DWORD dwWritten, dwRead, dwLastError, dwCount;
  12.     char sBuffer[PIPESIZE];
  13.     BOOL fConnected;
  14.  
  15.     for(dwCount = 0; ; ++dwCount)
  16.     {
  17.         if((hPipe = CreateNamedPipe(PIPE,
  18.               PIPE_ACCESS_DUPLEX,
  19.               PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE |
  20.               PIPE_WAIT,
  21.               PIPE_UNLIMITED_INSTANCES,
  22.               PIPESIZE,
  23.               PIPESIZE,
  24.               0,
  25.               NULL)) == INVALID_HANDLE_VALUE)
  26.         {
  27.             printf("CreateNamedPipe failed!\n");
  28.             ExitProcess(1);
  29.         }
  30.         else
  31.         {
  32.             printf("CreateNamedPipe scceeded! Handle = %08lx\n", hPipe);
  33.         }
  34.  
  35.         fConnected = ConnectNamedPipe(hPipe, NULL) ?
  36.           TRUE : ((dwLastError = GetLastError()) == ERROR_PIPE_CONNECTED);
  37.  
  38.         if(dwLastError == ERROR_CALL_NOT_IMPLEMENTED)
  39.         {
  40.             printf("Are you using Windows 9X?\nNo named pipes here!\n");
  41.             ExitProcess(1);
  42.         }
  43.  
  44.         if(fConnected)
  45.         {
  46.             wsprintf(sBuffer, "Hello #%ld", dwCount);
  47.             printf("Pipe connected #%d.\n", dwCount);
  48.             WriteFile(hPipe, sBuffer, lstrlen(sBuffer), &dwWritten, NULL);
  49.             printf("Iteration #%d: %ld Bytes written\n", dwCount, dwWritten);
  50.             FlushFileBuffers(hPipe);
  51.             DisconnectNamedPipe(hPipe);
  52.             CloseHandle(hPipe);
  53.         }
  54.     }
  55.  
  56. }
  57.  
  58.  
  59.