//ar是IAsyncResult类型的接口,表示异步操作的状态 //是由Listner。beginaccepttcpclient(callback,listener)传递过来的 private void RequestCallback(IAsyncResult ar) { //异步操作能执行到此处,说明调用beginconnect已经完成 //并得到了IAsyncResult类型的状态参数ar,但beginconnect尚未结束 //此时需要解除阻塞,以便能调用Endconnect allDone.Set(); //调用Set后,事件状态变为终止状态,当前线程继续 //buttonconnect_client执行结束 //同时窗体可以响应用户操作 try { //获取连接成功后得到的参数状态 client = (TcpClient)ar.AsyncState; //异步接收传入的连接尝试,使beginconnect正常结束 client.EndConnect(ar); ListBoxAdd(string.Format("连接[{0}]成功", client.Client.RemoteEndPoint)); //listBox_Status.Invoke(setListBoxCallback,string.Format("连接[{0}]成功",client.Client.RemoteEndPoint)); //获取接收和发送数据的网络流 networkStream = client.GetStream(); //异步接收服务器发送的数据,Beginread完成后,会自动调用readcallback ReadObject readObj = new ReadObject(networkStream, client.ReceiveBufferSize); networkStream.BeginRead(readObj.bytes, 0, readObj.bytes.Length, ReadCallback, readObj); } catch (System.Exception ex) { listBox_Status.Invoke(setListBoxCallback, ex.Message); return; } }
//ar是IAsyncResult类型的几口,表示异步操作的状态 //是由listenner.BeginAcceptTcpClient(callback,listener)传递过来的 private void RequestCallback(IAsyncResult ar) { //异步操作能执行到此处,说明调用BeginConnect已经完成 //并且得到了IAsyncResult类型的状态参数ar,但BeginConnect尚未结束 //此时需要解除阻塞,以便能调用EndConnect allDone.Set(); //调用Set后,事件的状态变为终止状态,当前线程继续 //buttonConnect_Click执行结束,同时窗台界面可以相应用户操作 try { //获取连接成功后得到的状态参数 client = (TcpClient)ar.AsyncState; //异步接收传入的连接尝试,使BeginConnect正常结束 client.EndConnect(ar); listBoxStatus.Invoke(setListBoxCallback, string.Format("与服务器{0}连接成功", client.Client.RemoteEndPoint)); //获取接收和发送数据的网络流 networkStream = client.GetStream(); //获取接收和发送的数据,BeginRead完成后,会自动调用ReadCallback ReadObject readObject = new ReadObject(networkStream, client.ReceiveBufferSize); networkStream.BeginRead(readObject.bytes, 0, readObject.bytes.Length, ReadCallback, readObject); //allDone.WaitOne(); } catch (Exception err) { listBoxStatus.Invoke(setListBoxCallback, err.Message); return; } }
private void ReadCallback(IAsyncResult ar) { //异步操作能执行到此处,说明调用BeginRead已经完成 try { ReadObject readObject = (ReadObject)ar.AsyncState; int count = readObject.netStream.EndRead(ar); richTextBoxReceive.Invoke(setRichTextBoxReceiveCallback, Encoding.UTF8.GetString(readObject.bytes, 0, count)); if (isExit == false) { //重新调用BeginRead进行异步读取 readObject = new ReadObject(networkStream, client.ReceiveBufferSize); networkStream.BeginRead(readObject.bytes, 0, readObject.bytes.Length, ReadCallback, readObject); } } catch (Exception err) { listBoxStatus.Invoke(setListBoxCallback, err.Message); } }
private void ReadCallback(IAsyncResult ar) { //异步操作能执行到此处,说明调用beginread已经完成 try { ReadObject readobj = (ReadObject)ar.AsyncState; int count = readobj.netStream.EndRead(ar); //textBox_Receive.Invoke(setTextBoxReceiveCallback, System.Text.Encoding.Unicode.GetString(readobj.bytes, 0, count)); TextBoxAdd("[服务器]:" + System.Text.Encoding.Unicode.GetString(readobj.bytes, 0, count)); if (isExit == false) { //重新调用beginread进行异步读取 readobj = new ReadObject(networkStream, client.ReceiveBufferSize); networkStream.BeginRead(readobj.bytes, 0, readobj.bytes.Length, ReadCallback, readobj); } } catch (System.Exception ex) { listBox_Status.Invoke(setListBoxCallback, ex.Message); } }