Delphi中远程控制的实现

2008-04-09 04:18:27来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

  实行远程控制,实际上就是一个服务器程序(以下简称被控程序)和一个客户程序(以下简称主控程序):被控方即为服务器程序,它监听客户的请求,并作出相应的处理;主控方即为客户程序,它连接上服务器后,发出自己的请求,服务器便根据自已的请求进行不同的响应。而WinSock是一组API,用于在网络上传输数据和交换信息。通过WinSock编程有更大的灵活性,因为它不需要关心网络连接的细节,然而用WinSock编程却很复杂。但是,在Delphi中我们并不需要直接与WinSock的API打交道,因为TClientSocket元件和TServerSocket元件(在Internet页)封装了WinSock的大部分API,使得对WinSock的访问大大简化。下面这个简单的远程控制的例子就很清楚地说明了这个问题,它可以发送一个消息给对方,对方收到后发出信息确认。当然,在实际的编程中,我们可以根据需要,要求被控方可以根据主控方发来的信息,经过判断后作出相应的动作。如关机、传送文件…
一、主控程序(Client端):

ClientSocket1的Port设为:2058。当然,可以改变,但要求主控程序和被控程序的Port值一致。界面如下:


源程序如下:

unit main;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, ExtCtrls, ScktComp;

type

TMainForm = class(TForm)

Panel1: TPanel;

Panel2: TPanel;

Panel3: TPanel;

Memo1: TMemo;

Edit1: TEdit;

Button1: TButton;

Button2: TButton;

ClientSocket1: TClientSocket;

Edit2: TEdit;

procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);

procedure Button2Click(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure FormClose(Sender: TObject; var Action: TCloseAction);

procedure Edit1KeyPress(Sender: TObject; var Key: Char);

private


procedure Send_Message;//发送信息

{ Private declarations }

public

{ Public declarations }

end;

var

MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.Send_Message;

begin

ClientSocket1.Socket.SendText(Edit1.Text);//发送信息

Edit1.text:='';

end;

procedure TMainForm.ClientSocket1Read(Sender: TObject;

Socket: TCustomWinSocket);

var

s:string;

begin

s:=ClientSocket1.Socket.ReceiveText;//接收信息

Memo1.Lines.Add('服务器端返回的信息为:' s);

end;

procedure TMainForm.Button2Click(Sender: TObject);

begin

if Button2.Caption='登录' then

begin

Button2.Caption:='断开';

ClientSocket1.Address:=Edit2.Text;

ClientSocket1.Active:=False;

ClientSocket1.Active:=True;

Button1.Enabled:=True;

Edit1.Enabled:=True;

end else

begin

Button2.Caption:='登录';

ClientSocket1.Active:=False;

Button1.Enabled:=False;

Edit1.Enabled:=False;

end;

end;

procedure TMainForm.Button1Click(Sender: TObject);

begin

Send_Message;

end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);

begin

if ClientSocket1.Active then

ClientSocket1.Active:=False;

end;

procedure TMainForm.Edit1KeyPress(Sender: TObject; var Key: Char);

begin

if key=#13 then

begin

key:=#0;

Send_Message;

end;

end;

end. 二、被控方的程序(Server端):

ServerSocket1的Port设为:2058。程序界面如下:



源程序如下:

unit main;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ScktComp;

type

TMainForm = class(TForm)

Panel1: TPanel;

Panel2: TPanel;

Panel3: TPanel;

Memo1: TMemo;

Button1: TButton;

Label1: TLabel;

ServerSocket1: TServerSocket;

procedure ServerSocket1Listen(Sender: TObject;

Socket: TCustomWinSocket);

procedure ServerSocket1ClientConnect(Sender: TObject;

Socket: TCustomWinSocket);

procedure ServerSocket1ClientDisconnect(Sender: TObject;

Socket: TCustomWinSocket);

procedure ServerSocket1ClientRead(Sender: TObject;

Socket: TCustomWinSocket);

procedure Button1Click(Sender: TObject);

procedure FormClose(Sender: TObject; var Action: TCloseAction);

private

procedure UpdateState;//更新当前的连接信息

{ Private declarations }

public

{ Public declarations }

end;

var

MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.UpdateState;

begin

Label1.Caption:='在线人数为:' IntToStr(ServerSocket1.Socket.ActiveConnections);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:为数据库建立索引

下一篇:DELPHI超级Internet控件集--INDY