Im making a simple client server pipe example as an exercise. The server will receive a string from a client using a named pipe. The server will reverse the case of every character within the string recieved from the client and use the pipe to send the string back to the client. It works but the string I write to the pipe seems to get broken up by spaces. Theres an image attached that shows my problem.
I Create a named pipe like this on the server.
HANDLE pipe_handle = CreateNamedPipe(
PIPE_REV_NAME, //name
PIPE_ACCESS_DUPLEX, //openMode
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, //Pipe Mode
1,//Max Instances
1024,//OutBuffSize
1024,//InBuffSize
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
And read and write from/to it on the server like this:
DWORD bytes_read;
ReadFile(
pipe_handle,
(LPVOID)string_to_reverse,
MAX_PIPE_REVLEN - 1,
&bytes_read,
NULL);
string_to_reverse[bytes_read] = '\0';
printf("Recieved: %s\n", string_to_reverse);
cap_reverse(string_to_reverse, bytes_read);
printf("Sending: %s\n", string_to_reverse);
DWORD bytes_written;
WriteFile(
pipe_handle,
(LPVOID)string_to_reverse,
bytes_read,
&bytes_written,
NULL);
The Client Creates a file to use the pipe like this:
HANDLE pipe_handle = CreateFile(
PIPE_REV_NAME,
GENERIC_READ | GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL
);
And reads and writes to the pipe like this:
strncpy_s(buff, toReverse.c_str(), MAX_PIPE_REVLEN - 1);
printf("Sending: %s\n", buff);
WriteFile(
pipe_handle,
(LPVOID)buff,
toReverse.length(),
&bytes_written,
NULL);
printf("Waiting\n");
DWORD bytes_read = 0;
ReadFile(
pipe_handle,
(LPVOID)buff,
toReverse.length(),
&bytes_read,
NULL);