Board index » cppbuilder » Named pipes make me crazy

Named pipes make me crazy


2007-10-29 04:24:23 PM
cppbuilder9
Hi there.
I've been experiencing a problem with named pipes server (single
threaded server using i/o overlapped reading/writing). The code
examples of a client and server may be found in Codegear RAd
studio 2007 ("named pipe client" and "named pipes single thread
using overlapped i/o"). The only thing i changed was implementing
a reading handler wich was supposed to store received data into
the Tlist object.
The problem is that if i dont receive data from client outright
after connect i get an ERROR_IO_PENDING pseude error while executing ReadFile function. Subsequently, after having received
that error just once i'm unable to read the info from the pipe
unless to drop the client and try the same again (which results
with the same error).
Have no idea about whether was it planned by example writers to handle this kind of a trouble but nothing seems to be working to receive the data which followed that error.
So i'm interested is there any workarounds on that? I mean when i execute ReadFile and it returs ERROR_IO_PENDING error.
In my opinion the most common "reading pipe scenario" would be:
0)Connect to the client
1)Perform ReadFile on the connected pipe;
2)if it returns ERROR_IO_PENDING - wait for the pipe to finish the pending operation (read in this case) by using GetOverlappedResult function (as it implemented in example).
3) perform readfile function again.
The problem is that it can never read the data from the pipe later on even after all the info have been received (GetOverlappedResult returns correct bytes transferred).
As far as it goes i'm deeply stuck into that without seeing any light in the end of the tonnel.
Thanks in advance
Any help would be appreciated.
Andrew.
P.S. code examples located in CodeGear RAD Studion 2007 ("Using Pipes") ms-help://borland.bds5/Ipc/base/using_pipes.htm
 
 

Re:Named pipes make me crazy

"Andrew" < XXXX@XXXXX.COM >wrote in message
Quote
The problem is that if i dont receive data from client outright after
connect i get an ERROR_IO_PENDING pseude error while
executing ReadFile function.
Please show your actual code. Are you calling WaitForOverlappedResult() at
all?
Quote
Subsequently, after having received that error just once i'm unable to
read the info from the pipe unless to drop the client and try the same
again (which results with the same error).
Then you are likely not managing the pipe correctly in the first place.
Quote
So i'm interested is there any workarounds on that? I mean when
i execute ReadFile and it returs ERROR_IO_PENDING error.
You are supposed to be using WaitForOverlappedResult() in that situation, if
you are not already.
Quote
In my opinion the most common "reading pipe scenario" would be:
Step #3 is wrong. You do not call ReadFile() again to receive the pending
data. The overlapped operation will receive the data into the same buffer
that you initially pass to the first call to ReadFile(). That is why the
buffer you give to ReadFile() must remain valid until after the overlapped
operation is completely finished. GetOverlappedResult() will then report
the number of bytes that the overlapped operation stored into the buffer,
similar to ReadFile()'s lpNumberOfBytesRead parameter.
Quote
The problem is that it can never read the data from the pipe later on
even after all the info have been received (GetOverlappedResult
returns correct bytes transferred).
You are calling ReadFile() a second time when there is nothing to read,
because it was already read by the first call to ReadFile(). Pipes are very
sensitive to reading/writing calls that are out of order. I learned that
the hard way a long time ago. That is why I rarely ever use named pipes
anymore. TCP sockets are more reliable and flexible.
Quote
P.S. code examples located in CodeGear RAD Studion 2007
("Using Pipes") ms-help://borland.bds5/Ipc/base/using_pipes.htm
I don't have that example onhand right now, hense the request to please show
the actual code you are using.
Gambit
 

Re:Named pipes make me crazy

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >сообщи?сообщила ?новостях
следующе? news:4726154f$ XXXX@XXXXX.COM ...
Quote

"Andrew" < XXXX@XXXXX.COM >wrote in message
news:472598b7$ XXXX@XXXXX.COM ...

>The problem is that if i dont receive data from client outright after
>connect i get an ERROR_IO_PENDING pseude error while
>executing ReadFile function.

Please show your actual code. Are you calling WaitForOverlappedResult()
at all?

>Subsequently, after having received that error just once i'm unable to
>read the info from the pipe unless to drop the client and try the same
>again (which results with the same error).

Then you are likely not managing the pipe correctly in the first place.

>So i'm interested is there any workarounds on that? I mean when
>i execute ReadFile and it returs ERROR_IO_PENDING error.

You are supposed to be using WaitForOverlappedResult() in that situation,
if you are not already.

>In my opinion the most common "reading pipe scenario" would be:

Step #3 is wrong. You do not call ReadFile() again to receive the pending
data. The overlapped operation will receive the data into the same buffer
that you initially pass to the first call to ReadFile(). That is why the
buffer you give to ReadFile() must remain valid until after the overlapped
operation is completely finished. GetOverlappedResult() will then report
the number of bytes that the overlapped operation stored into the buffer,
similar to ReadFile()'s lpNumberOfBytesRead parameter.
thank you very much!
i'll give it a shot!
P.S. couldnt find WaitForOverlappedResult function, yet, i'll stick to
GetOverlappedresult one.
Quote
>The problem is that it can never read the data from the pipe later on
>even after all the info have been received (GetOverlappedResult
>returns correct bytes transferred).

You are calling ReadFile() a second time when there is nothing to read,
because it was already read by the first call to ReadFile(). Pipes are
very sensitive to reading/writing calls that are out of order. I learned
that the hard way a long time ago. That is why I rarely ever use named
pipes anymore. TCP sockets are more reliable and flexible.

>P.S. code examples located in CodeGear RAD Studion 2007
>("Using Pipes") ms-help://borland.bds5/Ipc/base/using_pipes.htm

I don't have that example onhand right now, hense the request to please
show the actual code you are using.


Gambit

 

{smallsort}

Re:Named pipes make me crazy

Andrew Fedoseev wrote:
Quote
P.S. couldnt find WaitForOverlappedResult function, yet, i'll stick to
GetOverlappedresult one.
Use CreateEvent() and place the event handle in your OVERLAPPED structure.
Then you can WaaiForSingleObject() on that handle.
 

Re:Named pipes make me crazy

"Andrew Fedoseev" < XXXX@XXXXX.COM >wrote in message
Quote
couldnt find WaitForOverlappedResult function, yet, i'll
stick to GetOverlappedresult one.
That is what I meant, sorry.
Gambit
 

Re:Named pipes make me crazy

"Bob Gonder" < XXXX@XXXXX.COM >???????/???????? ? ????????
?????????: news: XXXX@XXXXX.COM ...
Quote
Andrew Fedoseev wrote:

>P.S. couldnt find WaitForOverlappedResult function, yet, i'll stick to
>GetOverlappedresult one.

Use CreateEvent() and place the event handle in your OVERLAPPED structure.
Then you can WaaiForSingleObject() on that handle.


Yes, thanks for the suggestion, Bob. It was already implemented in that
example. The only problem WAS :)
a tricky pipe behavior you may have heard above.