delphi:通过套接字发送多个string?

我目前正在使用delphi6(是的,我知道..但它迄今为止完成工作。)

我正在使用Serversocket和客户端套接字。 当我有我的客户端连接到我的服务器,我想它发送一些信息,如computername,LAN IP,OS名称,平。

目前我只有客户端发送计算机名称到服务器,我想知道如何发送多个信息,并在我的网格相应地设置它? 这里是源代码:

客户:

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. 

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

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

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

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