Exemplo n.º 1
0
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sokConnection">用来接收的会话端</param>
        private void ReceiveData(object sokConnection)
        {
            TCPSession tCPSession    = sokConnection as TCPSession;
            Socket     SocketSession = tCPSession.Socket;

            tCPSession.IsConnected = true;
            while (tCPSession.IsConnected)
            {
                try
                {
                    string ipEndPort = SocketSession.RemoteEndPoint.ToString();
                    // 定义一个2M的缓存区;
                    byte[] arrMsgRec = new byte[1024 * 1024 * 2];
                    // 将接受到的数据存入到输入  arrMsgRec中;
                    int length = -1;
                    try
                    {
                        length = SocketSession.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
                    }
                    catch                                          //一旦出现错误,立马断开连接
                    {
                        tCPSession.IsConnected = false;
                        // 从通信线程集合中删除被中断连接的通信线程对象;
                        string keystr = SocketSession.RemoteEndPoint.ToString();
                        //删除客户端字典中该socket
                        Dic_ClientSession.Remove(keystr);
                        //关闭线程
                        Dic_ClientThread[keystr].Abort();
                        //删除字典中该线程
                        Dic_ClientThread.Remove(keystr);
                        tCPSession    = null;
                        SocketSession = null;
                        break;
                    }
                    //定义MD5pwd检验是不是该用户
                    byte[] md5pwd = new byte[16];
                    Array.Copy(arrMsgRec, 0, md5pwd, 0, 16);

                    if (!Enumerable.SequenceEqual(md5pwd, UseMd5(DateTime.Now.Hour.ToString())))
                    {
                        break;
                    }
                    //定义存储具体数据的byte数组
                    byte[] buf = new byte[length - 16];

                    //将缓冲区中的数据写入byte数组中
                    Array.Copy(arrMsgRec, 16, buf, 0, length - 16);

                    //获得byte数组表示数据的字符串
                    string strReceived = Encoding.UTF8.GetString(buf);
                    msg = ipEndPort + "-" + strReceived;
                }
                catch (Exception)
                {
                }
                Thread.Sleep(100);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送数据给指定的客户端
        /// </summary>
        /// <param name="_endPoint">客户端套接字</param>
        /// <param name="_buf">发送的数组</param>
        /// <returns>是否发送成功</returns>
        public bool SendData(byte i, string _endPoint, byte[] _buf)
        {
            List <byte> buflist = new List <byte>();

            buflist.Add(i);
            buflist.AddRange(_buf);
            byte[]     buf = buflist.ToArray();
            TCPSession myT = new TCPSession();

            if (Dic_ClientSession.TryGetValue(_endPoint, out myT))
            {
                myT.Send(buf);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 监听
 /// </summary>
 private void ListenConnecting()
 {
     while (Flag_Listen)                            // 持续不断的监听客户端的连接请求;
     {
         Socket sokConnection = TCPSocket.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字;
                                                    // 将与客户端连接的 套接字 对象添加到集合中;
         string     str_EndPoint = sokConnection.RemoteEndPoint.ToString();
         TCPSession myTCPSession = new TCPSession()
         {
             Socket = sokConnection
         };
         //创建线程接收数据
         Thread th_ReceiveData = new Thread(ReceiveData);
         th_ReceiveData.IsBackground = true;
         th_ReceiveData.Start(myTCPSession);
         //把线程及客户连接加入字典
         Dic_ClientSession.Add(str_EndPoint, myTCPSession);
         Dic_ClientThread.Add(str_EndPoint, th_ReceiveData);
         Thread.Sleep(200);
     }
 }