Delphi:通过套接字发送多个字符串?

时间:2013-09-01 15:37:06

标签: windows delphi sockets

我目前正在使用delphi 6(是的,我知道..但它到目前为止已完成工作。)

我正在使用Serversocket和客户端套接字。当我的客户端连接到我的服务器时,我想让它发送一些信息,如computername,lAN 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.

1 个答案:

答案 0 :(得分:7)

你已经知道了答案,因为你已经在做了。发送带有分隔符的各种字符串(在您的示例中,您使用的是/,但您也可以使用CRLF,字符串长度前缀等),然后使用一些最终分隔符来表示数据的结尾。

您的代码存在的真正问题是使用SendText()ReceiveText()SendText()无法保证一次性发送整个字符串。它返回实际发送的字节数。如果少于字符串长度,则必须再次调用SendText()以发送剩余字节。对于ReceiveText(),它只返回当时套接字上的任意数据,这可能是一个不完整的字符串,或者多个字符串合并在一起。

您正在使用较低级别的I / O方法,而无需先设计更高级别的协议来描述正在发送的数据。你需要一个协议。设计您希望数据的外观,然后按照这种方式格式化数据并使用方法发送/接收数据,然后在收到数据时根据需要拆分数据。

因此,在这种情况下,您可以发送一个包含/ - 分隔值的CRLF分隔字符串。然后服务器将读取,直到它到达CRLF,然后拆分该行并使用相应的值。