Server
#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("Create NamedPipe failed with error %d\n", GetLastError()); return; } printf("Server is now running, listenning for the client.\n"); while(1){ 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("ReadFile failed with error %d\n", GetLastError()); return; } } CloseHandle(PipeHandle); return;}
Client
#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; } printf("Server is now running."); 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.", 15, &BytesWritten, NULL) == 0){ printf("ReadFile failed with error %d\n", GetLastError()); CloseHandle(PipeHandle); return; } printf("Wrote %d bytes.\n", BytesWritten); return;}