实行远程控制,实际上就是一个服务器程序(以下简称被控程序)和一个客户程序......
二、被控方的程序(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);
end;
procedure TMainForm.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
UpdateState;
end;
procedure TMainForm.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
UpdateState;
end;
procedure TMainForm.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
UpdateState;
end;
procedure TMainForm.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=Socket.ReceiveText;
Memo1.Lines.Add('从客户端接收的信息为:'+s);
Socket.SendText('接收信息成功!');
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
if Button1.Caption='启动' then
begin
Button1.Caption:='停止';
ServerSocket1.Active:=False;
ServerSocket1.Active:=True;
end else
begin
Button1.Caption:='启动';
ServerSocket1.Active:=False;
end;
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ServerSocket1.Active then
ServerSocket1.Active:=False;
end;
end.
这样,程序就编写成功。运行一下看看自己的劳动成果吧!先运行Server端的程序,然后再运行Client端的程序。界面如下:
以上程序在Delphi 5.0和 window 98下运行通过。
查看本文来源