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

时间:2017-02-24 09:59:06

标签: sockets delphi serversocket delphi-10.1-berlin

出于某种原因,如果我通过客户端发送消息,我会自动将用户名" license.embarcadero.com:"因此,如果我从客户端向服务器发送一个消息,它就会出现" license.embarcadero.com:(msg)"

如何将license.embarcadero.com更改为我想要的内容。 如果它有助于我使用berlin update 2

Screenshot

这是客户端来源:(我使用风格管理器为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.

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

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.

1 个答案:

答案 0 :(得分:2)

在屏幕截图中," Connected Users"右下角的窗口显示

license.embarcadero.com 127.0.0.1 

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

的条目
127.0.0.1 license.embarcadero.com

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

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