How connect a client app to server app, passing through a third app hosted in a VPS (Virtual Private Server)?

Viewed 178

I have a client > server application where are estabilished connections using Socket components (TClientSocket of client connects to TServerSocket of server). Now i wish that clients connect first to a third application (a kind of "bridge of connection" or "repeater") that will run in a Windows VPS and must direct this clients connections to server application running on server pc. Example:

enter image description here

Probably (i not sure) this kind of "bridge of connection" or "repeater" that will running on VPS can be similar to server application that will run on server pc, but i not know how connect and make the managment of connection/desconnection and the send/receive of data between these three applications. Someone could say me how this could be made of a simply way and if possible also give a code example about this?

Below follows code of client and server (server that will run on server pc), this is all that i have until now.

Client:

uses
  ScktComp;

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    Timer1: TTimer;
    Label1: TLabel;
    procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocket1Connecting(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure Timer1Timer(Sender: TObject);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket);
begin
  Label1.Caption := 'CONNECTED';
end;

procedure TForm1.ClientSocket1Connecting(Sender: TObject; Socket: TCustomWinSocket);
begin
  Label1.Caption := 'CONNECTING...';
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  Label1.Caption := 'DISCONNECTED';
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if not ClientSocket1.Active then
    ClientSocket1.Active := true;
end;

procedure TForm1.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  s: string;
begin
  s := Socket.ReceiveText;
  if s = 'CMD' then
    Socket.SendText('Hello, here is Client: ' + Socket.LocalHost + '!');
end;

end.

Server:

uses
  ScktComp;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ServerSocket1: TServerSocket;
    PopupMenu1: TPopupMenu;
    SON: TMenuItem;
    SOFF: TMenuItem;
    SCMD: TMenuItem;
    procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure SONClick(Sender: TObject);
    procedure SOFFClick(Sender: TObject);
    procedure SCMDClick(Sender: TObject);
    procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
  Item: TListItem;
begin
  Item := ListView1.Items.Add;
  Item.Caption := IntTostr(Socket.Handle);
  Item.SubItems.Add(Socket.RemoteAddress);
  Item.SubItems.Add(Socket.RemoteHost);
  Item.Data := Socket.Data;
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
var
  Item: TListItem;
begin
  Item := ListView1.FindCaption(0, inttostr(Socket.Handle), false, true, false);
  if Item <> nil then
    Item.Delete;
end;

procedure TForm1.ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0;
end;

procedure TForm1.SONClick(Sender: TObject);
begin
  ServerSocket1.Active := true;
end;

procedure TForm1.SOFFClick(Sender: TObject);
begin
  ServerSocket1.Active := false;
end;

procedure TForm1.SCMDClick(Sender: TObject);
begin
  if ListView1.Selected = nil then
    exit;
  ServerSocket1.Socket.Connections[ListView1.ItemIndex].SendText('CMD');
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  s: string;
begin
  s := Socket.ReceiveText;
  if s <> '' then
    ShowMessage(s);
end;

end.

EDITION:

This ask was solved with help of this question/answer. Thank you to @afarah by point to right direction (proxy server) :D.

0 Answers
Related