Пример #1
0
 public void OnFaildConnect(SocketClientManager scm)
 {
     Console.WriteLine(GetDateNow() + "连接服务器" + scm.ip + " : " + scm.port + " 失败\r\n");
     if (scm.tbSocketLog.InvokeRequired)
     {
         this.Invoke(new Action(() =>
         {
             scm.tbSocketLog.Text += GetDateNow() + "连接服务器" + scm.ip + " : " + scm.port + " 失败\r\n";
         }));
     }
 }
Пример #2
0
        private SocketClientManager InitSocket(String ip, int port, TextBox tbSocketLog)
        {
            tbSocketLog.Text = "";
            SocketClientManager _scm = new SocketClientManager(ip, port, tbSocketLog);

            _scm.OnReceiveMsg   += OnReceiveMsg;
            _scm.OnConnected    += OnConnected;
            _scm.OnFaildConnect += OnFaildConnect;
            _scm.Start();
            return(_scm);
        }
Пример #3
0
        public void OnConnected(SocketClientManager scm)
        {
            Console.WriteLine(GetDateNow() + "连接服务器" + scm.ip + " : " + scm.port + "成功\r\n");
            string ipClient   = scm._socket.LocalEndPoint.ToString().Split(':')[0];
            string posrClient = scm._socket.LocalEndPoint.ToString().Split(':')[1];

            if (scm.tbSocketLog.InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    scm.tbSocketLog.Text += GetDateNow() + "连接服务器" + scm.ip + ":" + scm.port + "成功\r\n";
                    scm.tbSocketLog.Text += GetDateNow() + "当前连接:" + ipClient + ":" + posrClient + "\r\n";
                }));
            }
        }
Пример #4
0
        public void OnReceiveMsg(SocketClientManager scm)
        {
            byte[] buffer = scm.socketInfo.buffer;
            string msg    = Encoding.UTF8.GetString(buffer).Replace("\0", "");

            if (string.IsNullOrEmpty(msg))
            {
                return;
            }
            else if (msg.StartsWith("{'status'"))
            {
                JObject ja     = (JObject)JsonConvert.DeserializeObject(msg);
                string  status = ja["status"].ToString();
                if (chklbRemoteUpdater.InvokeRequired)
                {
                    this.Invoke(new Action(() =>
                    {
                        for (int j = 0; j < chklbRemoteUpdater.Items.Count; j++)
                        {
                            string text = chklbRemoteUpdater.Items[j].ToString();
                            string ip   = text.Substring(0, text.IndexOf("[") < 0 ? text.Length : text.IndexOf("["));
                            if (ip.Equals(scm.ip))
                            {
                                chklbRemoteUpdater.Items[j] = ip + "[" + status + "]";
                            }
                        }
                    }));
                }
            }
            else
            {
                if (scm.tbSocketLog.InvokeRequired)
                {
                    this.Invoke(new Action(() =>
                    {
                        scm.tbSocketLog.Text += GetDateNow() + "收到数据:" + msg + "\r\n";
                    }));
                }
            }
        }