如何检测与命名管道断开连接的客户端? [英] How do I detect a client disconnected from a named pipe?
问题描述
我无法通过MSDN回答的一个问题是,在服务器端,我可以检测到客户端已与服务器断开连接。我所看到的是,如果我尝试在已经断开,关闭或断开的管道上运行命令,那么它将抛出异常。但是,我可以使用任何异步命令或事件来告诉我客户端何时断开连接? (除了让客户端向服务器发送消息说它正在断开连接。)
现在,这里的基本操作理论是创建一个包含NamedPipeServerStream对象的PipeServer类,定义该类的一些事件当客户连接或收到消息时,可以提出等等。这里有一个关于它现在如何工作的草图 - 因为我对此完全陌生,我真的不知道我在做什么。
公共类PipeServer |
{ |
public NamedPipeServerStream oPipe; |
IAsyncResult ConnectRequest; |
private byte [] oBuffer; |
public PipeServer() |
{ |
oPipe = < font style ="color:blue"> new NamedPipeServerStream(" pipetest",PipeDirection.InOut); |
oBuffer = new byte [4096]; |
ConnectRequest = oPipe 。BeginWaitForConnection(ClientConnected,null); |
} |
private void ClientConnected() |
{ |
if(ConnectRequest!= null) |
{ |
oPipe.EndWaitForConnection(ConnectRequest); |
ConnectRequest = null ; |
//提升连接事件 |
< font style ="color:red"> ConnectRequest = oPipe 的BeginRead(oBuffer,0,4096,ClientMessage,NULL);。 |
} |
} |
private void ClientMessage() |
{ |
if(ConnectRequest!= null) |
{ |
oPipe.EndRead(ConnectRequest); |
string message = oBuffer 。ToString(); |
//用"message"做一些事情。 |
ConnectRequest = oPipe 。BeginRead(oBuffer,0,4096,ClientMessage,null); |
} |
} |
public void SendMessageToClient(string message) |
{ |
if(ConnectRequest!= null) |
{ |
oPipe.EndRead(ConnectRequest); |
ConnectRequest = null ; |
} |
使用(StreamWriter sw = new StreamWriter(oPipe)) |
{ |
sw.AutoFlush = true ; |
sw.WriteLine(message); |
oPipe.WaitForPipeDrain(); |
} |
ConnectRequest = oPipe 。BeginRead(oBuffer,0,4096,ClientMessage , 空值); |
} | } |
此外,值得一提的是上面的代码远未完成。这只是为了提供一个关于这门课程的工作方式的想法。不过我还在写它。
Hi there. I'm trying to write a C# wrapper around a named pipe so I can create an event-driven "Pipe Server" in my application. The idea is that I can create a pool of pipes that'll sit in the background and service any number of clients, raising events when messages come in and also supporting pushing data down to the clients at will. True bi-directional piping.
The one question I can't seem to answer through MSDN is how, on the server end, I can detect that a client has disconnected from the server. All I see is that if I attempt to run a command on a pipe that has been broken, closed or disconnected, it'll throw an exception then. But is there any async command or event I can tap into that will tell me when a client disconnects? (Aside from making the client send a message to the server saying it's disconnecting.)
Right now, the basic theory of operation here is to create a PipeServer class that contains a NamedPipeServerStream object, defines some events that the class can raise when a client connects or a message is received, etc. Here's a rough sketch of how this works right now - since I'm totally new to this, I don't really know what I'm doing so much.
public class PipeServer |
{ |
public NamedPipeServerStream oPipe; |
IAsyncResult ConnectRequest; |
private byte[] oBuffer; |
public PipeServer() |
{ |
oPipe = new NamedPipeServerStream("pipetest", PipeDirection.InOut); |
oBuffer = new byte[4096]; |
ConnectRequest = oPipe.BeginWaitForConnection(ClientConnected, null); |
} |
private void ClientConnected() |
{ |
if( ConnectRequest != null ) |
{ |
oPipe.EndWaitForConnection(ConnectRequest); |
ConnectRequest = null; |
// raise connect event |
ConnectRequest = oPipe.BeginRead(oBuffer, 0, 4096, ClientMessage, null); |
} |
} |
private void ClientMessage() |
{ |
if( ConnectRequest != null ) |
{ |
oPipe.EndRead(ConnectRequest); |
string message = oBuffer.ToString(); |
// Do something with "message". |
ConnectRequest = oPipe.BeginRead(oBuffer, 0, 4096, ClientMessage, null); |
} |
} |
public void SendMessageToClient(string message) |
{ |
if( ConnectRequest != null ) |
{ |
oPipe.EndRead(ConnectRequest); |
ConnectRequest = null; |
} |
using( StreamWriter sw = new StreamWriter(oPipe) ) |
{ |
sw.AutoFlush = true; |
sw.WriteLine(message); |
oPipe.WaitForPipeDrain(); |
} |
ConnectRequest = oPipe.BeginRead(oBuffer, 0, 4096, ClientMessage, null); |
} |
} |
Also, it's worth mentioning that the above code is far from complete. It's just meant to provide an idea of the kind of work this class is meant to do. I'm still writing it up, though.
这篇关于如何检测与命名管道断开连接的客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!