Program.cs 1.0 KB
Newer Older
L
linxinfa 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
using System;
using System.Threading;


class Program
{
    static void Main(string[] args)
    {
        //建立一个线程,用来监听管道数据
        Program p = new Program();
        Thread nonParameterThread = new Thread(new ThreadStart(p.StartServerNamedPipe));
        nonParameterThread.Start();

        Console.WriteLine("我是服务端,你可以通过客户端给我发消息了");
    }

    /// <summary>
    /// 启动命名管道
    /// </summary>
    public void StartServerNamedPipe()
    {
        m_serverNamedPipe = new NamedPipeServer("test_named_pipe");
        m_serverNamedPipe.Readed += OnPipeReadMsg;
        m_serverNamedPipe.Start();
    }

    /// <summary>
    /// 收到管道数据
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public string OnPipeReadMsg(string arg)
    {
        Console.WriteLine("客户端发过来的数据:\r\n{0}\r\n", arg);
        

        return string.Format(" 字符串长度:{0} ", arg.Length);
    }

    private NamedPipeServer m_serverNamedPipe;
}