Example #1
0
        void client_Conn(ConClient conClient, bool conn)
        {
            if (conClient != null && conn)
            {
                LogOut.LogIn(string.Format("连接到:{0}  ---->OK", conClient.Sock.Sock.RemoteEndPoint.ToString()), ActionType.Message);

                if (!ConnUserList.ContainsKey(conClient.Key))
                {
                    if (ConnUserList.TryAdd(conClient.Key, conClient))
                    {
                        conClient.Sock.StartRead();

                        if (ClientConnToMe != null)
                        {
                            ClientConnToMe(conClient);
                        }
                    }
                }
                else
                {
                    conClient.Sock.Close();
                }
            }
            else
            {
                LogOut.LogIn(string.Format("无法连接到指定地址端口 {0}:{1}", conClient.Host, conClient.Port), ActionType.Message);
                conClient.Sock.Close();
            }
        }
Example #2
0
        public void ConToServer()
        {
            Mainclient               = new SocketClient();
            Mainclient.BinaryInput  += new ClientBinaryInputHandler(DataIn);
            Mainclient.MessageInput += new ClientMessageInputHandler(Exption);

            if (Mainclient.Connect(Host, Port))
            {
                Mainclient.StartRead();

                LogOut.LogIn("成功连接服务器", ActionType.ServerConn);

                string localip = ((IPEndPoint)(Mainclient.Sock.LocalEndPoint)).Address.ToString(); //获取本地局域网IP地址

                BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.REGION);
                tmp.AddItem(Key);
                tmp.AddItem(localip);
                tmp.AddItem(Mac);
                Mainclient.Send(tmp.Finish());
            }
            else
            {
                LogOut.LogIn("不能连接到服务器", ActionType.ServerNotConn);

                if (ServerDiscon != null)
                {
                    ServerDiscon("不能连接到服务器");
                }
            }
        }
Example #3
0
        void Exption(string message)
        {
            LogOut.LogIn("与服务器断开连接", ActionType.ServerDiscon);

            if (ServerDiscon != null)
            {
                ServerDiscon("与服务器断开连接");
            }
        }
Example #4
0
        void RunQueueList()
        {
            try
            {
                if (UserMaskList.Count > 0) //如果列队数量大于0
                {
Re:
                    string userkey;

                    if (UserMaskList.TryDequeue(out userkey)) //挤出一个用户ID
                    {
                        if (userkey == Key)
                        {
                            goto Re;
                        }

                        SocketClient client = new SocketClient();                        //建立一个 SOCKET客户端
Pt:
                        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定当前端口

                        if (BindPort >= 60000)
                        {
                            BindPort = 1000;
                        }

                        try
                        {
                            client.Sock.Bind(endpoint); //如果无法绑定此端口 那么换个端口
                        }
                        catch
                        {
                            goto Pt;
                        }

                        if (client.Connect(Host, RegIpPort)) //连接注册服务器端口
                        {
                            BufferFormat tmp = new BufferFormat(100);
                            tmp.AddItem(Key);
                            tmp.AddItem(BindPort);
                            client.Send(tmp.Finish());

                            System.Threading.Thread.Sleep(50); //等待 50毫秒

                            BufferFormatV2 tmp2 = new BufferFormatV2((int)PCMD.CONN);
                            tmp2.AddItem(userkey);
                            Mainclient.Send(tmp2.Finish());
                            client.Close();//关闭客户端
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogOut.LogIn(e.ToString(), ActionType.Error);
            }
        }
Example #5
0
        /// <summary>
        /// 连接到指定的端口上
        /// </summary>
        /// <param name="o"></param>
        private void RunConnToMe(object o)
        {
            try
            {
                string[] iphost = o.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                if (iphost.Length == 3)
                {
                    int port;

                    if (int.TryParse(iphost[1], out port))
                    {
                        int maxport = port + ResetConnt;         //最大端口等于提供的端口+尝试次数

                        for (int i = port + 1; i < maxport; i++) //循环连接的端口
                        {
                            ConClient client = new ConClient(iphost[0], i);

                            IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++);

                            if (BindPort >= 60000)
                            {
                                BindPort = 1000;
                            }

                            try
                            {
                                client.Sock.Sock.Bind(endpoint);
                                client.Key         = iphost[2];
                                client.Conn       += new ConnectionsHandlerFiler(client_Conn);
                                client.DataOutPut += new DataOutPutHandlerFiler(client_DataOutPut);
                                client.ExpOUtPut  += new ExpOutPutHandlerFiler(client_ExpOUtPut);
                                client.ConnTo();

                                LogOut.LogIn(string.Format("开始尝试本地端口{0} 连接到 {1}:{2}", BindPort - 1, iphost[0], i), ActionType.Message);
                            }
                            catch (SocketException e)
                            {
                                LogOut.LogIn("错误无法绑定本地端口:" + e.Message, ActionType.Error);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogOut.LogIn("代码出错#1:" + ex.Message, ActionType.Error);
            }
            finally
            {
                RunQueueList();
            }
        }
Example #6
0
        void client_ExpOUtPut(ConClient conClient, string Message)
        {
            if (ConnUserList.ContainsKey(conClient.Key))
            {
                ConnUserList.TryRemove(conClient.Key, out conClient);
            }

            LogOut.LogIn(string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key), ActionType.Message);

            if (ClientDiscon != null)
            {
                ClientDiscon(conClient, string.Format("客户连接断开 {0}:{1}", conClient.Host + ":" + conClient.Port, conClient.Key));
            }
        }