Named Pipe woes

Answered Named Pipe woes

  • lundi 16 avril 2012 16:57
     
     

    I am clearly doing something wrong, or I don't understand Named Pipes. 

    On the server side I call the following:

    hPipe = CreateNamedPipe( mypipe,PIPE_ACCESS_DUPLEX,PIPE_NOWAIT|PIPE_TYPE_MESSAGE,PIPE_UNLIMITED_INSTANCES,1024,1024,0,0);

    I then go into a ReadFile loop repeating until ReadFile returns TRUE, and then call WriteFile to write back a reply.  After that I go back into the ReadFile loop.

    On the Client side I do the following:

    rc = CallNamedPipe(myPipe, ..., 10);  // 10 MS

    The first transaction from the client works fine.  The server receives the message and the client receives the reply.

    However, the second time I go into a ReadFile loop, ReadFile fails with ERROR_BROKEN_PIPE, and on the client side, CallNamedPipe fails with ERROR_PIPE_BUSY.

    On the server side, do I need to close and reopen the pipe after each transaction?

    Thanks for any guidance.

Toutes les réponses

  • lundi 16 avril 2012 17:22
     
     

    check this link this may help you out

    http://www.cplusplus.com/forum/articles/11168/

  • lundi 16 avril 2012 18:16
     
     

    Well, using CallNamedPipe you do. As the documentation states

    Connects to a message-type pipe (and waits if an instance of the pipe is not available), writes to and reads from the pipe, and then closes the pipe.

    So using CallNamedPipe you will have to reopen the pipe every time.

    However, if you use CreateFile (yes, CreateFile) to open the client end of the pipe then it won't close the pipe when you close the handle to the client end of the pipe.

    Reading through the Pipe section of the MSDN documentation would help I think.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • lundi 16 avril 2012 18:18
     
     

    > check this link this may help you out
    > http://www.cplusplus.com/forum/articles/11168/

    Thanks for the suggestion.  It looks like instead of using CallNamedPipe this example is using CreateFile, WriteFile, ReadFile, CloseHandle on the client side.

    I also tried it that way with the same results. The first "transaction" worked fine, but after that I got ERROR_PIPE_BROKEN on the server side and ERROR_PIPE_BUSY on the client side.



    • Modifié Neil.W lundi 16 avril 2012 18:32
    •  
  • lundi 16 avril 2012 18:29
     
     
    Did you try to call DisconnectNamedPipe on the server side after the client had disconnected?

    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • lundi 16 avril 2012 18:31
     
     

    Well, using CallNamedPipe you do. As the documentation states

    Connects to a message-type pipe (and waits if an instance of the pipe is not available), writes to and reads from the pipe, and then closes the pipe.

    Yes, I saw that thanks. I just assumed that it closed down the connection from the client side, but that the server side would continue listening for connections. I didn't realize it would close down the whole pipe.

    What if, on the Client side, I am using CreateFile, WriteFile, ReadFile, CloseHandle: does that shut down the pipe on the server side as well? From my tests it appears that it does.


    • Modifié Neil.W lundi 16 avril 2012 18:34
    •  
  • lundi 16 avril 2012 19:11
     
     Traitée

    To be honest the problem is most likely the server side.

    The main sample, the multithreaded pipe server deals with the different pipe instances by simply closing the handle to the pipe on the server each time. But that isn't good for this. But looking for information in another sample gave the information that was needed. (It saved me writing a sample myself.)

    In the Named Pipe Server Using Overlapped I/O sample. The DisconnectAndReconnect function shows you how to do this without closing the pipe handle itself.

    In amongst all of that you will notice that it calls the DisconnectNamedPipe function to disconnect the current client and then calls ConnectNamedPipe to connect a new client.

    So in your server process, after the ReadFile/WriteFile loop, try calling DisconnectNamedPipe before your call to ConnectNamedPipe.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • mardi 24 avril 2012 07:36
    Modérateur
     
     

    Hi,

    I marked Crescens2K's reply as answer, if your issue have not been solved please unmark it and update more information about this issue.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us