📄 client.pas
字号:
unit client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Forms,
Dialogs, StdCtrls, ScktComp, Grids, ExtCtrls,comctrls, Controls,shellapi,
winsock, Buttons;
type tserverconnectionstatus=record
tcsactiveconnections:integer;
tcslocalhost:string;
tcslocaladdress:string;
tcslocalport:integer;
tcsremotehost:string;
tcsremoteaddress:string;
tcsremoteport:integer;
end;
type tserverconnectionstatusarray=array of tserverconnectionstatus;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
GroupBox2: TGroupBox;
Label1: TLabel;
Label3: TLabel;
Label4: TLabel;
Panel1: TPanel;
StringGrid1: TStringGrid;
GroupBox1: TGroupBox;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
CheckBox1: TCheckBox;
RadioButton1: TRadioButton;
ListBox1: TListBox;
Label2: TLabel;
BitBtn2: TBitBtn;
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure deleteconnectionsinfo(clientinfo:tserverconnectionstatus);
procedure displayconnectionsinfo;
procedure checkaddinfo(clientinfo:tserverconnectionstatus);
procedure RadioButton2Click(Sender: TObject);
procedure RadioButton3Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
procedure broadcast;
procedure foundhost;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
connectflag:boolean;
arrayconnectionstatus:tserverconnectionstatusarray;
statue:string;//设置控制状态:1关机 2重启 3注销
implementation
{$R *.dfm}
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin //处理产生错误
errorcode:=0;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
stringgrid1.ColCount:=7; //初始化stringgrid1的数据。
stringgrid1.RowCount:=2;
with stringgrid1 do
begin
Cells[0,0]:=' #';
ColWidths[0]:=25;
Cells[1,0]:=' 远 程 主 机';
ColWidths[1]:=70;
Cells[2,0]:=' 远 程 地 址';
ColWidths[2]:=100;
Cells[3,0]:=' 远 程 端 口';
ColWidths[3]:=65;
Cells[4,0]:=' 本 地 主 机';
ColWidths[4]:=70;
Cells[5,0]:=' 本 地 地 址';
ColWidths[5]:=100;
Cells[6,0]:=' 本 地 端 口';
ColWidths[6]:=65;
end;
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var clientinfo:tserverconnectionstatus; //客户端连接成功时记录该客户数据。
begin
listbox1.ItemIndex:=0;
with clientinfo do
begin
tcsremotehost:=socket.RemoteHost;
tcsremoteaddress:=socket.RemoteAddress;
tcsremoteport:=socket.RemotePort;
tcslocalhost:=socket.LocalHost;
tcslocaladdress:=socket.LocalAddress;
tcslocalport:=socket.LocalPort;
checkaddinfo(clientinfo);
displayconnectionsinfo;
end;
label4.Caption :=inttostr(serversocket1.Socket.ActiveConnections );
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var clientinfo:tserverconnectionstatus;
begin
//当客户端断开连接时,显示当前连接的客户数;
clientinfo.tcsremotehost:=socket.RemoteHost;
clientinfo.tcsremoteaddress:=socket.RemoteAddress;
clientinfo.tcsremoteport:=socket.RemotePort;
clientinfo.tcslocalhost:=socket.LocalHost;
clientinfo.tcslocaladdress:=socket.LocalAddress;
clientinfo.tcslocalport:=socket.LocalPort;
deleteconnectionsinfo(clientinfo);
displayconnectionsinfo;
label4.Caption :=inttostr(strtoint(label4.Caption )-1);
end;
procedure tform1.deleteconnectionsinfo(clientinfo:tserverconnectionstatus);
var i:integer;
begin
for i:=0 to high(arrayconnectionstatus) do
begin
with arrayconnectionstatus[i] do
begin
if (tcsremoteaddress=clientinfo.tcsremoteaddress ) and
(tcsremoteport=clientinfo.tcsremoteport ) then
begin
tcsremotehost:='empty';
tcsremoteaddress:='0.0.0.0';
tcsremoteport:=0;
tcslocalhost:='';
tcslocaladdress:='';
tcslocalport:=0;
break;
end;
end;
end;
end;
procedure tform1.displayconnectionsinfo ; //在stringgrid中显示客户数据
var i:integer;
begin
listbox1.Items.Clear;
with ServerSocket1 do
begin
if (length(arrayconnectionstatus)>=1) then
stringgrid1.RowCount:=length(arrayconnectionstatus)+1
else
stringgrid1.RowCount:=2;
with stringgrid1 do
begin
for i:= 0 to high(arrayconnectionstatus) do
begin
with arrayconnectionstatus[i] do
begin
cells[0,i+1]:=inttostr(tcsactiveconnections);
cells[1,i+1]:=' '+tcsremotehost;
cells[2,i+1]:=' '+tcsremoteaddress;
cells[3,i+1]:=' '+inttostr(tcsremoteport);
cells[4,i+1]:=' '+tcslocalhost;
cells[5,i+1]:=' '+tcslocaladdress;
cells[6,i+1]:=' '+inttostr(tcslocalport);
listbox1.items.Add(tcsremotehost);
end;
end;
end;
end;
end;
procedure tform1.broadcast ; //向已登陆的全体计算机执行命令
var i:integer;
begin
with ServerSocket1.Socket do
begin
for I := 0 to (ActiveConnections - 1) do
begin
Connections[I].SendText(statue);
with arrayconnectionstatus[i] do
begin
tcsremotehost:='empty';
tcsremoteaddress:='0.0.0.0';
tcsremoteport:=0;
tcslocalhost:='';
tcslocaladdress:='';
tcslocalport:=0;
end;
end;
end;
displayconnectionsinfo ;
label4.Caption :=inttostr(strtoint(label4.caption)-1 );
end;
procedure tform1.checkaddinfo(clientinfo:tserverconnectionstatus);
var i:integer;insertin:boolean;
begin
insertin:=false;
for i:=0 to high(arrayconnectionstatus) do
begin
if (arrayconnectionstatus[i].tcsremotehost='empty' ) then
begin
insertin:=true;
break;
end;
end;
if insertin=false then
begin
setlength(arrayconnectionstatus,length(arrayconnectionstatus)+1);
i:=high(arrayconnectionstatus);
end;
with arrayconnectionstatus[i] do
begin
tcsactiveconnections:=i;
tcslocalhost:=clientinfo.tcslocalhost;
tcslocaladdress:=clientinfo.tcslocaladdress;
tcslocalport:=clientinfo.tcsremoteport;
tcsremotehost:=clientinfo.tcsremotehost;
tcsremoteaddress:=clientinfo.tcsremoteaddress;
tcsremoteport:=clientinfo.tcsremoteport;
end;
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
statue:='2';
end;
procedure TForm1.RadioButton3Click(Sender: TObject);
begin
statue:='3';
end;
procedure tform1.foundhost ;
var i,b,c:integer;
begin
for i:=0 to listbox1.Count -1 do
begin
if listbox1.Selected[i]= true then
begin
for b:=0 to serversocket1.Socket.ActiveConnections-1 do
begin
if (listbox1.Items[i]=serversocket1.Socket.Connections[b].RemoteHost)
and (serversocket1.Socket.Connections[b].Connected ) then
begin
serversocket1.Socket.Connections[b].SendText(statue);
label4.Caption :=inttostr(strtoint(label4.caption)-1 );
with arrayconnectionstatus[b] do
begin
tcsremotehost:='empty';
tcsremoteaddress:='0.0.0.0';
tcsremoteport:=0;
tcslocalhost:='';
tcslocaladdress:='';
tcslocalport:=0;
break;
end;
end;
end;
end;
end;
displayconnectionsinfo ;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if checkbox1.Checked=true then broadcast;
if RadioButton1.Checked=true then statue:='1';
if RadioButton2.Checked=true then statue:='2';
if RadioButton3.Checked=true then statue:='3';
foundhost;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -