By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,627 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,627 IT Pros & Developers. It's quick & easy.

How to write named pipes application

P: 20
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
May 8 '10 #1

✓ answered by allynm

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:


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4.  
  5. int main( int argc, char * argv[ ]) 
  6. {
  7. static TCHAR ClientName[ ] = TEXT("pipeclient.exe") ;
  8. static TCHAR ServerName[ ] = TEXT("pipeserver.exe" ) ;
  9. TCHAR CommandLine[ ] = TEXT("Server Joe") ;
  10.  
  11. STARTUPINFO ClientStartupInfo, ServerStartupInfo ;
  12. PROCESS_INFORMATION ClientProcInfo, ServerProcInfo ;
  13. HANDLE hProcServ, hProcCli ;
  14. GetStartupInfo(&ClientStartupInfo) ;
  15. GetStartupInfo (&ServerStartupInfo ) ;
  16.  
  17. CreateProcess (ServerName, CommandLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
  18.     NULL, NULL, &ServerStartupInfo, &ServerProcInfo ) ;
  19. hProcServ = ServerProcInfo.hProcess ;
  20. printf ("Curr Server Process is : %d\n", hProcServ ) ;
  21. WaitForSingleObject(hProcServ, 1000) ;
  22. CreateProcess(ClientName, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, 
  23.     NULL, NULL, &ClientStartupInfo, &ClientProcInfo );
  24. hProcCli = ClientProcInfo.hProcess ;
  25. printf ("Curr Client Process is : %d\n", hProcCli ) ;
  26. WaitForSingleObject (hProcServ, INFINITE) ;
  27. WaitForSingleObject (hProcCli, INFINITE ) ;
  28. CloseHandle (hProcServ); CloseHandle(hProcCli) ;
  29. printf ("All Done!\n");
  30. return 0 ;
  31. }
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

Share this Question
Share on Google+
1 Reply


P: 20
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:


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4.  
  5. int main( int argc, char * argv[ ]) 
  6. {
  7. static TCHAR ClientName[ ] = TEXT("pipeclient.exe") ;
  8. static TCHAR ServerName[ ] = TEXT("pipeserver.exe" ) ;
  9. TCHAR CommandLine[ ] = TEXT("Server Joe") ;
  10.  
  11. STARTUPINFO ClientStartupInfo, ServerStartupInfo ;
  12. PROCESS_INFORMATION ClientProcInfo, ServerProcInfo ;
  13. HANDLE hProcServ, hProcCli ;
  14. GetStartupInfo(&ClientStartupInfo) ;
  15. GetStartupInfo (&ServerStartupInfo ) ;
  16.  
  17. CreateProcess (ServerName, CommandLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
  18.     NULL, NULL, &ServerStartupInfo, &ServerProcInfo ) ;
  19. hProcServ = ServerProcInfo.hProcess ;
  20. printf ("Curr Server Process is : %d\n", hProcServ ) ;
  21. WaitForSingleObject(hProcServ, 1000) ;
  22. CreateProcess(ClientName, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, 
  23.     NULL, NULL, &ClientStartupInfo, &ClientProcInfo );
  24. hProcCli = ClientProcInfo.hProcess ;
  25. printf ("Curr Client Process is : %d\n", hProcCli ) ;
  26. WaitForSingleObject (hProcServ, INFINITE) ;
  27. WaitForSingleObject (hProcCli, INFINITE ) ;
  28. CloseHandle (hProcServ); CloseHandle(hProcCli) ;
  29. printf ("All Done!\n");
  30. return 0 ;
  31. }
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
May 9 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.