C# TCP《六》TcpListener 接收多个客户端连接

//---------- 服务端 TcpListener--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApplication12
{
    class Program
    {
        private static byte[] result = new byte[1024];
        private const int port = 8888;
        private static string IpStr = "127.0.0.1";
        private static TcpListener listener;
        public static List<TcpClient> clients = new List<TcpClient>();

        static void Main(string[] args)
        {
            IPAddress ip = IPAddress.Parse(IpStr);
            IPEndPoint ip_end_point = new IPEndPoint(ip, port);
            listener = new TcpListener(ip_end_point);
            listener.Start();
            Console.WriteLine("启动监听成功");

            //异步接收 递归循环接收多个客户端
            listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpclient), listener);
            Console.ReadKey();
        }

        private static void DoAcceptTcpclient(IAsyncResult State)
        {
            /*                   */
            /* 处理多个客户端接入*/
            /*                   */
            TcpListener listener = (TcpListener)State.AsyncState;
            
            TcpClient client = listener.EndAcceptTcpClient(State);

            clients.Add(client);

            Console.WriteLine("\n收到新客户端:{0}", client.Client.RemoteEndPoint.ToString());
            //开启线程用来持续收来自客户端的数据
            Thread myThread = new Thread(new ParameterizedThreadStart(printReceiveMsg));
            myThread.Start(client);

            listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpclient), listener);
        }

        private static void printReceiveMsg(object reciveClient)
        {
            /*                   */
            /* 用来打印接收的消息*/
            /*                   */
            TcpClient client = reciveClient as TcpClient;
            if (client == null)
            {
                Console.WriteLine("client error");
                return;
            }
            while (true)
            {
                try
                {
                    NetworkStream stream = client.GetStream();                    
                    int num = stream.Read(result, 0, result.Length); //将数据读到result中,并返回字符长度                  
                    if (num != 0)
                    {
                        string str = Encoding.UTF8.GetString(result, 0, num);//把字节数组中流存储的数据以字符串方式赋值给str
                        //在服务器显示收到的数据
                        Console.WriteLine("From: " + client.Client.RemoteEndPoint.ToString() + " : " + str);


                        //服务器收到消息后并会给客户端一个消息。
                        string msg = "服务器已收到您的消息[" + str + "]" ;
                        result = Encoding.UTF8.GetBytes(msg);
                        stream = client.GetStream();
                        stream.Write(result, 0, result.Length);
                        stream.Flush();
                    }
                    else
                    {   //这里需要注意 当num=0时表明客户端已经断开连接,需要结束循环,不然会死循环一直卡住
                        Console.WriteLine("客户端关闭");
                        break;
                    }
                }
                catch (Exception e)
                {
                    clients.Remove(client);
                    Console.WriteLine("error:" + e.ToString());
                    break;
                }

            }

        }
    }
}

//--------- TcpClient 端 -----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ConsoleApplication16
{
    class Program
    {
        public static byte[] data = new byte[1024];
        public static void printReceiveMsg(object Obj_tcpClient)
        {
            TcpClient tcpClient = (TcpClient)Obj_tcpClient;
            NetworkStream stream = tcpClient.GetStream();

            while (true)
            {
                if (stream.DataAvailable)
                {
                    int length = stream.Read(data, 0, data.Length);
                    string receiveMsg = Encoding.UTF8.GetString(data, 0, length);
                    Console.WriteLine("\n接收服务端发来的数据: " + receiveMsg);
                    Console.Write("请输入您要发送的数据: ");
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("这是 TcpClient 端. ");
            //声明一个客户端
            TcpClient tcpClient = new TcpClient("127.0.0.1",8888);
            //声明一个流用来写数据
            NetworkStream stream = tcpClient.GetStream();

            //通过线程循环接收
            Thread T = new Thread(new ParameterizedThreadStart(printReceiveMsg));
            T.Start(tcpClient);

            while (true)
            {
                Console.Write("请输入您要发送的数据: ");
                string msg = Console.ReadLine();

                byte[] data = Encoding.UTF8.GetBytes(msg);
                stream.Write(data, 0, data.Length);
            }
        }
    }
}

 

 

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜕变之痛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值