---- 第二步,编制VServer.exe文件。新建Delphi工程,将窗体的Name属性设为“Server”。加入TNMUDP控件,Name属性设为“SUDP”;LocalPort属性设为“2222”,让控件SUDP监视主控机的2222端口,当有数据发送到该口时,触发控件SUDP的OnDataReceived事件;RemotePort属性设为“1111”,当控件SUDP发送数据时,将数据发到受控机的1111口。加入控件Image1,Align属性设为“alClient”;加入控件Button1,Caption属性设为“截屏”;加入控件Label1,Caption属性设为“左:上:右:下”;加入控件Edit1,Text属性设为“0:0:100:100”;加入控件Label2,Caption属性设为“受控机IP地址”;加入控件Edit2,Text属性设为“127.0.0.1”; 在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;