(Delphi)发送消息时更改套接字客户端名称

(Delphi) Change Socket client name when sending a message

出于某种原因,如果我通过客户端发送一条消息,我会自动将用户名 "license.embarcadero.com:" 广告给它,所以如果我从客户端向服务器发送一条消息,它会显示为 "license.embarcadero.com: (msg)"

如何将 license.embarcadero.com: 更改为我想要的。 如果有帮助,我正在使用柏林更新 2

这是客户端来源:(我使用 stylemanager 作为 ui)

unit uClient;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
  System.Win.ScktComp;

type
  TForm1 = class(TForm)
    pnl1: TPanel;
    grp1: TGroupBox;
    grp2: TGroupBox;
    mmochat: TMemo;
    edtchat: TEdit;
    btnsend: TButton;
    lbl1: TLabel;
    edtip: TEdit;
    lbl2: TLabel;
    edtport: TEdit;
    btnconnect: TButton;
    lblstatus: TLabel;
    clntsckt1: TClientSocket;
    procedure btnconnectClick(Sender: TObject);
    procedure clntsckt1Connect(Sender: TObject; Socket: TCustomWinSocket);
    procedure clntsckt1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure clntsckt1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure btnsendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnsendClick(Sender: TObject);
begin
  clntsckt1.Socket.SendText(edtchat.Text);
  mmochat.Lines.Add('Me: ' + edtchat.Text);
end;

procedure TForm1.btnconnectClick(Sender: TObject);
begin
  edtip.Enabled := False;
  edtport.Enabled := False;
  btnconnect.Enabled := False;

  clntsckt1.Host := edtip.Text;
  clntsckt1.Port := StrToInt(edtport.Text);
  clntsckt1.Active := True;
end;

procedure TForm1.clntsckt1Connect(Sender: TObject; Socket: TCustomWinSocket);
begin
  mmochat.Clear;
end;

procedure TForm1.clntsckt1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  lblstatus.Caption := 'Disconnected...';
end;

procedure TForm1.clntsckt1Read(Sender: TObject; Socket: TCustomWinSocket);
begin
  mmochat.Lines.Add(clntsckt1.Socket.ReceiveText);
end;

end.

这是服务器源:(我使用 stylemanager 作为 ui)

unit uServer;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
  System.Win.ScktComp, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    pnl1: TPanel;
    grp1: TGroupBox;
    mmochat: TMemo;
    grp2: TGroupBox;
    lbl1: TLabel;
    edtport: TEdit;
    lbl2: TLabel;
    lbl3: TLabel;
    edtstatus: TEdit;
    btnlisten: TButton;
    srvrsckt1: TServerSocket;
    btnstop: TButton;
    lst1: TListBox;
    edtchat: TEdit;
    btnsend: TButton;
    procedure btnlistenClick(Sender: TObject);
    procedure srvrsckt1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure btnstopClick(Sender: TObject);
    procedure btnsendClick(Sender: TObject);
    procedure srvrsckt1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnsendClick(Sender: TObject);
begin
  srvrsckt1.Socket.Connections[0].SendText('server: ' + edtchat.Text);
  mmochat.Lines.Add('server: ' + edtchat.Text);
  edtchat.Clear;
end;

procedure TForm1.btnlistenClick(Sender: TObject);
var
  port : Integer;
begin
  btnlisten.Enabled := False;
  btnstop.Enabled := True;
  edtport.Enabled := False;

  port := StrToInt(edtport.Text);
  srvrsckt1.Port := port;
  srvrsckt1.Active := True;
  edtstatus.Text := 'Listening...';
end;

procedure TForm1.btnstopClick(Sender: TObject);
begin
  btnlisten.Enabled := True;
  btnstop.Enabled := False;
  edtport.Enabled := True;

  srvrsckt1.Active := False;
  edtstatus.Text := 'Stopped Listening...';
end;

procedure TForm1.srvrsckt1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  lst1.Items.Add(Socket.RemoteHost + '  ' + Socket.RemoteAddress);
end;

procedure TForm1.srvrsckt1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
begin
  mmochat.Lines.Add(Socket.RemoteHost + ': ' + Socket.ReceiveText);
end;

end.

截图右下角的"Connected Users"window显示

license.embarcadero.com 127.0.0.1 

所以我假设您的 hosts 文件包含类似

的条目
127.0.0.1 license.embarcadero.com

套接字服务器对传入连接的 IP 地址执行反向 DNS 查找并显示关联名称,即 license.embarcadero.com

所以这只是本地主机连接的问题。不同计算机上的客户端不会显示相同的服务器名称。