Delphi中远程控制的实现
2008-04-09 04:18:27来源:互联网 阅读 ()
一、主控程序(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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:为数据库建立索引
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash