简体   繁体   中英

(Delphi) Change Socket client name when sending a message

For some reason if I send a msg through the client I made it automatically ads the username "license.embarcadero.com:" to it so if I send a msg from the client to the server it comes out as "license.embarcadero.com: (msg)"

How can I change license.embarcadero.com: to what ever I want. If it helps i'm using berlin update 2

屏幕截图

Here is the client source: (I use the stylemanager for the 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.

Here is the server source: (I use the stylemanager for the 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.

In the screenshot the "Connected Users" window in the right lower corner shows

license.embarcadero.com 127.0.0.1 

So I assume your hosts file contains an entry like

127.0.0.1 license.embarcadero.com

The socket server performs a reverse DNS lookup for the IP address of the incoming connection and displays the associated name, which is license.embarcadero.com

So this is a problem only for localhost connections. Clients on a different computer will not show up with the same server name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM