How to work with named pipes (C++ server , C# client)

偶尔善良 提交于 2019-11-30 23:53:33

ReadLine hangs because it is waiting for a newline, which your test message doesn't include.

If you want the server to send messages continuously just put a loop round the WriteFile call. You don't need to connect more than once. Similarly in the client, put the loop around ReadLine.

If each message consists of text terminated by a newline then that should suffice, but if you really want the pipe client to work in message mode you need to call:

pipeClient.ReadMode = PipeTransmissionMode.Message;

However, I doubt this would interact well with a StreamReader. Instead, you should read single messages using pipeClient.Read.

Update

To answer your new question:

On the server, once the client has connected enter a loop in which:

  • The server does a read from the client. This will block until the client requests a frame.
  • The server sends a frame.

On the client, once it has connected to the server enter a loop in which:

  • The client sends a "please send a frame" message.
  • The client does a read from the server to get the frame.
  • The client displays the frame.

I wouldn't use a message mode pipe. If the frames are fixed in size then the client knows how much data to read from the server. Otherwise, precede the frame with a uint containing its length.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!