Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和C#挂起之间的命名管道双工通信_C#_Python_Named Pipes_Pywin32 - Fatal编程技术网

Python和C#挂起之间的命名管道双工通信

Python和C#挂起之间的命名管道双工通信,c#,python,named-pipes,pywin32,C#,Python,Named Pipes,Pywin32,我正在python(管道服务器)和C#(管道客户端)之间实现一个命名的管道双工通信。如果管道服务器从管道中读取数据,然后向管道中写入数据,而管道客户机则从管道中读取数据,反之亦然,那么它可以工作。但是,当我更改顺序,使管道服务器先写后读,管道客户端则反之亦然时,程序在读取时挂起(ReadLine()),没有任何错误或异常。我实现了异常处理,但为了更好地概述,这里省略了它 以下代码正常工作: # Python 2 pipe server (ReadThenWrite) pipe_handle =

我正在python(管道服务器)和C#(管道客户端)之间实现一个命名的管道双工通信。如果管道服务器从管道中读取数据,然后向管道中写入数据,而管道客户机则从管道中读取数据,反之亦然,那么它可以工作。但是,当我更改顺序,使管道服务器先写后读,管道客户端则反之亦然时,程序在读取时挂起(
ReadLine()
),没有任何错误或异常。我实现了异常处理,但为了更好地概述,这里省略了它

以下代码正常工作:

# Python 2 pipe server (ReadThenWrite)
pipe_handle = win32pipe.CreateNamedPipe(pipeName, win32pipe.PIPE_ACCESS_DUPLEX, ...)
win32pipe.ConnectNamedPipe(pipe_handle, None)

(_, read_message) = win32file.ReadFile(pipe_handle, 1000)
print('Received: ' + read_message)

win32file.WriteFile(pipe_handle, 'server')

win32file.FlushFileBuffers(pipe_handle)
win32pipe.DisconnectNamedPipe(pipe_handle)
win32file.CloseHandle(pipe_handle)
在下面的代码中,只有读和写的顺序被交换,其他的都没有改变。奇怪的是,C#应用程序在阅读时挂起

# Python 2 pipe server (WriteThenRead)
pipe_handle = win32pipe.CreateNamedPipe(pipeName, win32pipe.PIPE_ACCESS_DUPLEX, ...)
win32pipe.ConnectNamedPipe(pipe_handle, None)

win32file.WriteFile(pipe_handle, 'server')

(_, read_message) = win32file.ReadFile(pipe_handle, 1000)
print('Received: ' + read_message)

win32file.FlushFileBuffers(pipe_handle)
win32pipe.DisconnectNamedPipe(pipe_handle)
win32file.CloseHandle(pipe_handle)
如果在管道服务器(WriteThenRead)中删除了管道读取,则管道客户端不会挂起并读取消息

作为一种解决方法,管道服务器在写入之后(分别在读取之前)再次创建管道,管道客户端在写入之前(分别在读取之后)再次连接到管道。但是,它不被用作双联管

为什么会发生这种行为?如何解决这个问题

任何帮助都将不胜感激,谢谢

# Python 2 pipe server (WriteThenRead)
pipe_handle = win32pipe.CreateNamedPipe(pipeName, win32pipe.PIPE_ACCESS_DUPLEX, ...)
win32pipe.ConnectNamedPipe(pipe_handle, None)

win32file.WriteFile(pipe_handle, 'server')

(_, read_message) = win32file.ReadFile(pipe_handle, 1000)
print('Received: ' + read_message)

win32file.FlushFileBuffers(pipe_handle)
win32pipe.DisconnectNamedPipe(pipe_handle)
win32file.CloseHandle(pipe_handle)
// C# pipe client (ReadThenWrite)
NamedPipeClientStream pipeClient = NamedPipeClientStream(".", pipeName, PipeDirection.InOut)
pipeClient.Connect();

StreamReader sr = new StreamReader(pipeClient);
string temp = sr.ReadLine(); // here it hangs
Console.WriteLine("Received: " + temp);

StreamWriter sw = new StreamWriter(pipeClient);
sw.AutoFlush = true;
sw.WriteLine("client");