Named pipes question!

Named pipes question!

Post by GTi » Thu, 08 Nov 2001 05:22:58



Wild guess:
LPTSTR lpszPipename = "\\\\.\\pipe\\mm";
( local computer )
LPTSTR lpszPipename = \\\\*\\pipe\\mm;
(every computer in the primary domain )

Used when sending data only.
Use <.> when listen. Use <*> when broadcast.


This source code server aplication:
DWORD Pipe(VOID)
{
   BOOL fConnected;
   DWORD dwThreadId;
   HANDLE hPipe, hThread;
   LPTSTR lpszPipename = "\\\\.\\pipe\\mm";
   while (1)
   {
      hPipe = CreateNamedPipe(
          lpszPipename,             // pipe name
          PIPE_ACCESS_DUPLEX,       // read/write access
          PIPE_TYPE_MESSAGE |       // message type pipe
          PIPE_READMODE_MESSAGE |   // message-read mode
          PIPE_WAIT,                // blocking mode
          PIPE_UNLIMITED_INSTANCES, // max. instances
          BUFSIZE,                  // output buffer size
          BUFSIZE,                  // input buffer size
          PIPE_TIMEOUT,             // client time-out
          NULL);                    // no security attribute
      if (hPipe == INVALID_HANDLE_VALUE) break;
      fConnected = ConnectNamedPipe(hPipe, NULL) ?
         TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

      if (fConnected)
      {
         hThread = CreateThread(
            NULL,              // no security attribute
            0,                 // default stack size
            (LPTHREAD_START_ROUTINE) InstanceThread,
            (LPVOID) hPipe,    // thread parameter
            0,                 // not suspended
            &dwThreadId);      // returns thread ID
         if (hThread == NULL) break;
      }
      else
   {
             CloseHandle(hPipe);
   break;
   }
   }
   return 1;

Quote:}

void InstanceThread(LPVOID lpvParam)
{
   CHAR chRequest[BUFSIZE];
   CHAR chReply[BUFSIZE];
   DWORD cbBytesRead, cbReplyBytes, cbWritten;
   BOOL fSuccess;
   HANDLE hPipe;
   hPipe = (HANDLE) lpvParam;
   while (1)
   {
      fSuccess = ReadFile(
         hPipe,        // handle to pipe
         chRequest,    // buffer to receive data
         BUFSIZE,      // size of buffer
         &cbBytesRead, // number of bytes read
         NULL);        // not overlapped I/O
      if (! fSuccess || cbBytesRead == 0)
         break;
      GetAnswerToRequest(chRequest, chReply, &cbReplyBytes,hh);
      fSuccess = WriteFile(
         hPipe,        // handle to pipe
         chReply,      // buffer to write from
         cbReplyBytes, // number of bytes to write
         &cbWritten,   // number of bytes written
         NULL);        // not overlapped I/O
      if (! fSuccess || cbReplyBytes != cbWritten) break;
  }
   FlushFileBuffers(hPipe);
   DisconnectNamedPipe(hPipe);
   CloseHandle(hPipe);

Quote:}

And this is source code client aplication:
DWORD main(int argc, char *argv[])
{
   HANDLE hPipe;
   LPVOID lpvMessage;
   CHAR chBuf[512];
   BOOL fSuccess;
   DWORD cbRead, cbWritten, dwMode;
   LPTSTR lpszPipename = "\\\\.\\pipe\\mm";
   while (1)
   {
      hPipe = CreateFile(
         lpszPipename,   // pipe name
         GENERIC_READ |  // read and write access
         GENERIC_WRITE,
         0,              // no sharing
         NULL,           // no security attributes
         OPEN_EXISTING,  // opens existing pipe
         0,              // default attributes
         NULL);          // no template file
      if (hPipe != INVALID_HANDLE_VALUE)
         break;
      if (GetLastError() != ERROR_PIPE_BUSY) break;
      if (! WaitNamedPipe(lpszPipename, 20000) ) break;
   }
   dwMode = PIPE_READMODE_MESSAGE;
   fSuccess = SetNamedPipeHandleState(
      hPipe,    // pipe handle
      &dwMode,  // new pipe mode
      NULL,     // don't set maximum bytes
      NULL);    // don't set maximum time
   if (!fSuccess) exit(0);
  /lpvMessage = (argc > 1) ? argv[1] : "default message";

   fSuccess = WriteFile(
      hPipe,                  // pipe handle
      lpvMessage,             // message
      strlen((char *)lpvMessage) + 1, // message length
      &cbWritten,             // bytes written
      NULL);                  // not overlapped
   if (! fSuccess) exit(0);
   do
   {
      fSuccess = ReadFile(
         hPipe,    // pipe handle
         chBuf,    // buffer to receive reply
         512,      // size of buffer
         &cbRead,  // number of bytes read
         NULL);    // not overlapped

      if (! fSuccess && GetLastError() != ERROR_MORE_DATA)
         break;
     if (! WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
         chBuf, cbRead, &cbWritten, NULL))
      {
         break;
    }
   } while (! fSuccess);  // repeat loop if ERROR_MORE_DATA
    CloseHandle(hPipe);
    return 0;

Quote:}

If I try to connect to pipe at local computer - it works. If I try to
connect to pipe across network this program fail. Client can't create file
in CreateFile function. What is the problem?
If you know please e-mail me:

 
 
 

Named pipes question!

Post by Dave » Thu, 08 Nov 2001 12:12:28


What does GetLastError() return in the client application?

What do you set this line in the client application to when you are
trying to connect across the network?
LPTSTR lpszPipename = "\\\\.\\pipe\\mm";

-Dave


> This source code server aplication:

> DWORD Pipe(VOID)
> {
>    BOOL fConnected;
>    DWORD dwThreadId;
>    HANDLE hPipe, hThread;
>    LPTSTR lpszPipename = "\\\\.\\pipe\\mm";
>    while (1)
>    {
>       hPipe = CreateNamedPipe(
>           lpszPipename,             // pipe name
>           PIPE_ACCESS_DUPLEX,       // read/write access
>           PIPE_TYPE_MESSAGE |       // message type pipe
>           PIPE_READMODE_MESSAGE |   // message-read mode
>           PIPE_WAIT,                // blocking mode
>           PIPE_UNLIMITED_INSTANCES, // max. instances
>           BUFSIZE,                  // output buffer size
>           BUFSIZE,                  // input buffer size
>           PIPE_TIMEOUT,             // client time-out
>           NULL);                    // no security attribute
>       if (hPipe == INVALID_HANDLE_VALUE) break;
>       fConnected = ConnectNamedPipe(hPipe, NULL) ?
>          TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

>       if (fConnected)
>       {
>          hThread = CreateThread(
>             NULL,              // no security attribute
>             0,                 // default stack size
>             (LPTHREAD_START_ROUTINE) InstanceThread,
>             (LPVOID) hPipe,    // thread parameter
>             0,                 // not suspended
>             &dwThreadId);      // returns thread ID
>          if (hThread == NULL) break;
>       }
>       else
>    {
>              CloseHandle(hPipe);
>    break;
>    }
>    }
>    return 1;
> }

> void InstanceThread(LPVOID lpvParam)
> {
>    CHAR chRequest[BUFSIZE];
>    CHAR chReply[BUFSIZE];
>    DWORD cbBytesRead, cbReplyBytes, cbWritten;
>    BOOL fSuccess;
>    HANDLE hPipe;
>    hPipe = (HANDLE) lpvParam;
>    while (1)
>    {
>       fSuccess = ReadFile(
>          hPipe,        // handle to pipe
>          chRequest,    // buffer to receive data
>          BUFSIZE,      // size of buffer
>          &cbBytesRead, // number of bytes read
>          NULL);        // not overlapped I/O
>       if (! fSuccess || cbBytesRead == 0)
>          break;
>       GetAnswerToRequest(chRequest, chReply, &cbReplyBytes,hh);
>       fSuccess = WriteFile(
>          hPipe,        // handle to pipe
>          chReply,      // buffer to write from
>          cbReplyBytes, // number of bytes to write
>          &cbWritten,   // number of bytes written
>          NULL);        // not overlapped I/O
>       if (! fSuccess || cbReplyBytes != cbWritten) break;
>   }
>    FlushFileBuffers(hPipe);
>    DisconnectNamedPipe(hPipe);
>    CloseHandle(hPipe);
> }

> And this is source code client aplication:

> DWORD main(int argc, char *argv[])
> {
>    HANDLE hPipe;
>    LPVOID lpvMessage;
>    CHAR chBuf[512];
>    BOOL fSuccess;
>    DWORD cbRead, cbWritten, dwMode;
>    LPTSTR lpszPipename = "\\\\.\\pipe\\mm";
>    while (1)
>    {
>       hPipe = CreateFile(
>          lpszPipename,   // pipe name
>          GENERIC_READ |  // read and write access
>          GENERIC_WRITE,
>          0,              // no sharing
>          NULL,           // no security attributes
>          OPEN_EXISTING,  // opens existing pipe
>          0,              // default attributes
>          NULL);          // no template file
>       if (hPipe != INVALID_HANDLE_VALUE)
>          break;
>       if (GetLastError() != ERROR_PIPE_BUSY) break;
>       if (! WaitNamedPipe(lpszPipename, 20000) ) break;
>    }
>    dwMode = PIPE_READMODE_MESSAGE;
>    fSuccess = SetNamedPipeHandleState(
>       hPipe,    // pipe handle
>       &dwMode,  // new pipe mode
>       NULL,     // don't set maximum bytes
>       NULL);    // don't set maximum time
>    if (!fSuccess) exit(0);
>   /lpvMessage = (argc > 1) ? argv[1] : "default message";

>    fSuccess = WriteFile(
>       hPipe,                  // pipe handle
>       lpvMessage,             // message
>       strlen((char *)lpvMessage) + 1, // message length
>       &cbWritten,             // bytes written
>       NULL);                  // not overlapped
>    if (! fSuccess) exit(0);
>    do
>    {
>       fSuccess = ReadFile(
>          hPipe,    // pipe handle
>          chBuf,    // buffer to receive reply
>          512,      // size of buffer
>          &cbRead,  // number of bytes read
>          NULL);    // not overlapped

>       if (! fSuccess && GetLastError() != ERROR_MORE_DATA)
>          break;
>      if (! WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
>          chBuf, cbRead, &cbWritten, NULL))
>       {
>          break;
>     }
>    } while (! fSuccess);  // repeat loop if ERROR_MORE_DATA
>     CloseHandle(hPipe);
>     return 0;
> }

> If I try to connect to pipe at local computer - it works. If I try to
> connect to pipe across network this program fail. Client can't create
> file in CreateFile function. What is the problem?
> If you know please e-mail me:



 
 
 

Named pipes question!

Post by Andy Walldorf » Thu, 08 Nov 2001 21:58:49


If I remember correctly, you cannot create a named pipe on a remote server,
only open it.  It needs to be created locally.  So..., you answered your oen
question.

PS: Just say no to HTML news.....


This source code server aplication:
[source deleted]
If I try to connect to pipe at local computer - it works. If I try to
connect to pipe across network this program fail. Client can't create file
in CreateFile function. What is the problem?
If you know please e-mail me:

 
 
 

Named pipes question!

Post by Jarmo Muukk » Fri, 09 Nov 2001 03:50:08


The problem is in client code. You try to connect to local pipe.
You should replace dot with the computer name of the server end.

--
JMu



If I try to connect to pipe at local computer - it works. If I try
to connect to pipe across network this program fail. Client can't
create file in CreateFile function. What is the problem?
If you know please e-mail me:

 
 
 

1. IPC and Named Pipe Question

I have two win2k processes which need to communicate.  The processes may run
together on the same machine, in the case where network is not available, or
on separate machines, where network is available.  One case is that no
network, not even loopback functionality is available.  Is there an IPC
mechanism that will handle both cases?  I know shared memory would be an
option for the case where network is not available, but will not work across
network.  Will Named Pipes work in the case of no network hardware, or do
they rely on underlying network protocol to work?  Am I doomed to
use/maintain two mechanisms? Share Memory in one case and another IPC in the
other?

CB

2. newbie firewall question

3. Named Pipe questions

4. Adding more evidence

5. Named Pipe Question

6. Is there a commercial bi-directional RF amp I can put ahead of a cable modem?

7. Named pipe questions

8. MKIII RAM bus Speed Issue

9. Named Pipes Question

10. Help with named pipe reconnect losing TX of the pipe, RX still works

11. Named Pipes and overlapping IO question

12. Question: transport layer of named pipe?

13. Named Pipe Security Questions...