I tried making a simple chat system in Delphi 10.3.3 Community Edition using FireMonkey and the Indy components TIdTCPClient
and TIdTCPServer
.
It works fine if the Client and Server are located in the same WiFi network (Server = Windows 10 and Client = Android 10). I used the computer's IPv4 address shown in ipconfig
to get the computer's IP, and my mobile phone successfully connects.
But, if I use the internet IP (got it from https://www.whatismyip.com/de/), the client shows the german equivalent to "socket error # 111 connection refused", so what am I missing? I entered the IP in the edit box to connect to - so if the local IP works, why doesn't any other IP work as well?
Here is the code I used:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation, IdCustomTCPServer, IdTCPServer, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdContext, FMX.ScrollBox, FMX.Memo,
FMX.Edit;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Button1: TButton;
IdTCPClient1: TIdTCPClient;
IdTCPServer1: TIdTCPServer;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Connect(AContext: TIdContext);
procedure IdTCPServer1Disconnect(AContext: TIdContext);
procedure IdTCPServer1Execute(AContext: TIdContext);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPClient1.Host := Edit1.Text;
IdTCPClient1.Port := StrToInt(Edit2.Text);
IdTCPClient1.Connect;
if IdTCPClient1.Connected then
begin
IdTCPClient1.IOHandler.WriteLn(Edit3.Text);
IdTCPClient1.Disconnect;
end;
end;
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
var
ip:String;
begin
ip:=AContext.Binding.PeerIP;
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add('connect: ' + ip)
end);
end;
procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
var
ip:String;
begin
ip:=AContext.Binding.PeerIP;
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add('disconnect: ' + ip)
end);
end;
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
msg:String;
begin
msg:=AContext.Connection.IOHandler.ReadLn;
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add('message: ' + msg)
end);
end;
end.