这种客户端不需要做什么大的更改,还是同一服务端一客户端的一样,在服务端需要建立一个集合,保存连接上的客户端,发送信息的时候,只需要选择相对于的客户端则可以进行通信。

客户端

Thread th = new Thread(ServerListen);

        th.Start();

 List<TcpClient> clientlist = new List<TcpClient>();

        NetworkStream ns;

        StreamReader sr;

        StreamWriter sw;

        void ServerListen()

        {

            TcpListener listener = new  TcpListener(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));

            listener.Start();

            int i=0;

            while (true)

            {

                TcpClient tc = listener.AcceptTcpClient();

                clientlist.Add(tc);

                

                comOnline.Items.Add(++i);

                Thread reth = new Thread(Read);

                reth.Start(tc);

                

            }

        }

        void Read(object tc)

        {

            while (true)

            {

                ns = ((TcpClient)tc).GetStream();

                sr = new StreamReader(ns);

                string con = sr.ReadLine();

                if (con != null && con != "")

                {

                    txtOld.Text += DateTime.Now + "\r\n";

                    txtOld.Text += "客户端" + (clientlist.IndexOf((TcpClient)tc)  +1) + ": " + con + "\r\n";

          

                }

            }

        }

发送的时候

 sw=newStreamWriter(clientlist[comOnline.SelectedIndex].GetStream());

       sw.WriteLine(txtNew.Text);

       sw.Flush();

       txtOld.Text += DateTime.Now + "\r\n";

       txtOld.Text += txtNew.Text+"\r\n";

       txtNew.Clear();

客户端

     Thread th = new Thread(ClientConn);

            th.Start();

     NetworkStream ns;

            StreamReader sr;

            StreamWriter sw;

            void ClientConn()

        {

            TcpClienttc = new TcpClient(textBox1.Text, int.Parse(textBox2.Text));

            ns= tc.GetStream();

            sr=new StreamReader(ns);

            while (true)

            {

                string con = sr.ReadLine();

                if (con != "")

                {

                    txtOld.Text += DateTime.Now + "\r\n";

                    txtOld.Text += "服务端说: " + con + "\r\n";

                }

            }

        }

发送时:

            sw = new StreamWriter(ns);

            sw.WriteLine(txtNew.Text);

            sw.Flush();

            txtOld.Text += DateTime.Now + "\r\n";

            txtOld.Text += txtNew.Text + "\r\n";

         txtNew.Clear();