How to write named pipes application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • allynm
    New Member
    • Apr 2010
    • 20

    How to write named pipes application

    am a newby with Windows named pipes, and have an obviously newby question.

    The context of the program is this: I am trying to create a simple named pipe application on a single machine (no network!). A server creates a named pipe and a cllient reads/writes to the the server using a duplex pipe. The server is not multithreaded. The server has a main( ) and the client has its own main( ).

    I am having huge conceptual difficulty understanding how the server with its main entry point can run at the same time as the client with its own main entry point. I keep thinking the two programs must be in different processes and therefore there must be a third program which creates the two processes, ie, the client and the server.

    I have spent two fruitless days trying to hunt down examples of client/server and failed. The examples I have seen discuss the problem without clearly spelling out whether and how the client and server run concurrently. What I do know is that when I code the commonly available examples, I receive a message back saying that the pipe is could not be opened.

    Any light that could be shown on this would be most helpful. If you could direct me to a good tutorial that clearly identifies the process architecture for this type of application that would be welcome too.

    Sincerely,
    Mark Allyn
  • allynm
    New Member
    • Apr 2010
    • 20

    #2
    Hello Everyone,

    As often happens to me, after posting something and struggling some more, I get a solution. The answer is: Yes, you must indeed create a separate server process and a separate client process as well as a pipe connecting them. The program I wrote that does this is shown next:


    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int main( int argc, char * argv[ ]) 
    {
    static TCHAR ClientName[ ] = TEXT("pipeclient.exe") ;
    static TCHAR ServerName[ ] = TEXT("pipeserver.exe" ) ;
    TCHAR CommandLine[ ] = TEXT("Server Joe") ;
    
    STARTUPINFO ClientStartupInfo, ServerStartupInfo ;
    PROCESS_INFORMATION ClientProcInfo, ServerProcInfo ;
    HANDLE hProcServ, hProcCli ;
    GetStartupInfo(&ClientStartupInfo) ;
    GetStartupInfo (&ServerStartupInfo ) ;
    
    CreateProcess (ServerName, CommandLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
    	NULL, NULL, &ServerStartupInfo, &ServerProcInfo ) ;
    hProcServ = ServerProcInfo.hProcess ;
    printf ("Curr Server Process is : %d\n", hProcServ ) ;
    WaitForSingleObject(hProcServ, 1000) ;
    CreateProcess(ClientName, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, 
    	NULL, NULL, &ClientStartupInfo, &ClientProcInfo );
    hProcCli = ClientProcInfo.hProcess ;
    printf ("Curr Client Process is : %d\n", hProcCli ) ;
    WaitForSingleObject (hProcServ, INFINITE) ;
    WaitForSingleObject (hProcCli, INFINITE ) ;
    CloseHandle (hProcServ); CloseHandle(hProcCli) ;
    printf ("All Done!\n");
    return 0 ;
    }
    Obviously, my client is in pipeclient.exe and the server is in pipeserver.exe.
    The thing ain't pretty, but it works.

    Any comments or suggestions on how to clean this thing up -- e.g, error correction code--would be appreciated.

    Ciao,
    'Mark

    Comment

    Working...