Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.8 KiB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. DWORD Size;
  6. LPSTR WriteData = "Hello World\n";
  7. VOID
  8. ClientThread(
  9. LPVOID ThreadParameter
  10. )
  11. {
  12. DWORD n;
  13. HANDLE C1;
  14. LPSTR l;
  15. printf("Create client...\n");
  16. C1 = CreateFile("\\\\.\\Pipe\\cw\\testpipe",
  17. GENERIC_READ | GENERIC_WRITE,
  18. FILE_SHARE_READ | FILE_SHARE_WRITE,
  19. NULL, // Security Attributes
  20. OPEN_EXISTING,
  21. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  22. NULL
  23. );
  24. if ( C1 == INVALID_HANDLE_VALUE ) {
  25. DWORD Status;
  26. printf("CreateFile returned Error %lx\n", Status = GetLastError() );
  27. }
  28. printf("Client writing...\n");
  29. assert( TRUE == WriteFile(C1,WriteData,Size, &n, NULL) );
  30. assert( n==Size );
  31. printf("Client done Writing...\n");
  32. // Get WriteData back from the server
  33. l = LocalAlloc(LMEM_ZEROINIT,Size);
  34. assert(l);
  35. printf("Client reading\n");
  36. assert( TRUE == ReadFile(C1,l,Size, &n, NULL));
  37. printf("Client reading Done %s\n",l);
  38. Sleep(10000);
  39. printf("Client done Sleeping...\n");
  40. }
  41. int
  42. main(
  43. int argc,
  44. char *argv[],
  45. char *envp[]
  46. )
  47. {
  48. DWORD n;
  49. LPSTR l;
  50. HANDLE Thread;
  51. HANDLE S1,S2,S3; // Server handles
  52. DWORD ThreadId;
  53. DWORD Dummy;
  54. OVERLAPPED S1Overlapped;
  55. S1 = CreateNamedPipe("\\\\.\\Pipe\\cw\\testpipe",
  56. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  57. PIPE_WAIT | PIPE_READMODE_MESSAGE| PIPE_TYPE_MESSAGE,
  58. 2,
  59. 1024,
  60. 1024,
  61. 0,
  62. NULL);
  63. assert(S1 != INVALID_HANDLE_VALUE);
  64. S2 = CreateNamedPipe("\\\\.\\Pipe\\cw\\testpipe",
  65. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  66. PIPE_WAIT | PIPE_READMODE_MESSAGE| PIPE_TYPE_MESSAGE,
  67. 992, //IGNORED
  68. 0, //IGNORED
  69. 0, //IGNORED
  70. 0, //IGNORED
  71. NULL);
  72. assert(S2 != INVALID_HANDLE_VALUE);
  73. S3 = CreateNamedPipe("\\\\.\\Pipe\\cw\\testpipe",
  74. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  75. PIPE_WAIT | PIPE_READMODE_MESSAGE| PIPE_TYPE_MESSAGE,
  76. 992,
  77. 0,
  78. 0,
  79. 0,
  80. NULL);
  81. assert( S3 == INVALID_HANDLE_VALUE ); //Should fail - Used all instances
  82. S1Overlapped.hEvent = NULL;
  83. assert( FALSE == ConnectNamedPipe( S1, &S1Overlapped ));
  84. assert( ERROR_IO_PENDING == GetLastError());
  85. assert( FALSE == GetOverlappedResult( S1, &S1Overlapped, &Dummy, FALSE ));
  86. assert( ERROR_IO_INCOMPLETE == GetLastError());
  87. Size = strlen(WriteData)+1;
  88. l = LocalAlloc(LMEM_ZEROINIT,Size);
  89. assert(l);
  90. Thread = CreateThread(NULL,0L,ClientThread,(LPVOID)99,0,&ThreadId);
  91. assert(Thread);
  92. printf("Waiting for connection\n");
  93. assert( TRUE == GetOverlappedResult( S1, &S1Overlapped, &Dummy, TRUE ));
  94. printf("Connected, Server now Reading\n");
  95. if ( FALSE == ReadFile(S1,l,Size, &n, &S1Overlapped) ) {
  96. DWORD Status;
  97. printf("Server ReadFile returned Error %lx\n", Status = GetLastError() );
  98. if ( Status == ERROR_IO_PENDING ) {
  99. printf("Server got ERROR_IO_PENDING ok,Waiting for data\n");
  100. assert( TRUE == GetOverlappedResult( S1, &S1Overlapped, &Dummy, TRUE ));
  101. }
  102. }
  103. printf("Server Reading Done %s\n",l);
  104. printf("Server Writing\n");
  105. if ( FALSE == WriteFile(S1,l,Size, &n, &S1Overlapped) ) {
  106. DWORD Status;
  107. printf("Server WriteFile returned Error %lx\n", Status = GetLastError() );
  108. if ( Status == ERROR_IO_PENDING ) {
  109. printf("Server got ERROR_IO_PENDING ok,Waiting for transfer complete\n");
  110. assert( TRUE == GetOverlappedResult( S1, &S1Overlapped, &Dummy, TRUE ));
  111. }
  112. }
  113. printf("Server Writing Done %s\n",l);
  114. }