使用命名管道实现进程间通信(下)

1、服务端C#

private const String MY_PIPE_NAME = "__MY__PIPE__TEST__";
private const int BUFFER_SIZE = 1024;
NamedPipeServerStream pipe;
StreamWriter writer;
String msg = "Message is comming";
 
private void PipeCreate()
{
    if (pipe != null && pipe.IsConnected)
    {
        pipe.Close();
    }
 
    pipe = new NamedPipeServerStream(MY_PIPE_NAME, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None);
     
 
    if (pipe == null)
    {
        textBox1.Text = textBox1.Text + "Pipe is null";
        return;
    }
 
    if (pipe.IsConnected)
    {
        textBox1.Text = textBox1.Text + "Pipe is already connected";
        return;
    }
 
    pipe.WaitForConnection();
 
    textBox1.Text = textBox1.Text + "Pipe is ready to connect\r\n";
}
 
private void PipeWrite()
{
    if (pipe!=null && pipe.IsConnected)
    {
        if(writer==null)
        {
            //关闭BOM(Byte Order Mark 0xfeff)
            UnicodeEncoding unicodeWithoutBom = new System.Text.UnicodeEncoding(false, false);
            writer = new StreamWriter(pipe, unicodeWithoutBom);
            //写完后直接flush,会阻塞
            writer.AutoFlush = true;
        }
        writer.Write(msg);
 
        textBox1.Text = textBox1.Text + "Pipe message sent \r\n";
    }
    else
    {
        textBox1.Text = textBox1.Text + "Pipe is not connected \r\n";
    }
}
 
private void PipeClose()
{
    if (pipe!=null && pipe.IsConnected)
    {
        writer.Close();
        pipe.Close();
 
        writer = null;
        pipe = null;
    }
}

2、客户端C#

private const String MY_PIPE_NAME =  "__MY__PIPE__TEST__";
private const int BUFFER_SIZE = 1024;
NamedPipeClientStream pipe;
StreamReader reader;
 
private void PipeConnect()
{
    if (pipe != null && pipe.IsConnected)
    {
        pipe.Close();
    }
    pipe = new NamedPipeClientStream(".", MY_PIPE_NAME, PipeDirection.InOut);
 
    if (pipe != null)
    {
        if (!pipe.IsConnected)
        {
            pipe.Connect();
            textBox1.Text = textBox1.Text + "\r\npipe is connected";
        }
        else
        {
            textBox1.Text = textBox1.Text + "\r\npipe is already connected";
        }
    }
    else
    {
        textBox1.Text = textBox1.Text + "\r\npipe is null";
    }
 
     
}
 
private void PipeRead()
{
    if (pipe.IsConnected)
    {
        if (reader == null)
        {
            reader = new StreamReader(pipe, Encoding.Unicode);
        }
 
        char[] buffer = new char[BUFFER_SIZE];
        int byteRead = reader.Read(buffer, 0, BUFFER_SIZE);
        String msgTxt = new String(buffer, 0, byteRead);
        textBox1.Text = textBox1.Text + "\r\nPipe msg received: " + msgTxt;
    }
    else
    {
        textBox1.Text = textBox1.Text + "\r\nPipe is not connected";
    }
}
 
private void PipeClose()
{
    if (reader != null)
    {
        reader.Close();
        reader = null;
    }
    if (pipe != null && pipe.IsConnected)
    {
        pipe.Close();
    }
 
    textBox1.Text = textBox1.Text + "\r\npipe is closed";
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*