コード例 #1
0
        private void ConnectionThread(object clientsocket) //接收数据
        {
            Socket connection = (Socket)clientsocket;

            byte[]    recBufff = new byte[BUFFSIZE];
            TcpPocket pocket   = new TcpPocket();

            while (connection.Connected)
            {
                try
                {
                    int    receiveNumber = connection.Receive(recBufff);
                    string recStr        = Encoding.UTF8.GetString(recBufff, 0, receiveNumber);
                    if (!pocket.AnalyseCommand(recStr))
                    {
                        continue;
                    }
                    if (pocket.Command == SocketCommand.ActConnected)
                    {
                        if (pocket.Result == SocketCommand.True)
                        {
                            Event_RecieveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, "服务器连接成功!"));
                            // _time.Start();
                        }
                    }
                    else if (pocket.Command == SocketCommand.ActAbortSocket)//退出
                    {
                        if (Event_RecieveMsg != null)
                        {
                            Event_RecieveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, "断开连接"));
                        }
                        break;                                       //跳出当前循环,线程退出
                    }
                    else if (pocket.Command == SocketCommand.ActMsg) //消息
                    {
                        if (Event_RecieveMsg != null)
                        {
                            Event_RecieveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, pocket.ConStructShowMsg()));
                        }
                    }
                }
                catch (Exception e)
                {
                    break;
                }
                Thread.Sleep(100);
            }
            //退出
            if (clientSocket != null && clientSocket.Connected)
            {
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
            }
            clientSocket = null;
            workThread   = null;
        }
コード例 #2
0
        private void ConnectLoopThread(object clientsocket)//接收远端socket数据处理函数
        {
            Socket     Connection = (Socket)clientsocket;
            IPEndPoint ip         = (IPEndPoint)Connection.RemoteEndPoint;

            // bool registerFlag = false;   //注册标志
            byte[]    result = new byte[BUFFSIZE];
            TcpPocket pocket = new TcpPocket();

            try
            {
                while (true)
                {
                    int receiveNumber = Connection.Receive(result);//接收数据
                    if (receiveNumber > 0)
                    {
                        string recStr = Encoding.UTF8.GetString(result, 0, receiveNumber);
                        if (!pocket.AnalyseCommand(recStr))
                        {
                            continue;
                        }

                        if (pocket.Command == SocketCommand.ActConnected)//连接
                        {
                            if (Event_ReceiveMsg != null)
                            {
                                Event_ReceiveMsg(this, new ReceiveArgs(pocket.Command, SocketCommand.True, pocket.Identify, pocket.ConStructShowMsg()));
                            }
                            Connection.Send(Encoding.UTF8.GetBytes(pocket.ConStructCommand(SocketCommand.ActConnected, SocketCommand.True, SocketCommand.IdfServer, "欢迎")));
                        }
                        else if (pocket.Command == SocketCommand.ActAbortSocket)//退出
                        {
                            if (Event_ReceiveMsg != null)
                            {
                                Event_ReceiveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, pocket.ConStructShowMsg()));
                            }
                            break;
                        }
                        else if (pocket.Command == SocketCommand.ActALive)//心跳
                        {
                            RemoteSocketLiveTime[ip].Restart();
                        }
                        //else if (recBuf[0] == SocketCommand.Register)//注册
                        //{
                        //    registerFlag = RemoteClientRegister(ip, recBuf[1]);
                        //    Connection.Send(Encoding.UTF8.GetBytes(SocketCommand.Register + "_"+ registerFlag.ToString()));
                        //}
                        //else if (recBuf[0] == SocketCommand.MSG)//消息
                        //{
                        //    if (true == registerFlag)
                        //    {
                        //        for (index = 1; index < recBuf.Length; index++)
                        //        {
                        //            receiveMsg += recBuf[index];
                        //            if ((index + 1) != recBuf.Length)
                        //            {
                        //                receiveMsg += "_";
                        //            }
                        //        }
                        //        RemoteClientMsg(receiveMsg);
                        //        if (Event_ReceiveMsg != null)
                        //        {
                        //            Event_ReceiveMsg(this, new ReceiveArgs(receiveMsg));
                        //        }
                        //    }
                        //}
                        else if (pocket.Command == SocketCommand.ActMsg)//消息
                        {
                            SendAnotherSocketMessage(ip, pocket.ConStructCommand());
                            if (Event_ReceiveMsg != null)
                            {//触发外部的事件
                                Event_ReceiveMsg(this, new ReceiveArgs(pocket.Command, pocket.Result, pocket.Identify, pocket.ConStructShowMsg()));
                            }
                        }
                    }
                    if (RemoteSocketLiveTime[ip].ElapsedMilliseconds > SocketCommand.ServerHeartbeatClock)
                    {//心跳机制
                        break;
                    }
                    Thread.Sleep(10);
                }
            }
            catch (Exception ex)
            {
            }
            //退出
            threadBuf[ip] = null;
            if (RemotClientSockets.Count != 0 && RemotClientSockets[ip] != null && RemotClientSockets[ip].Connected)
            {
                RemotClientSockets[ip].Shutdown(SocketShutdown.Both);
                RemotClientSockets[ip].Close();
                RemotClientSockets[ip] = null;
            }
            if (RemoteSocketLiveTime.Count != 0 && RemoteSocketLiveTime[ip] != null)
            {
                RemoteSocketLiveTime[ip] = null;
            }
        }