在implementation后面加入变量定义 const BufSize=2048; var RsltStream,TmpStream:TMemoryStream;
为Server的OnCreate事件添加代码: procedure TServer.FormCreate(Sender: TObject); begin RsltStream:=TMemoryStream.Create; TmpStream:=TMemoryStream.Create; end;
为Client的OnDestroy事件添加代码: procedure TServer.FormDestroy(Sender: TObject); begin RsltStream.Free; TmpStream.Free; end;
为控件Button1的OnClick事件添加代码: procedure TServer.Button1Click(Sender: TObject); var ReqCode:array[0..29] of char;ReqCodeStr:string; begin ReqCodeStr:='show'+Edit1.Text; StrpCopy(ReqCode,ReqCodeStr); TmpStream.Clear; RsltStream.Clear; SUDP.RemoteHost:=Edit2.Text; SUDP.SendBuffer(ReqCode,30); end;
为控件SUDP的OnDataReceived事件添加代码: procedure TServer.SUDPDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String); var ReqCode:array[0..29] of char;ReqCodeStr:string; begin ReqCodeStr:='show'+Edit1.text; StrpCopy(ReqCode,ReqCodeStr); SUDP.ReadStream(TmpStream); RsltStream.CopyFrom(TmpStream,NumberBytes); if NumberBytes< BufSize then { 数据已读完 } begin RsltStream.Position:=0; Image1.Picture.Bitmap.LoadFromStream(RsltStream); TmpStream.Clear; RsltStream.Clear; end else begin TmpStream.Clear; ReqCode:='show'; SUDP.RemoteHost:=Edit2.Text; SUDP.SendBuffer(ReqCode,30); end; end;