Guest User

NP Server Test

a guest
Dec 11th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. // SONPServer.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. #define READ_BUFFER_SIZE 4096
  11. #define WRITE_BUFFER_SIZE 4096
  12.  
  13. int main()
  14. {
  15.     HANDLE pipe_handle = CreateNamedPipeW(
  16.         NAMED_PIPE,
  17.         PIPE_ACCESS_DUPLEX,
  18.         PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
  19.         PIPE_UNLIMITED_INSTANCES,
  20.         WRITE_BUFFER_SIZE,
  21.         READ_BUFFER_SIZE,
  22.         NMPWAIT_USE_DEFAULT_WAIT,
  23.         NULL);
  24.  
  25.     BOOL connected = ConnectNamedPipe(pipe_handle, NULL);
  26.  
  27.     assert(connected);
  28.  
  29.     const unsigned buffer_size = 80 * 1024;
  30.     char * buf = new char[buffer_size];
  31.  
  32.     assert(buf);
  33.  
  34.     memset(buf, 'a', buffer_size);
  35.  
  36.     DWORD bytes_written;
  37.  
  38.     //write 80 KB
  39.     BOOL successful_write = WriteFile(pipe_handle, buf, buffer_size, &bytes_written, NULL);
  40.  
  41.     assert(successful_write);
  42.     assert(bytes_written == buffer_size); //80 KB were written with one WriteFile call
  43.  
  44.     //to force all the data over the pipe before we close the handle
  45.     FlushFileBuffers(pipe_handle);
  46.  
  47.     DisconnectNamedPipe(pipe_handle);
  48.     CloseHandle(pipe_handle);
  49.  
  50.     delete[] buf;
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment