Ejemplo n.º 1
0
        private async void ConnectToServer(Dictionary <string, string> obj)
        {
            var client = new EasyClient <MyPackageInfo>();

            client.Initialize(new NoReceiveFilter());
            client.Connected          += OnClientConnected;
            client.NewPackageReceived += OnPackageReceived;
            client.Error  += OnClientError;
            client.Closed += OnClientClosed;
            var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Parse(obj["IPAddress"]), Convert.ToInt32(obj["Port"])));



            if (connected)
            {
                ClientConnectInfo sci = new ClientConnectInfo
                {
                    ServerAddress = obj["IPAddress"] + ":" + obj["Port"],
                    ServerPort    = Convert.ToInt32(obj["Port"]),
                    LocalEndPoint = client.LocalEndPoint.ToString(),
                    IsConnected   = "已连接"
                };

                ClientConnectInfoList.Add(sci);
                ClientList.Add(client);
            }
            else
            {
                DXMessageBox.Show("连接服务端失败!", "提示");
            }

            Messenger.Default.Unregister <Dictionary <string, string> >(this, "ConnectToServer", ConnectToServer);
        }
Ejemplo n.º 2
0
        private void Close()
        {
            for (int i = 0; i < ClientList.Count; i++)
            {
                if (ClientList[i].IsConnected == true)
                {
                    if (ClientList[i].LocalEndPoint.ToString() == SelectedClientConnectInfo.LocalEndPoint)
                    {
                        string localEndPoint = SelectedClientConnectInfo.LocalEndPoint;
                        if (ClientList[i].Close().Result)
                        {
                            ClientList.Remove(ClientList[i]);

                            for (int k = 0; k < ClientConnectInfoList.Count; k++)
                            {
                                if (ClientConnectInfoList[k].LocalEndPoint == localEndPoint)
                                {
                                    ClientConnectInfoList.Remove(ClientConnectInfoList[k]);
                                    AllClientMessageList.Remove(localEndPoint);
                                }
                            }
                        }
                    }
                }
            }
        }