Click here to Skip to main content
15,507,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am implementing a named pipe duplex communication between python (pipe server) and C# (pipe client). If the pipe server reads from pipe and then writes to it and the pipe client does so vice versa, it works. However, when I change the order so that pipe server writes then reads and the pipe client does so vice versa, the program hangs at reading (ReadLine()) without any error or exception. I implement exception handling, but omit it here in favour of a better overview.

What I have tried:

c# Code
using (NamedPipeClientStream pipeClient =
           new NamedPipeClientStream(".", "CSServer", PipeDirection.InOut))

                // Connect to the pipe or wait until the pipe is available.
                Console.Write("Attempting to connect to pipe...");

                Console.WriteLine("Connected to pipe.");
                Console.WriteLine("There are currently {0} pipe server instances open.",
                StreamWriter sw = new StreamWriter(pipeClient);
                StreamReader sr = new StreamReader(pipeClient);
                while (true)
                        sw.AutoFlush = true;
                        sw.WriteLine("Hello from c#");

                        //  Console.WriteLine("Received: " + temp);
                        string temp = sr.ReadLine();
                        Console.WriteLine("Received from python : {0}", temp);
                    catch (EndOfStreamException)
                        break;                    // When client disconnects

// Console.Write("Press Enter to continue...");



python Code
import win32file
import win32pipe
import time

# Python 2 pipe server (ReadThenWrite)
pipe_handle = win32pipe.CreateNamedPipe(
        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
        1, 65536, 65536,
win32pipe.ConnectNamedPipe(pipe_handle, None)
while True:
    (_, read_message) = win32file.ReadFile(pipe_handle, 1000)
    print('Received from c#: ' + read_message.decode('utf-8'))

    win32file.WriteFile(pipe_handle, 'Hello from Python'.encode())

   # win32pipe.DisconnectNamedPipe(pipe_handle)


I am able read the in python console but not in c# console file

i need to read and write the data within 2 sec of time
please help me any mistake i have did in my code
Updated 8-Aug-22 1:03am

1 solution

The issue is that the C# code is trying to read a complete line of text, so it is waiting for a newline character in the stream. So change the python code to:
ret, length = win32file.WriteFile(pipe_handle, 'Hello from Python\n'.encode()) # add newline character at end
print(F'{ret = }, {length = } from WriteFile') # added for debugging

For a fully operational application you may need to use a different read method, and manage the buffering and string building yourself.
Share this answer
HelpMewithCode 8-Aug-22 6:19am    
Thanks Richard MacCutchan

Its work to get Data to the c# console .

But i need to update read and write within 2 sec
How it will work
both the side i have given while loop and time frame 2 sec
Anything need to changed ?
Richard MacCutchan 8-Aug-22 6:24am    
You cannot guarantee the times as they are completely dependent on external factors. The best you can do is ensure there are no delays (e.g time.sleep(2)) in the code.
HelpMewithCode 8-Aug-22 6:50am    
oky I have remove the Time.sleep
but i tried with forloop that i can atleast read and write 5 times

but its only printing once time
Richard MacCutchan 8-Aug-22 8:05am    
The existing code was working fine when I tested it, so what have you changed to break it?
HelpMewithCode 8-Aug-22 8:14am    
No need Thank you its working fine
Thanks Richard MacCutchan

I need one more Its possible to python method using Pipes from c# and execute them
I dont so asked you , if possible please let me know how i will do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900