private void btnConnect_Click(object sender, EventArgs e) { try { IPAddress ip = IPAddress.Parse(txtIPAddress.Text); int port = int.Parse(txtPort.Text); client = new EventDrivenTCPClient(ip, port); client.DataReceived += client_DataReceived; client.ConnectionStatusChanged += client_ConnectionStatusChanged; client.Connect(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void client_DataReceived(EventDrivenTCPClient sender, object data) { string text = data as string; string[] lines = Regex.Split(text, "\r\n"); foreach (string line in lines) { if (!String.IsNullOrEmpty(line)) ProcessLine(line); } }
private void btnClose_Click(object sender, EventArgs e) { if (client != null) { client.Disconnect(); client = null; } }
void client_ConnectionStatusChanged(EventDrivenTCPClient sender, EventDrivenTCPClient.ConnectionStatus status) { this.Invoke((MethodInvoker)delegate { switch (status) { case EventDrivenTCPClient.ConnectionStatus.Connected: btnClose.Enabled = true; btnUpdate.Enabled = true; btnConnect.Enabled = false; timer1.Enabled = true; break; case EventDrivenTCPClient.ConnectionStatus.DisconnectedByHost: client.Disconnect(); break; case EventDrivenTCPClient.ConnectionStatus.DisconnectedByUser: timer1.Enabled = false; treeView1.Nodes.Clear(); txtIncoming.Text = ""; btnConnect.Enabled = true; btnUpdate.Enabled = false; btnSet.Enabled = false; btnClose.Enabled = false; break; } }); }