1

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);

The Output of the client and server

DNS_Jeezus
  • 289
  • 4
  • 17
  • 1
    Please don't post screenshots/images of output that could have just as easily been posted as text. – Jesper Juhl Jul 18 '19 at 16:48
  • If you want to send a stream of bytes (rather than a string), then read/send a byte stream and not a string. Don't use functions that *interpret* the bytes. – Jesper Juhl Jul 18 '19 at 16:52
  • though thank you for filling in the alt text, at least – Lightness Races in Orbit Jul 18 '19 at 16:54
  • Show the *loop* that reads the input data to send, then actually sends it. the latter we have an idea of, but the former is key. as it stands now there is no [mcve], and we need one, otherwise we're just guessing. My guess: you should be using `std::getline` to read your string, and send *that*; not using formatted extraction via `operator >>`. Now, show the relevant code by updating your question. – WhozCraig Jul 18 '19 at 16:58

1 Answers1

5

This has nothing to do with pipes.

From your screenshot we can see that it is your client that has handled your input one word at a time. (Notice how "Sending:" is repeated for each word.)

You didn't show us that code, or indeed a minimal reproducible example, but I can tell that you did something like this:

std::string input;
if (std::cin >> input)
{
   // send to pipe
}

Don't. Instead, do this:

std::string input;
if (std::getline(std::cin, input))
{
   // send to pipe
}

References:

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055