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.

170 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. tnpcli.c
  5. Abstract:
  6. This program makes simple client calls to tnpsrv.c
  7. Author:
  8. Colin Watson (ColinW) 19-March-1991
  9. Revision History:
  10. --*/
  11. #include <assert.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <windows.h>
  15. CHAR* WriteData1 = "Hello Mars\n";
  16. CHAR* WriteData2 = "Hello Jupiter\n";
  17. int
  18. main(
  19. int argc,
  20. char *argv[],
  21. char *envp[]
  22. )
  23. {
  24. HANDLE C1;
  25. DWORD Size;
  26. DWORD Dummy;
  27. DWORD Count;
  28. CHAR Data[1024];
  29. OVERLAPPED Overlapped;
  30. printf("Create client...\n");
  31. C1 = CreateFile("\\\\.\\Pipe\\cw\\testpipe",
  32. GENERIC_READ | GENERIC_WRITE,
  33. FILE_SHARE_READ | FILE_SHARE_WRITE,
  34. NULL, // Security Attributes
  35. OPEN_EXISTING,
  36. FILE_ATTRIBUTE_NORMAL,
  37. NULL
  38. );
  39. for ( Count = 1; Count < 10; Count++ ) {
  40. printf("Client Writing...\n");
  41. if ( FALSE == WriteFile(C1, WriteData1, strlen(WriteData1)+1, &Dummy, NULL) ) {
  42. printf("Client WriteFile returned Error %lx\n", GetLastError() );
  43. }
  44. while (1) {
  45. printf("Client now Peeking...\n");
  46. if ( FALSE == PeekNamedPipe(C1,Data, sizeof(Data), &Size, NULL, NULL) ) {
  47. printf("Client PeekNamedPipe returned Error %lx\n", GetLastError() );
  48. break;
  49. }
  50. if ( Size ) {
  51. printf("Client PeekNamedPipe Done %lx: %s\n", Size, Data);
  52. break;
  53. }
  54. else {
  55. printf("Client PeekNamedPipe Done, no data yet, sleep 1 sec\n");
  56. Sleep(1000);
  57. }
  58. }
  59. printf("Client now Reading...\n");
  60. if ( FALSE == ReadFile(C1,Data, sizeof(Data), &Size, NULL) ) {
  61. printf("Client ReadFile returned Error %lx\n", GetLastError() );
  62. }
  63. printf("Client Reading Done %lx: %s\n", Size, Data);
  64. }
  65. {
  66. DWORD Flags;
  67. DWORD OutBufferSize;
  68. DWORD InBufferSize;
  69. DWORD MaxInstances;
  70. printf("Client call GetNamedPipeInfo....\n");
  71. if ( FALSE == GetNamedPipeInfo(C1, &Flags, &OutBufferSize, &InBufferSize, &MaxInstances) ){
  72. printf("Client GetNamedPipeInfo returned Error %lx\n", GetLastError() );
  73. }
  74. else {
  75. printf("Flags %lx, OutBufferSize %lx, InBufferSize %lx, MaxInstances %lx\n",
  76. Flags, OutBufferSize, InBufferSize, MaxInstances);
  77. }
  78. }
  79. {
  80. DWORD State;
  81. DWORD CurInstances;
  82. printf("Client call GetNamedPipeHandleState....\n");
  83. if ( FALSE == GetNamedPipeHandleState(C1, &State, &CurInstances, NULL, NULL, NULL, 0) ){
  84. printf("Client GetNamedPipeHandleState returned Error %lx\n", GetLastError() );
  85. }
  86. else {
  87. printf("State %lx, CurInstances %lx\n", State, CurInstances );
  88. }
  89. }
  90. printf("Client attempting 10 second WaitNamedPipe- should timeout...\n");
  91. if ( FALSE == WaitNamedPipe("\\\\.\\Pipe\\cw\\testpipe", 10000, NULL) ) {
  92. printf("Client WaitNamedPipe returned Error %lx\n", GetLastError() );
  93. }
  94. printf("WaitNamedPipe complete\n");
  95. printf("Client attempting 10 second WaitNamedPipe- should timeout...\n");
  96. Overlapped.hEvent = CreateEvent(NULL,TRUE,TRUE);
  97. if ( FALSE == WaitNamedPipe("\\\\.\\Pipe\\cw\\testpipe", 10000, &Overlapped) ) {
  98. printf("Client WaitNamedPipe returned Error %lx\n", GetLastError() );
  99. if ( GetLastError() == ERROR_IO_PENDING ) {
  100. printf("Server got ERROR_IO_PENDING ok,Waiting for pipe\n");
  101. assert( FALSE == GetOverlappedResult(NULL , &Overlapped, &Dummy, TRUE ));
  102. printf("Client WaitNamedPipe returned Error Ok %lx\n", GetLastError() );
  103. }
  104. }
  105. printf("WaitNamedPipe complete\nClient closing...\n");
  106. CloseHandle(C1);
  107. printf("Client closed\n");
  108. printf("Client attempting 10 second WaitNamedPipe- should work...\n");
  109. if ( FALSE == WaitNamedPipe("\\\\.\\Pipe\\cw\\testpipe", 10000, NULL) ) {
  110. printf("Client WaitNamedPipe returned Error %lx\n", GetLastError() );
  111. }
  112. printf("WaitNamedPipeComplete\n");
  113. for ( Count = 1; Count < 5; Count++ ) {
  114. printf("Client CallNamedPipe...\n");
  115. if ( FALSE == CallNamedPipe(
  116. "\\\\.\\Pipe\\cw\\testpipe",
  117. WriteData2,
  118. strlen(WriteData2)+1,
  119. Data,
  120. sizeof(Data),
  121. &Size,
  122. 10000 // Ten second timeout
  123. ) ) {
  124. printf("CallNamedPipe returned Error %lx\n", GetLastError() );
  125. }
  126. printf("Client CallNamedPipe Done %lx: %s\n", Size, Data);
  127. }
  128. printf("CallNamedPipe complete\n");
  129. }