Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:12:15+00:00 2026-05-24T12:12:15+00:00

I have a named pipe server and client. (Doing this in VC++). Server does

  • 0

I have a named pipe server and client. (Doing this in VC++).

Server does

  1. CreateNamedPipe
  2. ConnectNamedPipe
  3. WriteFile
  4. Disconnect
  5. Repeat from 2 to 4

Client does

  1. CreateFile
  2. ReadFile

The order of execution is as follows,

  1. Server — CreateNamedPipe
  2. Client — CreateFile
  3. Server — ConnectNamedPipe (should return immediately as the client is already connected)
  4. Server — WriteFile
  5. Client — ReadFile
  6. Server — DisconnectNamedPipe
  7. Client — CloseHandle
  8. goto 2

This works fine for the first time. However problem occurs when client tries to connects for the second time. When the client tries to connect (CreateFile) for the second time before the server did ConnectNamedPipe (but after disconnectnamedpipe), it gets ERROR_PIPE_BUSY. It works if client calls createfile after the server calls ConnectNamedPipe.

Is there anyway that i can get client connected (CreateFile) before server called ConnectNamedPipe (after DisconnectNamedPipe)?

Server code:

pipe_handle.pipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\testpipe1"),
                PIPE_ACCESS_OUTBOUND |
                FILE_FLAG_OVERLAPPED,        // read/write access
                PIPE_TYPE_MESSAGE |           // message type pipe
                PIPE_READMODE_MESSAGE |       // message-read mode
                PIPE_WAIT,                    // blocking mode
                PIPE_UNLIMITED_INSTANCES,     // max. instances
                BUFFER_SIZE,                  // output buffer size
                BUFFER_SIZE,                  // input buffer size
                2000,              // client time-out
                NULL);

if (pipe_handle.pipe == INVALID_HANDLE_VALUE) {
    std::cout << "Error while creating pipe" << std::endl;
    return -1;
}
std::cout <<"Connecting to named pipe" << std::endl;

std::cout<< "Somebody connected to named pipe" << std::endl;

int ac;

for (ac=0; ac<2; ac++) {

    char a[25];
    // Wait for some input. This helps me to start the client in other terminal.
    cin >> a;
    cout << "Connecting..." << endl;

    ConnectNamedPipe(pipe_handle.pipe, 0);

    cout << "Connect pipe returned." << endl;

    // Wait for some input.
    cin >> a;
    string message = "Test message";
    DWORD bytes_written;

    if (!WriteFile(pipe_handle.pipe, message.c_str(), message.size(),
                   &bytes_written, NULL)) {

        DWORD er = GetLastError();
        char errs[200];
        sprintf(errs, "Error : %ld", er);
        std::cout << "Error communicating to client.";
        std::cout << errs;
    }
    std::cout << "Written to pipe";
    FlushFileBuffers(pipe_handle.pipe);
    if (!DisconnectNamedPipe(pipe_handle.pipe)) {
        std::cout << "Disconnect failed"<< GetLastError() << endl;
    } else {
        std::cout << "Disconnect successful"<<endl;
    }
}

Client Code:

while (1) { 

    std::cout << "Returned" << std::endl;
    hPipe = CreateFile( 
              lpszPipename,   // pipe name 
              GENERIC_READ, 
              0,              // no sharing 
              NULL,           // default security attributes
              OPEN_EXISTING,  // opens existing pipe 
              FILE_FLAG_OVERLAPPED,              // default attributes 
              NULL);          // no template file 

    // Break if the pipe handle is valid. 

    if (hPipe != INVALID_HANDLE_VALUE) 
        break; 


    // Exit if an error other than ERROR_PIPE_BUSY occurs. 

    if (GetLastError() != ERROR_PIPE_BUSY) {
        std::cout<< "Could not open pipe " << GetLastError() << std::endl; 
        return -1;
    }

    // All pipe instances are busy, so wait for sometime.

    if ( ! WaitNamedPipe(lpszPipename, NMPWAIT_USE_DEFAULT_WAIT)) { 
        std::cout<<  "Could not open pipe: wait timed out." << std::endl; 
    } 
}

OVERLAPPED ol1;

memset(&ol1, 0, sizeof(ol1));
ol1.Offset = 0;
ol1.OffsetHigh = 0;
ol1.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

HANDLE events[1];
events[0] = ol1.hEvent;
cbToWrite = (lstrlen(message)+1)*sizeof(TCHAR);

DWORD bytes_to_read = 2000;
char * buf = reinterpret_cast<char *>(malloc(bytes_to_read));
DWORD bytes_read;

std::cout << "Waiting for read" << std::endl;
bool a = ReadFile(hPipe, buf, bytes_to_read, &bytes_read, &ol1);


if ( ! fSuccess) {
    std::cout << "WriteFile to pipe failed. GLE " << GetLastError() << std::endl; 
}
std::cout << "Waiting for multiple objects" << std::endl;
WaitForMultipleObjects(1, events, FALSE, INFINITE);
std::cout << "multiple objects returned" << std::endl;
printf("\nMessage sent to server");
CancelIo(hPipe);
CloseHandle(hPipe);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:12:16+00:00Added an answer on May 24, 2026 at 12:12 pm

    If you get ERROR_PIPE_BUSY on the CreateFile() call in the client, you need to call WaitNamedPipe() and then retry when it returns. If you get a return of zero from WaitNamedPipe() that means it timed out without the pipe becoming available. You’ll never see that happen if you pass NMPWAIT_WAIT_FOREVER as the timeout.

    You also need to keep in mind that the pipe may become busy again between the time WaitNamedPipe() returns and you call CreateFile(); therefore, you need to do it in a loop. Like this:

    while (true)
    {
        hPipe = CreateFile(pipeName,
                           GENERIC_READ | GENERIC_WRITE,
                           0,
                           0,
                           OPEN_EXISTING,
                           FILE_ATTRIBUTE_NORMAL,
                           0);
        if (hPipe == INVALID_HANDLE_VALUE)
        {
            if (GetLastError() == ERROR_PIPE_BUSY)
            {
                if (!WaitNamedPipe(pipeName, NMPWAIT_USE_DEFAULT_WAIT))
                    continue;   // timeout, try again
            }
            else
                return false;   // error
        }
        else
            break;   // success
    }
    

    EDIT:

    I simplified your code and now it works fine. Working server and client follow.

    Server:

    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
        HANDLE pipe;
        const DWORD BUFFER_SIZE = 1024;
    
        pipe = CreateNamedPipe("\\\\.\\pipe\\testpipe1",
                                      PIPE_ACCESS_OUTBOUND |
                                      FILE_FLAG_OVERLAPPED,          // read/write access
                                      PIPE_TYPE_MESSAGE |             // message type pipe
                                      PIPE_READMODE_MESSAGE |         // message-read mode
                                      PIPE_WAIT,                          // blocking mode
                                      PIPE_UNLIMITED_INSTANCES,   // max. instances
                                      BUFFER_SIZE,                        // output buffer size
                                      BUFFER_SIZE,                        // input buffer size
                                      2000,                 // client time-out
                                      NULL);
    
        if (pipe == INVALID_HANDLE_VALUE)
        {
            printf("Error while creating pipe\n");
            return -1;
        }
        printf("Connecting to named pipe\n");
    
        int ac;
    
        for (ac=0; ac<2; ac++)
        {
            // Wait for some input. This helps me to start the client in other terminal.
            printf("Connecting...\n");
    
            ConnectNamedPipe(pipe, 0);
    
            printf("Connect pipe returned.\n");
    
            // Wait for some input.
            char * message = "Test message";
            DWORD bytes_written;
    
            if (!WriteFile(pipe, message, strlen(message)+1, &bytes_written, NULL))
            {
    
                DWORD er = GetLastError();
                char errs[200];
                sprintf_s(errs, "Error : %ld", er);
                printf("Error communicating to client.\n");
                printf(errs);
            }
            printf("Written to pipe\n");
            FlushFileBuffers(pipe);
            if (!DisconnectNamedPipe(pipe))
            {
                printf("Disconnect failed %d\n", GetLastError());
            }
            else
            {
                printf("Disconnect successful\n");
            }
        }
    }
    

    Client:

    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
        HANDLE hPipe;
    
        while (1)
        {
    
            printf("Returned\n");
            hPipe = CreateFile("\\\\.\\pipe\\testpipe1",
                                    GENERIC_READ, 
                                    0,                   // no sharing 
                                    NULL,                // default security attributes
                                    OPEN_EXISTING,   // opens existing pipe 
                                    0,                // default attributes 
                                    NULL);           // no template file 
    
            // Break if the pipe handle is valid. 
    
            if (hPipe != INVALID_HANDLE_VALUE)
                break;
    
    
            // Exit if an error other than ERROR_PIPE_BUSY occurs. 
    
            if (GetLastError() != ERROR_PIPE_BUSY)
            {
                printf("Could not open pipe %d\n", GetLastError()); 
                return -1;
            }
    
            // All pipe instances are busy, so wait for sometime.
    
            if ( ! WaitNamedPipe("\\\\.\\pipe\\testpipe1", NMPWAIT_USE_DEFAULT_WAIT))
            {
                printf("Could not open pipe: wait timed out.\n"); 
            }
        }
    
    
        char *message = "hello";
        DWORD cbToWrite = (strlen(message)+1)*sizeof(message[0]);
    
        DWORD bytes_to_read = 2000;
        char * buf = reinterpret_cast<char *>(malloc(bytes_to_read));
        DWORD bytes_read;
    
        printf("Waiting for read\n");
        bytes_read = 0;
        ReadFile(hPipe, buf, bytes_to_read, &bytes_read, 0);
    
        if (bytes_read <= 0)
        {
            printf("ReadFile from pipe failed. GLE \n"); 
        }
        else
            printf("Read %d bytes: %s\n", bytes_read, buf);
    
        CloseHandle(hPipe);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a client that connects to a server via a named pipe, and
I have a C pipe client (pulled directly from the CallNamedPipe example found here
I have a pair of shell programs that talk over a named pipe. The
I have two named instances of SQL Server 2008 and am trying to set
I have a table named Info of this schema: int objectId; int time; int
I have a table named visiting that looks like this: id | visitor_id |
I have two .NET applications that talk to each other over a named pipe.
I have been trying to get up to speed on Named Pipes this week.
Can I have a VB6 app call a named pipe connection to a service
I have a Named Pipe and It Works Fine While I access it using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.