操作系统分配资源的最小单位是线程 usingSystem.Threading; //开启线程用来监听 Thread th = new Thread(ListenClint);
    1. //设置为后台运行
    2. <font style="color:#E30000;"> th.IsBackground = true;</font>
    th.Start();

    //如果要传入参数可以写为void ListenClint(object o),参数只能写为object

    void ListenClint() { while (true) { //有客户连接后,返回一个客户端实例(没有客户端时,程序会一直停在这里)
    1. <font style="color:#2B91AF;">Socket</font> client = server.Accept();
    2. <font style="color:#008000;">//得到连接进来的ip和端口号</font>
    3. <font style="color:#2B91AF;">MessageBox</font>.Show(client.RemoteEndPoint.ToString() + <font style="color:#A31515;">"有客户端连接了"</font>);
    4. }
    5. }

    多线程 - 图1多线程 - 图2