一台机器监视另一台机器的问题,但出错,大侠们帮忙~ ( 积分: 95 )

  • 主题发起人 主题发起人 cjianwen
  • 开始时间 开始时间
C

cjianwen

Unregistered / Unconfirmed
GUEST, unregistred user!
我在监视端放的tidtcpclient,可以随时连接一台被监视的机器,

被监视端的代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, ExtCtrls;

type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
Tmythread=class(TThread)
protected
procedure execute;override;
end;

var
Form1: TForm1;
mm:TMemorystream;
H:HDC;
hc:hwnd;
a:array [0..2047] of char;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
mm:=TMemorystream.Create;
h:=getwindowdc(0);
hc:=getcursor();
Tmythread.Create(false);
end;

{ Tmythread }

procedure Tmythread.execute;
var
P:Tpoint;
bp:Tbitmap;
begin
inherited;
while (not self.Terminated) and (not application.Terminated) do
begin
bp:=Tbitmap.Create;//这里不知道为什么,只能用局部变量,用全局变量后抓几次就
bp.Width:=screen.Width; //没反应了
bp.Height:=screen.Height;//改成这就还可以不停的抓图
bitblt(bp.Canvas.Handle,0,0,bp.Width,bp.Height,h,0,0,srccopy);
getcursorpos(p);
drawicon(bp.Canvas.Handle,p.X,p.Y,hc);
mm.Clear;
bp.SaveToStream(mm);
mm.Position:=0;
form1.Image1.Picture.Bitmap.LoadFromStream(mm);
//form1.Image1.Picture.Bitmap:=bp;
application.ProcessMessages;
bp.Free;
sleep(100);
end;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
m,n:integer;
begin
if athread.Connection.ReadLn()='ok' then
begin
showmessage('ok'); //用来跟踪的~
athread.Connection.WriteLn(inttostr(mm.Size));//发送流大小过去
mm.Position:=0;
while mm.Position<mm.Size do
begin
n:=mm.Read(a,sizeof(a));
athread.Connection.WriteBuffer(a,n);
end;
end;
end;

end.


监视端的代码:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, ExtCtrls,math;

type
TForm1 = class(TForm)
Button1: TButton;
tcpc: TIdTCPClient;
Edit1: TEdit;
Button2: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
tthd=class(TTHread)
protected
procedure execute; override;
end;

var
Form1: TForm1;
mm:Tmemorystream;
a:array [0..2047] of byte;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
tcpc.Host:=edit1.Text;
tcpc.Connect();
if tcpc.Connected then
begin
showmessage('ok');
tthd.Create(false);
end
else showmessage('no');
end;

{ tthd }

procedure tthd.execute;
var
m,n,l:integer;
s:string;
begin
inherited;
form1.tcpc.WriteLn('ok');
s:=form1.tcpc.ReadLn(); //这里总是延时,不能运行~
l:=strtoint(s);
mm.Clear;
while mm.Size<l do
begin
form1.tcpc.ReadBuffer(a,sizeof(a));
n:=min(sizeof(a),l-mm.Size);
mm.WriteBuffer(a,n);
end;
showmessage(inttostr(mm.Size));
mm.Position:=0;
form1.Image1.Picture.Bitmap.LoadFromStream(mm);
sleep(100);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
mm:=Tmemorystream.Create;
end;

end.

第一次写类似的代码,有可能我的方法不对~读数据用线程来不停的请求~~~
望大侠们指点一下,非常感谢~`,本人太菜,分不多,全部奉上!
 
我在监视端放的tidtcpclient,可以随时连接一台被监视的机器,

被监视端的代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, ExtCtrls;

type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
Tmythread=class(TThread)
protected
procedure execute;override;
end;

var
Form1: TForm1;
mm:TMemorystream;
H:HDC;
hc:hwnd;
a:array [0..2047] of char;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
mm:=TMemorystream.Create;
h:=getwindowdc(0);
hc:=getcursor();
Tmythread.Create(false);
end;

{ Tmythread }

procedure Tmythread.execute;
var
P:Tpoint;
bp:Tbitmap;
begin
inherited;
while (not self.Terminated) and (not application.Terminated) do
begin
bp:=Tbitmap.Create;//这里不知道为什么,只能用局部变量,用全局变量后抓几次就
bp.Width:=screen.Width; //没反应了
bp.Height:=screen.Height;//改成这就还可以不停的抓图
bitblt(bp.Canvas.Handle,0,0,bp.Width,bp.Height,h,0,0,srccopy);
getcursorpos(p);
drawicon(bp.Canvas.Handle,p.X,p.Y,hc);
mm.Clear;
bp.SaveToStream(mm);
mm.Position:=0;
form1.Image1.Picture.Bitmap.LoadFromStream(mm);
//form1.Image1.Picture.Bitmap:=bp;
application.ProcessMessages;
bp.Free;
sleep(100);
end;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
m,n:integer;
begin
if athread.Connection.ReadLn()='ok' then
begin
showmessage('ok'); //用来跟踪的~
athread.Connection.WriteLn(inttostr(mm.Size));//发送流大小过去
mm.Position:=0;
while mm.Position<mm.Size do
begin
n:=mm.Read(a,sizeof(a));
athread.Connection.WriteBuffer(a,n);
end;
end;
end;

end.


监视端的代码:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, ExtCtrls,math;

type
TForm1 = class(TForm)
Button1: TButton;
tcpc: TIdTCPClient;
Edit1: TEdit;
Button2: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
tthd=class(TTHread)
protected
procedure execute; override;
end;

var
Form1: TForm1;
mm:Tmemorystream;
a:array [0..2047] of byte;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
tcpc.Host:=edit1.Text;
tcpc.Connect();
if tcpc.Connected then
begin
showmessage('ok');
tthd.Create(false);
end
else showmessage('no');
end;

{ tthd }

procedure tthd.execute;
var
m,n,l:integer;
s:string;
begin
inherited;
form1.tcpc.WriteLn('ok');
s:=form1.tcpc.ReadLn(); //这里总是延时,不能运行~
l:=strtoint(s);
mm.Clear;
while mm.Size<l do
begin
form1.tcpc.ReadBuffer(a,sizeof(a));
n:=min(sizeof(a),l-mm.Size);
mm.WriteBuffer(a,n);
end;
showmessage(inttostr(mm.Size));
mm.Position:=0;
form1.Image1.Picture.Bitmap.LoadFromStream(mm);
sleep(100);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
mm:=Tmemorystream.Create;
end;

end.

第一次写类似的代码,有可能我的方法不对~读数据用线程来不停的请求~~~
望大侠们指点一下,非常感谢~`,本人太菜,分不多,全部奉上!
 
高手都哪去了,帮帮我呀,虽然我分不多~~~
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部