clientFrm.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:4k
- unit clientFrm;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ScktComp, ExtCtrls, ToolWin, ComCtrls, Menus, Buttons, shellapi,
- jpeg, ImgList;
- type
- TfrmClient = class(TForm)
- Image: TImage;
- ClientSocket: TClientSocket;
- ImageList1: TImageList;
- ToolBar1: TToolBar;
- btnConnect: TToolButton;
- btnSnap: TToolButton;
- btnDisConnect: TToolButton;
- btnClose: TToolButton;
- procedure bu_snapClick(Sender: TObject);
- procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure btnConnectClick(Sender: TObject);
- procedure btnSnapClick(Sender: TObject);
- procedure btnDisConnectClick(Sender: TObject);
- procedure ClientSocketConnect(Sender: TObject;
- Socket: TCustomWinSocket);
- procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
- ErrorEvent: TErrorEvent; var ErrorCode: Integer);
- procedure BtnCloseClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- frmClient: TfrmClient;
- implementation
- const bufsize = 2048;//接收数据缓冲区的最大值
- var
- stream: Tmemorystream; //存储接收到jpeg内存流
- jpegscreen: Tjpegimage; //存储jpeg图象
- {$R *.DFM}
- //当窗体创建时,创建Stream,jpegStream对象
- procedure TfrmClient.FormCreate(Sender: TObject);
- begin
- stream := Tmemorystream.Create;
- jpegscreen := Tjpegimage.Create;
- end;
- //当窗体关闭时,关闭套接字的连接,并释放stream,jpegStream对象
- procedure TfrmClient.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- clientsocket.Active := false;
- stream.Free;
- jpegscreen.Free;
- end;
- //连接服务器
- procedure TfrmClient.BtnConnectClick(Sender: TObject);
- var
- remotehost: string;
- begin
- //输入远程服务器的IP地址
- remotehost := inputbox('建立连接', '请输入被察看的机器的IP地址或名称:', '');
- if trim(remotehost) <> '' then
- begin
- if clientsocket.Active then clientsocket.Active := true;
- clientsocket.Port := 1234;
- clientsocket.Host := remotehost;
- //连接
- try
- clientsocket.Active := true;
- except
- raise Exception.Create('连接失败!');
- end;
- end;
- end;
- //断开连接
- procedure TfrmClient.btnDisConnectClick(Sender: TObject);
- begin
- clientsocket.Active := false;
- stream.Clear;
- end;
- //向服务器端发出'show'指令,要求获取它的屏幕
- procedure TfrmClient.btnSnapClick(Sender: TObject);
- begin
- stream.Clear;
- if not ClientSocket.Active then
- Raise Exception.Create('请先连接');
- clientsocket.Socket.SendText('show');
- end;
- //关闭窗口
- procedure TfrmClient.BtnCloseClick(Sender: TObject);
- begin
- clientsocket.Active := false;
- close;
- end;
- //客户端接收远程屏幕图象
- procedure TfrmClient.ClientSocketRead(Sender: TObject;
- Socket: TCustomWinSocket);
- var
- buf: array[0..bufsize - 1] of char;
- numberbytes: integer;
- begin
- numberbytes := socket.ReceiveLength;
- //接收数据
- socket.receiveBuf(buf, numberbytes);
- //累加读到stream内存流中
- stream.Write(buf, numberbytes);
- //当接收到的数据不等于bufSize,代表接收的屏幕图象的最后一个数据,
- //否则,代表还没有接收完,继续相服务器端发送'show'指令,继续获取数据
- if NumberBytes = bufSize then
- Socket.SendText('show')
- else
- begin
- try
- //此句代码,非常重要,千万不要忘写,它将内存流指针移到头,
- //否则读取内存流时,会从当前位置开始读
- Stream.Position := 0;
- //将内存流读到JpegScreen图象中
- jpegscreen.LoadFromStream(stream);
- //显示在image组件中
- image.Picture.Bitmap.Assign(jpegscreen);
- except
- stream.Clear;
- end;
- end;
- end;
- procedure TfrmClient.ClientSocketConnect(Sender: TObject;
- Socket: TCustomWinSocket);
- begin
- showmessage('连接成功!');
- end;
- //当客户端Socket出错时,清空stream数据
- procedure TfrmClient.ClientSocketError(Sender: TObject;
- Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
- var ErrorCode: Integer);
- begin
- stream.Clear;
- end;
- end.