基本信息
源码名称:C#编写的tcp通讯助手
源码大小:0.11M
文件格式:.rar
开发语言:C#
更新时间:2021-11-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
C#编写的tcp通讯助手

        public MyTcpServer(string IpAddress, int IpPort)
        {
            try
            {
                //当点击开始监听的时候 在服务器端创建一个负责监IP地址跟端口号的Socket
                Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(IpAddress);
                //创建端口号对象
                IPEndPoint point = new IPEndPoint(ip, IpPort);
                //监听
                socketWatch.Bind(point);
                socketWatch.Listen(10);
                //开始监听
                Thread th = new Thread(Listen);
                th.IsBackground = true;
                th.Start(socketWatch);
                this.Address = IpAddress;
                this.Port = IpPort;
                this.ClientCount = 0;
                this.WaitReceive = false;
            }
            catch
            { }
        }
        /// <summary>
        /// IP地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// IP端口号
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// 已连接的客户端数量,为0则没有连接
        /// </summary>
        public int ClientCount { get; set; }
        /// <summary>
        /// 是否在等待接收消息状态
        /// </summary>
        private bool WaitReceive { get; set; }
        /// <summary>
        /// 接收到的消息
        /// </summary>
        private string MyReceiveMsg { get; set; }
        /// <summary>
        /// 接收到的字节数组
        /// </summary>
        private byte[] MyReceiveBuffer { get; set; }

        /// <summary>
        /// 监听
        /// </summary>
        /// <param name="o">需要监听的Socket</param>
        private void Listen(object o)
        {
            Socket socketWatch = o as Socket;
            //等待客户端的连接 并且创建一个负责通信的Socket
            while (true)
            {
                try
                {
                    //负责跟客户端通信的Socket
                    CurrentSocket = socketWatch.Accept();
                    //将远程连接的客户端的IP地址和Socket存入集合中
                    dicSocket.Add(CurrentSocket.RemoteEndPoint.ToString(), CurrentSocket);
                    //将远程连接的客户端的IP地址和端口号存储下拉框中
                    //开启 一个新线程不停的接受客户端发送过来的消息
                    Thread th = new Thread(Recive);
                    th.IsBackground = true;
                    th.Start(CurrentSocket);
                    this.ClientCount = dicSocket.Count;
                }
                catch(Exception ex)
                {
                    Console.WriteLine("dddd");
                }
            }
        }
        /// <summary>
        /// 接收消息线程
        /// </summary>
        /// <param name="o">需要接收消息的Socket</param>
        private void Recive(object o)
        {
            Socket socketSend = o as Socket;
            while (true)
            {
                try
                {
                    //客户端连接成功后,服务器应该接受客户端发来的消息
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    //实际接受到的有效字节数
                    int r = socketSend.Receive(buffer);
                    if (r == 0) //等于0表示该客户端断开了连接
                    {
                        if (dicSocket.ContainsKey(socketSend.RemoteEndPoint.ToString()))
                         { dicSocket.Remove(socketSend.RemoteEndPoint.ToString()); } //移除已断开连接的元素
                        if (CurrentSocket.RemoteEndPoint.ToString().Equals(socketSend.RemoteEndPoint.ToString()))//如果当前活动soket是断开的soket,则将活动soket重新赋值
                        {
                            if (dicSocket.Count > 0) //还有客户端连接
                            {
                                CurrentSocket = dicSocket.First().Value;
                            }
                        }

                        this.ClientCount = dicSocket.Count; 
                        break;
                    }
                    string str = Encoding.GetEncoding("GB2312").GetString(buffer, 0, r);
                    CurrentSocket = socketSend; //将活动连接设为当前接收到消息的连接
                    if (this.WaitReceive) //判断是否在等待接收消息的状态,在则发送线程同步set,不在则忽略
                    {
                        this.WaitReceive = false;
                        this.MyReceiveMsg = str;
                        this.MyReceiveBuffer = buffer.Take(r).ToArray();
                        ReceiveStr.Set();
                    }
                    else
                    {
                        this.MyReceiveMsg = "";
                        this.MyReceiveBuffer = new byte[0];
                    }
                }
                catch (Exception ex)//表示客户端非正常断开连接
                {
                    if (dicSocket.ContainsKey(socketSend.RemoteEndPoint.ToString()))
                    { dicSocket.Remove(socketSend.RemoteEndPoint.ToString()); }
                    this.ClientCount = dicSocket.Count;
                    if (CurrentSocket.RemoteEndPoint.ToString().Equals(socketSend.RemoteEndPoint.ToString()))//如果当前活动soket是断开的soket,则将活动soket重新赋值
                    {
                        if (dicSocket.Count > 0) //还有客户端连接
                        {
                            CurrentSocket = dicSocket.First().Value;
                        }
                    }
                    
                    //if (this.WaitReceive) //判断是否在等待接收消息的状态,在则发送线程同步set,不在则忽略
                    //{
                    //    this.WaitReceive = false;
                    //    this.MyReceiveMsg = "";
                    //    ReceiveStr.Set();
                    //}

                }
            }
        }