问题描述
要写一个Socket程序,界面很简单,两个textbox,textbox1输入要发送的string,连接建立后不需要点击button就能自动接收另一端发过来的string,而且textbox2自动显示收到的string,使用BeginStream进行异步回调,能够自动接收string到一个buffer中,但是我不知道怎么自动把收到的string自动显示到textbox2中,好像是要建立一个异步事件。请高手给指点一下,最好给出code。非常感激。
解决方案
解决方案二:
就是原来的程序server端点一个receivebutton,程序会停在那里等待client端给他发string,client也一样,点了receivebutton就会等server端发,现在我要去掉receivebutton,只要sendbutton,只要一端发,另一端能自动接收并显示到textbox2中,不知道改怎么做,请高手指点。
解决方案三:
delegatevoidaaa(objectsender,eventarge);在收到数据的事件里写.if(this.RequireInvoke)this.Invoke(newaaa(该事件处理方法的名称);else{将数据显示到TextBox;}
解决方案四:
楼上的我还是有点不明白,我程序中接收的code是这样的,不在classform1中是封在另一个class里:privatestaticIrDAClientirClient=newIrDAClient();publicstaticboolwait=false;//这是标志位publicstaticstringReceiveString="";//收到的string放到这里privateThreadTlisten=newThread(newThreadStart(IrDAListenerThread));//SetthreadstaticStreamReceiveStream;publicstaticvoidIrDAListenerThread()//thelistenthread!{while(true){if(wait==true){BeginRecStr();//beginreceive}}}publicstaticvoidBeginRecStr(){ReceiveStream=irClient.GetStream();byte[]bufferRec=newbyte[buffersize];ReceiveString="";if(wait)//waitisaswitch,couldbeacommandallowableswitch{try{ReceiveStream.BeginRead(bufferRec,0,buffersize,newAsyncCallback(EndRecStr),null);//??????????????????????????}catch(Exceptionex){MessageBox.Show(ex.Message);}while(wait){Thread.Sleep(50);//waitforreceive}//datareceived,readytoturntostring!ReceiveStream.Close();try{ReceiveString=ASCIIEncoding.Default.GetString(bufferRec,0,bufferRec.Length);Thread.Sleep(100);//wait=true;//我不明白什么时候把标致位设为true//bufferRec.Initialize();//thiswilltakeupsometime!}catch(Exception){MessageBox.Show("ReadFailure,NoBytesreceived!");//bufferRec.Initialize();}}else{ReceiveStream.Close();}}privatestaticvoidEndRecStr(IAsyncResultar){intByteReceived=ReceiveStream.EndRead(ar);//somedelayhere!if(ByteReceived<=0){MessageBox.Show("NoBytesReceived");}//MessageBox.Show(ByteReceived.ToString()+"BytesReceived!");//ReceiveStream.Close();wait=false;BeginRecStr();//callbackfunction}我还是不知道怎么写这个事件,在哪里写?怎么在form1中怎么触发接收的事件?