//ar是IAsyncResult类型的接口,表示异步操作的状态 //是由lister.beginaccepttcpclient(callback,listner)传递过来的 private void AcceptTcpClientCallback(IAsyncResult ar) { if (isExit == false) { try { //将事件状态设为终止状态,允许一个或多个等待链接线程继续 allDone.Set(); TcpListener myListener = (TcpListener)ar.AsyncState; //异步接收传入的连接,并创建新的TCPclient对象处理远程主机通信 TcpClient client = myListener.EndAcceptTcpClient(ar); listBox_Status.Invoke(setListBoxCallback, "已接受客户连接:" + client.Client.RemoteEndPoint); comboBox1.Invoke(setComboBoxCallback, client.Client.RemoteEndPoint.ToString()); ReadWriteObject readWriteObject = new ReadWriteObject(client); clientList.Add(readWriteObject); SendString(readWriteObject, "服务器已经接收连接,请通话!"); readWriteObject.netStream.BeginRead(readWriteObject.readByte, 0, readWriteObject.readByte.Length, ReadCallback, readWriteObject); } catch (System.Exception ex) { listBox_Status.Invoke(setListBoxCallback, ex.Message); return; } } }
private void SendCallback(IAsyncResult ar) { ReadWriteObject readWriteObject = (ReadWriteObject)ar.AsyncState; try { readWriteObject.netStream.EndWrite(ar); } catch (Exception err) { listBoxStatus.Invoke(setListBoxCallback, err.Message); comboBox1.Invoke(removeComboBoxItemsCallback, readWriteObject); } }
private void SendString(ReadWriteObject readWriteObject, string str) { try { readWriteObject.writeBytes = Encoding.UTF8.GetBytes(str + "\r\n"); readWriteObject.netStream.BeginWrite(readWriteObject.writeBytes, 0, readWriteObject.writeBytes.Length, new AsyncCallback(SendCallback), readWriteObject); readWriteObject.netStream.Flush(); listBoxStatus.Invoke(setListBoxCallback, string.Format("向{0}发送:{1}", readWriteObject.client.Client.RemoteEndPoint, str)); } catch (Exception err) { listBoxStatus.Invoke(setListBoxCallback, err.Message); } }
private void SendCallback(IAsyncResult ar) { ReadWriteObject readWriteObject = (ReadWriteObject)ar.AsyncState; try { readWriteObject.netStream.EndWrite(ar); } catch (System.Exception ex) { listBox_Status.Invoke(setListBoxCallback, ex.Message); comboBox1.Invoke(removeComboBoxItemsCallback, readWriteObject);//如果发送出现异常,就把ComboBox中指定的项删掉 } }
//【发送】按钮Click事件 private void buttonSend_Click(object sender, EventArgs e) { int index = comboBox1.SelectedIndex; if (index == -1) { MessageBox.Show("请先选择接受方,然后再点击[发送]"); } else { ReadWriteObject obj = (ReadWriteObject)clientList[index]; SendString(obj, richTextBoxSend.Text); richTextBoxSend.Clear(); } }
private void SendString(ReadWriteObject readWriteObject, string str) { try { readWriteObject.writeByte = System.Text.Encoding.Unicode.GetBytes(str + "\r\n"); readWriteObject.netStream.BeginWrite(readWriteObject.writeByte, 0, readWriteObject.writeByte.Length, new AsyncCallback(SendCallback), readWriteObject);//这里的callback委托为什么要新建一个对象呢?而不直接使用SENDCALLback? readWriteObject.netStream.Flush(); listBox_Status.Invoke(setListBoxCallback, string.Format("向{0}发送:{1}", readWriteObject.client.Client.RemoteEndPoint, str)); } catch (System.Exception ex) { listBox_Status.Invoke(setListBoxCallback, ex.Message); } }
private void ReadCallback(IAsyncResult ar) { try { ReadWriteObject readWriteObject = (ReadWriteObject)ar.AsyncState; int count = readWriteObject.netStream.EndRead(ar); richTextBoxReceive.Invoke(setRichTextBoxCallback, string.Format("[来自{0}]{1}", readWriteObject.client.Client.RemoteEndPoint, System.Text.Encoding.UTF8.GetString(readWriteObject.readBytes, 0, count))); if (isExit == false) { readWriteObject.InitReadArray(); readWriteObject.netStream.BeginRead(readWriteObject.readBytes, 0, readWriteObject.readBytes.Length, ReadCallback, readWriteObject); } } catch (Exception err) { listBoxStatus.Invoke(setListBoxCallback, err.Message); } }
private void RemoveComboBoxItems(ReadWriteObject readWriteObject) { int index = clientList.IndexOf(readWriteObject); comboBox1.Items.RemoveAt(index); }