private void buttonLink_Click(object sender, EventArgs e) { if (buttonLink.Text == "断开") { client.stopReceive(); client.Close(); append("\r\n===============[已断开]===============", true); buttonLink.Text = "连接"; buttonLink.Enabled = true; return; } client = new Client(); int port; string host = textBoxAddr.Text; try { port = Convert.ToInt32(textBoxPort.Text); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); return; } buttonLink.Enabled = false; new Thread(() => { try { append("连接中..."); client = new Client(); client.Connect(host, port); append("[OK]", true); append("==============[已连接]==============", true); invoke(() => { buttonLink.Text = "断开"; buttonLink.Enabled = true; }); client.dataReceive += client_dataReceive; client.exGet += client_exGet; client.startReceive(); } catch (Exception ex) { append("\r\n错误:" + ex.Message, true); invoke(() => buttonLink.Enabled = true); } }).Start(); }
public receiveThread(Client client) { //this.socket = client.Client; this.client = client; this.thread = new Thread(recv); }
void _exGet(Client client, Exception ex) { if (ex is ThreadAbortException) return; if (exGet != null) exGet(this, ex); }
void client_exGet(Client client, Exception ex) { append("\r\n错误:" + ex.Message, true); #if DEBUG throw ex; #endif }
void client_dataReceive(Client client, string str) { if (str == null) { if (client.Connected) client.Close(); append("\r\n===============[已断开]===============", true); invoke(() => { buttonLink.Text = "连接"; buttonLink.Enabled = true; }); } else { append(str); } }