private void timer1_Tick(object sender, EventArgs e) { lock (listBoxUsable) { for (int i = listBoxUsable.Items.Count - 1; i >= 0; i--) // ClientInfo ci in listBoxUsable.Items) { try { ClientInfo ci = listBoxUsable.Items[i] as ClientInfo; StreamWriter sw = new StreamWriter(new NetworkStream(ci.Socket)); sw.WriteLine("alive"); sw.Flush(); } catch (Exception e2) { if (DefaultShowDevice != null && (listBoxUsable.Items[i] as ClientInfo).IP == DefaultShowDevice.IP) { DefaultShowDevice = null; status.Text = ""; } try { ClientInfo ciRevoced = listBoxUsable.Items[i] as ClientInfo; listBoxUsable.Items.RemoveAt(i); ciRevoced.Socket.Close(); } catch (Exception e3) { FormClient.Log(e3.ToString()); } } } } }
private void 断开重连CToolStripMenuItem_Click(object sender, EventArgs e) { ClientInfo ci = listBoxUsable.SelectedItem as ClientInfo; if (ci == null) { return; } try { listBoxUsable.Items.Remove(listBoxUsable.SelectedItem); ci.Socket.Close(); } catch (Exception e1) { FormClient.Log(e1.ToString()); } }
public void ReceiveConnectionsThreadRun() { while (IsRunning) { newClient = _tcpl.AcceptSocket(); string ip = (newClient.RemoteEndPoint as IPEndPoint).Address.ToString(); string name = Win32API.GetConfigFromIni("Names", ip, "", iniFile); ClientInfo ci = new ClientInfo(ip, name, newClient); //_transmit_tb.Add(ip, ci); lock (listBoxUsable) { for (int i = listBoxUsable.Items.Count - 1; i >= 0; i--) //删除重复记录 { if ((listBoxUsable.Items[i] as ClientInfo).IP == ip) { try { ClientInfo ciRemoved = listBoxUsable.Items[i] as ClientInfo; listBoxUsable.Items.RemoveAt(i); ciRemoved.Socket.Close(); } catch (Exception e) { FormClient.Log(e.ToString()); } } } } listBoxUsable.Items.Add(ci); //networkstream = new NetworkStream(newClient); //streamWriter = new StreamWriter(networkstream); if (newClient.Connected) //对每个客户端开启一个连接 { newClient.ReceiveTimeout = 3000; Thread receiveControlMessageThread = new Thread(new ParameterizedThreadStart(receiveControlMessageThreadRun)); receiveControlMessageThread.Start(newClient); } } }