Guest User

NP Client Test

a guest
Dec 11th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. // SONPClient.cpp.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include <cassert>
  7.  
  8. #define NAMED_PIPE L"\\\\.\\pipe\\stackoverflow"
  9.  
  10. int main()
  11. {
  12.     HANDLE hPipe = CreateFile(
  13.         NAMED_PIPE,
  14.         GENERIC_READ | GENERIC_WRITE,
  15.         0,
  16.         NULL,
  17.         OPEN_EXISTING,
  18.         0,
  19.         NULL);
  20.  
  21.     assert(hPipe != INVALID_HANDLE_VALUE);
  22.  
  23.     unsigned total_read = 0;
  24.  
  25.     const unsigned buffer_size = 8092; //2x the pipe buffer size. half of this buffer will be unused
  26.     char * buf = new char[buffer_size];
  27.     DWORD bytes_read;
  28.     while (ReadFile(hPipe, buf, buffer_size, &bytes_read, NULL))
  29.     {
  30.         //I'm actually getting reads of 8092 bytes despite the server pipe buffer size being 4096.
  31.         //assert(bytes_read <= 4096);
  32.        
  33.         total_read += bytes_read;
  34.  
  35.         for (unsigned ix = 0; ix < bytes_read; ix++)
  36.         {
  37.             assert(buf[ix] == 'a'); //just to prove something real comes over the pipes
  38.         }
  39.  
  40.         memset(buf, 0, buffer_size);
  41.     }
  42.  
  43.     assert(total_read == 80 * 1024); //all data is received.
  44.  
  45.     CloseHandle(hPipe);
  46.  
  47.     delete[] buf;
  48.  
  49.     return 0;
  50. }
Add Comment
Please, Sign In to add comment