public ConnectionThread(string _ip, string _port, SocketClientConnetion _cls) { IP = _ip; port = _port; _socketClient = _cls; tThread = new Thread(new ThreadStart(Conntion)); tThread.Start(); }
/// <summary> /// TCP客户端构造函数 /// </summary> public SocketTCPClient() { _rwLock = new ReaderWriterLock(); _socketsCache = new Dictionary <long, SocketTCPHandler>(); _socketClient = new SocketClientConnetion(); _socketClient.OnNewSocketAccept += new NewSocketEventHandler(_socketListener_OnNewSocketAccept); _threadclient = new Thread(new ThreadStart(ThreadClientConnetion)); _threadclient.Start(); }
private void ThreadClientConnetion() { SocketTCPHandler tcpHandler; ConnectionThread ctCls; for (; ;) { try { if (_clientNow) { for (int i = _socketsCache.Values.Count - 1; i >= 0; i--) { tcpHandler = _socketsCache.Values.ToList()[i]; if (_lstclient.FindIndex(p => p._ip == tcpHandler.IP) < 0) { try { if (tcpHandler != null) { tcpHandler.Disconnect(); } } catch { } } } for (int i = 0; i < _lstclient.Count; i++) { if (_socketsCache.Values.ToList().FindIndex(p => (p.IP == _lstclient[i]._ip) && (p.Port.ToString() == _lstclient[i]._port)) < 0) {//以前没有,就重新建立连接 if (LstConntionTime.FindIndex(p => p.IP == _lstclient[i]._ip) < 0) { if (PingIP(_lstclient[i]._ip)) { _socketClient = new SocketClientConnetion(); _socketClient.OnNewSocketAccept += new NewSocketEventHandler(_socketListener_OnNewSocketAccept); ctCls = new ConnectionThread(_lstclient[i]._ip, _lstclient[i]._port, _socketClient); LstConntionTime.Add(new ConntionTime(_lstclient[i]._ip, 0)); } } } } } _clientNow = false; for (int i = 0; i < LstConntionTime.Count; i++) { LstConntionTime[i].Count++; if (LstConntionTime[i].Count > 30) { LstConntionTime.RemoveAt(i); i--; } } Thread.Sleep(500); } catch (Exception ex) { Basic.Framework.Logging.LogHelper.Error(string.Format(" socket log: ThreadClientConnetion失败--{0}!", ex.Message)); } } }