Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Client not reading from named pipe

KempCGDR (Programmer) (OP)
30 Nov 05 11:51
Hi, I've been putting together a server and client that use a named pipe to communicate (they both run on the same machine). The server creates the pipe and the client connects fine, then the client sends a message to the server which works fine as well (the server can print the message as it should be). That's where it starts to go wrong, the server replies to the client and the read operation on the client returns (it's set to be blocking), but it doesn't actually supply any data and the number of bytes read is set to 0. An abbreviated copy of my code is shown.

The server creates the pipe with this:

CODE

CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, sPIPE_BUFFER, sPIPE_BUFFER, 10000, NULL);

connected = ConnectNamedPipe(PipeHandle, NULL);

if (!connected) {
    connected = (GetLastError() == ERROR_PIPE_CONNECTED);
}

And then handles the client like this:

CODE

OpResult = ReadFile(PipeHandle, ClientRequest, sPIPE_BUFFER, &BytesRead, NULL);

if (OpResult && (BytesRead > 0))
{
    strcpy(Reply, "Example Response");
    ReplySize = (UINT)strlen("Example Response") + 1;
 
    OpResult = WriteFile(PipeHandle, Reply, ReplySize, &Written, NULL);
}

ProcessRequest returns the reply to send in the cunningly named Reply. This appears to be working this far as I can display the message properly (it isn't shown here but Reply and the other buffers in the code are defined as

CODE

char Whatever[BUFFER_SIZE]
And are zeroed before use.

So as far as I know, all that works. Now onto the client. The client does

CODE

PipeHandle = CreateFile(PipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (PipeHandle == INVALID_HANDLE_VALUE)
{
    MB("Error", "Unable to open pipe");
    return EXIT_FAILURE;
}
    
DWORD open_mode = (PIPE_READMODE_MESSAGE | PIPE_WAIT);
if (!SetNamedPipeHandleState(PipeHandle, &open_mode, NULL, NULL))
{
    MB("Error", "Unable set pipe mode");
    return EXIT_FAILURE;
}

if (TransactNamedPipe(PipeHandle, Message, sPIPE_BUFFER, Buff, sPIPE_BUFFER, &Read, NULL) == 0)
{
    MB("Error", "Transaction Error");
    return EXIT_FAILURE;
}

This doesn't display any error messages, but Buff appears to be all null characters (presumably from it being zeroed before use) and Read is 0.

This has had me scratching my head for a while now, especially as it is nearly identical to the examples given on MSDN. Does anybody have any thoughts on this?
xwb (Programmer)
27 Dec 05 2:50
Possibly waiting for the buffer to be full before sending.  After writing, add

CODE

FlushFileBuffers (PipeHandle);
KempCGDR (Programmer) (OP)
28 Dec 05 12:03
I have tried this, and still nothing is received by the client. No matter how long the timeout period is set it still returns straight away with no data, which is impossible as far as I know what with it being set to blocking.

I find it really strange as the code to send to the server and for the server to send stuff back again is virtually identical and the server does receive the message from the client.

Also, TransactNamedPipe doesn't return an error, which it should do if no data is received (eg, if it times out).

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close