The named pipe is hanging on the record

I am making a server with multiple Named Pipe clients. The clients are connecting to the server correctly, but whenever I try to write to the channel via the server or client, the code hangs in the write method. Any reason why 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);     
}

      

+3


source to share


1 answer


I'm still not sure what the post was expecting, but I found a solution / workaround for my problem. By setting PipeOptions in the NamedPipe constructors to Asynchronous, read / write completed successfully!



+5


source







All Articles