🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by T. Meyer Fri, 18 Aug 2006 12:35
Named pipe WriteFile problem
#3141
Author: T. Meyer
Date: Fri, 18 Aug 2006 12:35
104 lines
4286 bytes
Hello,

I use a named pipe to write something to my pipe server.
I want to write just one byte into the pipe so I use following code:

#define BUFSIZE 512
...
char chBuf[BUFSIZE] = "HELLO";
bool fSuccess = false;
DWORD dwBytesWritten = 0, dwTotalBytesWritten = 0, dwWait;

HANDLE PipeWriteEvt = CreateEvent(
                       NULL,    // default security attribute
                       TRUE,    // manual-reset event
                       FALSE,    // initial state = signaled
                       NULL);   // unnamed event object

OL.Internal = 0;
OL.InternalHigh = 0;
OL.Offset = 0;
OL.OffsetHigh = 0;
OL.hEvent = PipeWriteEvt;

fSuccess = WriteFile(
 hPipe,                 // pipe handle
 chBuf,                 // buffer to write
 1, // Write 1 byte only
 &dwBytesWritten,       // number of bytes written
 &OL);                  // NULL if not overlapped

if I look on the pipe server I see that the whole buffer was send (HELLO) 
even if I set nNumberOfBytesToWrite to 1.

I do further checkings after I use WriteFile(...). So I check if the no of 
bytes written equals the length of word to send (here HELLO = 5 bytes to 
send).
If not I repeat calling WriteFile(...) until they are equal.

But on my server I get now five times the whole word HELLO instead of H, E, 
L, L, O, even if I send only one byte each time.
Seems that the WriteFile function doesnt see that I set bytesToSend to 1?
If I look into dwBytesWritten after WriteFile(...) it says 1, but on the 
server I get all 5 bytes!?!
What is wrong with my code?

Here the code what I do after 1st time calling WriteFile:

    dwTotalBytesWritten = dwBytesWritten;
    if ((!fSuccess) || (dwTotalBytesWritten != MsgLength)) // MsgLenght is 
calculates, it equals 5 here
    {
      // ReadFile not completed, so wait until PipeEvt signaled
      DWORD dwErr;

      dwErr = GetLastError();
      if ((dwErr != ERROR_IO_PENDING) && (dwErr != ERROR_SUCCESS))
      {
        ShowError(dwErr);
      }
      else
      {
        while((!fSuccess) || (dwTotalBytesWritten != MsgLength))
        {
          Application->ProcessMessages();

          // Wait until PipeReadEvt singnals that some or all
          // of bytes are available
          dwWait = WaitForSingleObject(
           PipeWriteEvt, // handle of event objects
           1000);   // wait 1 sec.

          switch (dwWait)
          {
            case WAIT_TIMEOUT:  TimeNeeded += 1;
                                break;

            case WAIT_OBJECT_0: ResetEvent(PipeWriteEvt);
                                fSuccess = WriteFile(
                                 hPipe,                 // pipe handle
                                 chBuf,                 // buffer to write
                                 1, , // write 1 byte only
                                 &dwBytesWritten,       // number of bytes 
written
                                 &OL);                  // NULL if not 
overlapped

                                dwTotalBytesWritten += dwBytesWritten;
                                break;
          }
          Application->ProcessMessages();
          if (TimeNeeded >= TimeOut)
          {
            ret = false;
            break;
          }
        }
      }
    }
...
CloseHandle(PipeWriteEvt);
CloseHandle(hPipe);
...

Thanks for any help! 
Re: Named pipe WriteFile problem
#3142
Author: T. Meyer
Date: Fri, 18 Aug 2006 12:50
3 lines
892 bytes
Hello,

problem solved, it was on pipe server side!
Re: Named pipe WriteFile problem
#3145
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Aug 2006 17:42
32 lines
2056 bytes
"T. Meyer" <timon@mustek.de> wrote in message
news:44e5b421$1@newsgroups.borland.com...



> if I look on the pipe server I see that the whole buffer was send (HELLO)
> even if I set nNumberOfBytesToWrite to 1.
>
> I do further checkings after I use WriteFile(...). So I check if the no of
> bytes written equals the length of word to send (here HELLO = 5 bytes to
> send).
> If not I repeat calling WriteFile(...) until they are equal.

There is your problem them.  You are actually writing all 5 bytes to the
pipe, just individually.

> But on my server I get now five times the whole word HELLO instead
> of H, E, L, L, O, even if I send only one byte each time

On the server side, the bytes are going to cumulate in the pipe's buffer
until the server code actually reads from it.  Since you did not show any of
your server code yet, I can only assume that the code is reading the entire
buffer because you are specifying a size larger than 1 in your calls to
ReadFile().  Please show the actual server code.

> Seems that the WriteFile function doesnt see that I set bytesToSend to 1?

Yes, it does.


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads