简体   繁体   English

命名为 Pipe 写入时挂起

[英]Named Pipe hangs on write

I'm making a multi-client Named Pipe server.我正在制作一个名为 Pipe 服务器的多客户端。 The clients are connecting to the server correctly, but whenever I attempt to write to the pipe through the server or the client, the code hangs on the write method.客户端正确连接到服务器,但每当我尝试通过服务器或客户端写入 pipe 时,代码会挂在 write 方法上。 Any reason as to why the write methods are getting stuck?关于写入方法卡住的任何原因?

Server code:服务器代码:

public void ListenForConnections()
{
    Thread startListening = new Thread(AcceptConnections);
    startListening.Start(PipeName);
}

public static void AcceptConnections(object ServerPipeName)
{
    while (true)
    {
        try
        {
            NamedPipeServerStream pipeServer = 
                 new NamedPipeServerStream(ServerPipeName.ToString(),
                                           PipeDirection.InOut, 
                                           NamedPipeServerStream.MaxAllowedServerInstances,
                                           PipeTransmissionMode.Message);

            pipeServer.WaitForConnection();
            pipeServer.ReadMode = PipeTransmissionMode.Message;
            //A client has connected
            Pipes.Add(pipeServer);
            index++;

            Thread Poll = new Thread(PollPipe);
            Poll.Start(pipeServer);
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }

    public static void PollPipe(Object Pipe)
    {
        byte[] bytes = new byte[1024];
        NamedPipeServerStream PipeStream = (NamedPipeServerStream)Pipe;
        MemoryStream ms = new MemoryStream();
        do 
        {
            ms.Write(bytes, 0, PipeStream.Read(bytes, 0, bytes.Length));
        } while (!PipeStream.IsMessageComplete);   
    }

    public void BroadcastObject(GlassSquidObject obj)
    {
        long length = 0;
        byte[] bytes;

        SoapFormatter formatter = new SoapFormatter();

        using (MemoryStream ws = new MemoryStream())
        {
            formatter.Serialize(ws, obj);
            length = ws.Length;
            bytes = ws.GetBuffer();
        }

        for (int i = 0; i < Pipes.Count; i++)
        {
            Pipes[i].Write(bytes, 0, bytes.Length);
        }
    }
}

This is the code for my client:这是我的客户的代码:

public bool ConnectToPipe()
{
    if (String.IsNullOrWhiteSpace(PipeName))
        return false;

    PipeClient = new NamedPipeClientStream(Address, PipeName, PipeDirection.InOut);

    try
    {
        PipeClient.Connect(5000);

        Thread readThread = new Thread(PollPipe);
        readThread.Start(PipeClient);
    }
    catch (Exception ex)
    {
        return false;
    }

    return true;
}

public bool WriteObject(GlassSquidObject obj)
{
    long length = 0;
    byte[] bytes;

    try
    {
        SoapFormatter formatter = new SoapFormatter();

        using (MemoryStream ws = new MemoryStream())
        {
            formatter.Serialize(ws, obj);
            length = ws.Length;
            bytes = ws.GetBuffer();
        }

        PipeClient.Write(bytes, 0, bytes.Length);
    }
    catch (Exception ex)
    {
        return false;
    }

    return true;
}

public static void PollPipe(Object Pipe)
{
    byte[] bytes = new byte[1024];
    NamedPipeClientStream PipeStream = (NamedPipeClientStream)Pipe;
    PipeStream.ReadMode = PipeTransmissionMode.Message;
    MemoryStream ms = new MemoryStream();
    do
    {
        ms.Write(bytes, 0, PipeStream.Read(bytes, 0, bytes.Length));
    } while (!PipeStream.IsMessageComplete);     
}

I'm still not sure what the write was waiting for, but I found a solution/workaround to my problem. 我仍然不确定写作等待什么,但我找到了解决方案/解决方法来解决我的问题。 By setting the PipeOptions in the NamedPipe constructors to Asynchronous, the read/writes completed succesfully! 通过将NamedPipe构造函数中的PipeOptions设置为Asynchronous,读取/写入成功完成!

I was also facing this problem with the following situation:在以下情况下,我也遇到了这个问题:

  1. The server writes to the pipe服务器写入pipe
  2. After step 1, the client reads the pipe, and right afterwards writes a response to the pipe在第 1 步之后,客户端读取 pipe,然后立即向 pipe 写入响应
  3. The server tries to write again to the pipe without reading what was previously written by the client.服务器尝试再次写入 pipe ,而不读取客户端之前写入的内容。

I solved it by reading the pipe before each write call, and it solved the problem for me.我通过在每次write调用之前读取 pipe 来解决它,它为我解决了这个问题。

I'm not entirely sure, but I think this may be caused to prevent data loss, and the pipe acts like it is "busy", waiting to be read.我不完全确定,但我认为这可能是为了防止数据丢失,pipe 表现得像“忙”,等待读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM