This C++ program is a simple named pipe client that demonstrates the API calls needed to successfully develop a basic named pipe client application. When this application successfully connects to a named pipe, the message “This is a test” is written to the server.
There are four basic steps needed to implement a client:
- Wait for a Named Pipe instance to become available using the WaitNamedPipe() API function.
- Connect to the Named Pipe using the CreateFile() API function.
- Send data to or receive data from the server using the WriteFile() and ReadFile() API functions.
- Close the Named Pipe session using the CloseHandle() API functions.
Compile
cl -o Client Client.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | #include <windows.h> #include <stdio.h> #define PIPE_NAME "\\\\.\\Pipe\\Jim" void main(void) { HANDLE PipeHandle; DWORD BytesWritten; if (WaitNamedPipe(PIPE_NAME, NMPWAIT_WAIT_FOREVER) == 0) { printf("WaitNamedPipe failed with error %d\n", GetLastError()); return; } // Create the named pipe file handle if ((PipeHandle = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL)) == INVALID_HANDLE_VALUE) { printf("CreateFile failed with error %d\n", GetLastError()); return; } if (WriteFile(PipeHandle, "This is a test", 14, & BytesWritten, NULL) == 0) { printf("WriteFile failed with error %d\n", GetLastError()); CloseHandle(PipeHandle); return; } printf("Wrote %d bytes", BytesWritten); CloseHandle(PipeHandle); } // // End of client // /*****************************************/ //-----------------------------------------------------------// //OverlappedServer // Module Name: Overlap.cpp // // Purpose: // This sample demonstrates how to develop an advanced named // pipe server that is capable of servicing 5 named pipe // instances. The application is an echo server where data is // received from a client and echoed back to the client. All // the pipe instances are serviced in the main application // thread using Win32 overlapped I/O. // // Compile: // cl -o overlap overlap.cpp // // Command Line Options: // None // #include <windows.h> #include <stdio.h> #define NUM_PIPES 5 #define BUFFER_SIZE 256 void main(void) { HANDLE PipeHandles[NUM_PIPES]; DWORD BytesTransferred; CHAR Buffer[NUM_PIPES][BUFFER_SIZE]; INT i; OVERLAPPED Ovlap[NUM_PIPES]; HANDLE Event[NUM_PIPES]; // For each pipe handle instance, the code must maintain the // pipes' current state, which determines if a ReadFile or // WriteFile is posted on the named pipe. This is done using // the DataRead variable array. By knowing each pipe's // current state, the code can determine what the next I/O // operation should be. BOOL DataRead[NUM_PIPES]; DWORD Ret; DWORD Pipe; for (i = 0; i < NUM_PIPES; i++) { // Create a named pipe instance if ((PipeHandles[i] = CreateNamedPipe("\\\\.\\PIPE\\jim", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, NUM_PIPES, 0, 0, 1000, NULL)) == INVALID_HANDLE_VALUE) { printf("CreateNamedPipe for pipe %d failed " "with error %d\n", i, GetLastError()); return; } // Create an event handle for each pipe instance. This // will be used to monitor overlapped I/O activity on // each pipe. if ((Event[i] = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) { printf("CreateEvent for pipe %d failed with error %d\n", i, GetLastError()); continue; } // Maintain a state flag for each pipe to determine when data // is to be read from or written to the pipe DataRead[i] = FALSE; ZeroMemory( & Ovlap[i], sizeof(OVERLAPPED)); Ovlap[i].hEvent = Event[i]; // Listen for client connections using ConnectNamedPipe() if (ConnectNamedPipe(PipeHandles[i], & Ovlap[i]) == 0) { if (GetLastError() != ERROR_IO_PENDING) { printf("ConnectNamedPipe for pipe %d failed with", " error %d\n", i, GetLastError()); CloseHandle(PipeHandles[i]); return; } } } printf("Server is now running\n"); // Read and echo data back to Named Pipe clients forever while (1) { if ((Ret = WaitForMultipleObjects(NUM_PIPES, Event, FALSE, INFINITE)) == WAIT_FAILED) { printf("WaitForMultipleObjects failed with error %d\n", GetLastError()); return; } Pipe = Ret - WAIT_OBJECT_0; ResetEvent(Event[Pipe]); // Check overlapped results, and if they fail, reestablish // communication for a new client; otherwise, process read // and write operations with the client if (GetOverlappedResult(PipeHandles[Pipe], & Ovlap[Pipe], & BytesTransferred, TRUE) == 0) { printf("GetOverlapped result failed %d start over\n", GetLastError()); if (DisconnectNamedPipe(PipeHandles[Pipe]) == 0) { printf("DisconnectNamedPipe failed with error %d\n", GetLastError()); return; } if (ConnectNamedPipe(PipeHandles[Pipe], & Ovlap[Pipe]) == 0) { if (GetLastError() != ERROR_IO_PENDING) { // Severe error on pipe. Close this // handle forever. printf("ConnectNamedPipe for pipe %d failed with" "error %d\n", i, GetLastError()); CloseHandle(PipeHandles[Pipe]); } } DataRead[Pipe] = FALSE; } else { // Check the state of the pipe. If DataRead equals // FALSE, post a read on the pipe for incoming data. // If DataRead equals TRUE, then prepare to echo data // back to the client. if (DataRead[Pipe] == FALSE) { // Prepare to read data from a client by posting a // ReadFile operation ZeroMemory( & Ovlap[Pipe], sizeof(OVERLAPPED)); Ovlap[Pipe].hEvent = Event[Pipe]; if (ReadFile(PipeHandles[Pipe], Buffer[Pipe], BUFFER_SIZE, NULL, & Ovlap[Pipe]) == 0) { if (GetLastError() != ERROR_IO_PENDING) { printf("ReadFile failed with error %d\n", GetLastError()); } } DataRead[Pipe] = TRUE; } else { // Write received data back to the client by // posting a WriteFile operation. printf("Received %d bytes, echo bytes back\n", BytesTransferred); ZeroMemory( & Ovlap[Pipe], sizeof(OVERLAPPED)); Ovlap[Pipe].hEvent = Event[Pipe]; if (WriteFile(PipeHandles[Pipe], Buffer[Pipe], BytesTransferred, NULL, & Ovlap[Pipe]) == 0) { if (GetLastError() != ERROR_IO_PENDING) { printf("WriteFile failed with error %d\n", GetLastError()); } } DataRead[Pipe] = FALSE; } } } } // // End of OverlappedServer // /*************************/ //--------------------------------------------------------------------------// // Module Name: Server.cpp // // Purpose: // // This program is a simple named pipe server that demonstrates // the API calls needed to successfully develop a basic named // pipe server application. When this application receives a // client connection, it reads the data from the pipe and // reports the received message. // // You need five basic steps to write a named pipe server: // // 1. Create a named pipe instance handle using the // CreateNamedPipe() API function. // 2. Listen for a client connection on a pipe instance using // the ConnectNamedPipe() API function. // 3. Receive from and send data to the client using the // ReadFile() and WriteFile() API functions. // 4. Close down the named pipe connection using the // DisconnectNamedPipe() API function. // 5. Close the named pipe instance handle using the // CloseHandle() API function. // // Compile: // cl -o Server Server.cpp // // Command Line Options: // None // #include <windows.h> #include <stdio.h> void main(void) { HANDLE PipeHandle; DWORD BytesRead; CHAR buffer[256]; if ((PipeHandle = CreateNamedPipe("\\\\.\\Pipe\\Jim", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 0, 0, 1000, NULL)) == INVALID_HANDLE_VALUE) { printf("CreateNamedPipe failed with error %d\n", GetLastError()); return; } printf("Server is now running\n"); if (ConnectNamedPipe(PipeHandle, NULL) == 0) { printf("ConnectNamedPipe failed with error %d\n", GetLastError()); CloseHandle(PipeHandle); return; } if (ReadFile(PipeHandle, buffer, sizeof(buffer), & BytesRead, NULL) <= 0) { printf("ReadFile failed with error %d\n", GetLastError()); CloseHandle(PipeHandle); return; } printf("%.*s\n", BytesRead, buffer); if (DisconnectNamedPipe(PipeHandle) == 0) { printf("DisconnectNamedPipe failed with error %d\n", GetLastError()); return; } CloseHandle(PipeHandle); } // // End of Server // /*********************/ //------------------------------------------------------------------------------// // // Thread Server // // Module Name: Threads.cpp // // Purpose: // This sample demonstrates how to develop an advanced named // pipe server that is capable of servicing 5 named pipe // instances. The application is an echo server where data is // received from a client and echoed back to the client. All // the pipe instances are serviced using threads. // // Compile: // cl -o Threads Threads.cpp // // Command Line Options: // None // #include <windows.h> #include <stdio.h> #include <conio.h> #define NUM_PIPES 5 DWORD WINAPI PipeInstanceProc(LPVOID lpParameter); void main(void) { HANDLE ThreadHandle; INT i; DWORD ThreadId; for (i = 0; i < NUM_PIPES; i++) { // Create a thread to serve each pipe instance if ((ThreadHandle = CreateThread(NULL, 0, PipeInstanceProc, NULL, 0, & ThreadId)) == NULL) { printf("CreateThread failed with error %\n", GetLastError()); return; } CloseHandle(ThreadHandle); } printf("Press a key to stop the server\n"); _getch(); } // // Function: PipeInstanceProc // // Description: // This function handles the communication details of a single // named pipe instance. DWORD WINAPI PipeInstanceProc(LPVOID lpParameter) { HANDLE PipeHandle; DWORD BytesRead; DWORD BytesWritten; CHAR Buffer[256]; if ((PipeHandle = CreateNamedPipe("\\\\.\\PIPE\\jim", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, NUM_PIPES, 0, 0, 1000, NULL)) == INVALID_HANDLE_VALUE) { printf("CreateNamedPipe failed with error %d\n", GetLastError()); return 0; } // Serve client connections forever while (1) { if (ConnectNamedPipe(PipeHandle, NULL) == 0) { printf("ConnectNamedPipe failed with error %d\n", GetLastError()); break; } // Read data from and echo data to the client until // the client is ready to stop while (ReadFile(PipeHandle, Buffer, sizeof(Buffer), & BytesRead, NULL) > 0) { printf("Echo %d bytes to client\n", BytesRead); if (WriteFile(PipeHandle, Buffer, BytesRead, & BytesWritten, NULL) == 0) { printf("WriteFile failed with error %d\n", GetLastError()); break; } } if (DisconnectNamedPipe(PipeHandle) == 0) { printf("DisconnectNamedPipe failed with error %d\n", GetLastError()); break; } } CloseHandle(PipeHandle); return 0; } |