我目前正在使用delphi 6(是的,我知道......但到目前为止它已经完成了工作。)
我正在使用服务器套接字和客户端套接字。当我的客户端连接到我的服务器时,我想让它发送一些信息,比如计算机名、局域网 IP、操作系统名称、ping。
目前我只有客户端将计算机名称发送到服务器,我想知道如何发送多个信息,并在我的网格中进行相应设置?这是源代码:
客户:
unit client1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp;
type
TForm1 = class(TForm)
Client1: TClientSocket;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Client1Connect(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Getusernamefromwindows: string;
var
iLen: Cardinal;
begin
iLen := 256;
Result := StringOfChar(#0, iLen);
GetUserName(PChar(Result), iLen);
SetLength(Result, iLen);
end;
function Getcomputernamefromwindows: string;
var
iLen: Cardinal;
begin
iLen := MAX_COMPUTERNAME_LENGTH + 1;
Result := StringOfChar(#0, iLen);
GetComputerName(PChar(Result), iLen);
SetLength(Result, iLen);
end;
function osver: string;
begin
result := 'Unknown';
case Win32MajorVersion of
4:
case Win32MinorVersion of
0: result := 'windows 95';
10: result := 'Windows 98';
90: result := 'Windows ME';
end;
5:
case Win32MinorVersion of
0: result := 'windows 2000';
1: result := 'Windows XP';
end;
6:
case Win32MinorVersion of
0: result := 'Windows Vista';
1: result := 'Windows 7';
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Client1.Host := '192.168.1.106';
Client1.Active := true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Client1.Active := false;
end;
procedure TForm1.Client1Connect(Sender: TObject; Socket: TCustomWinSocket);
begin
Client1.Socket.SendText(Getcomputernamefromwindows + '/' + Getusernamefromwindows);
(*Upon connection to server, I would like it send the os name, but as you
can see I already have SendText being used*)
end;
end.
服务器:
unit server1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ScktComp, Grids, DBGrids;
type
TForm1 = class(TForm)
Server1: TServerSocket;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Server1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
With StringGrid1 do begin
Cells[0,0] := 'Username';
Cells[1,0] := 'IP Address';
Cells[2,0] := 'Operating System';
Cells[3,0] := 'Ping';
end;
end;
(* cells [0,0][1,0][2,0][3,0]
are not to be changed for
these are used to put the
titles in
*)
procedure TForm1.Server1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
begin
with StringGrid1 do begin
Cells[0,1] := Socket.ReceiveText;
Cells[1,1] := Server1.Socket.Connections[0].RemoteAddress;
(*in this area I want it to receive the os version info and place it in
Cells[2,1]*)
end;
end;
end.