如何检测与命名管道断开连接的客户端? [英] How do I detect a client disconnected from a named pipe?

查看:372
本文介绍了如何检测与命名管道断开连接的客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我正在尝试围绕命名管道编写一个C#包装器,这样我就可以创建一个事件驱动的"管道服务器"。在我的申请中。我的想法是,我可以创建一个管道池,这些管道将位于后台并为任意数量的客户提供服务,在消息进入时引发事件并支持随意将数据推送到客户端。真正的双向管道。

我无法通过MSDN回答的一个问题是,在服务器端,我可以检测到客户端已与服务器断开连接。我所看到的是,如果我尝试在已经断开,关闭或断开的管道上运行命令,那么它将抛出异常。但是,我可以使用任何异步命令或事件来告诉我客户端何时断开连接? (除了让客户端向服务器发送消息说它正在断开连接。)

现在,这里的基本操作理论是创建一个包含NamedPipeServerStream对象的PipeServer类,定义该类的一些事件当客户连接或收到消息时,可以提出等等。这里有一个关于它现在如何工作的草图 - 因为我对此完全陌生,我真的不知道我在做什么。































< tr>































































< tr>



















< tr>
公共类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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆