We help IT Professionals succeed at work.

TCP/IP Gateway

go4ram
go4ram asked
on
1,073 Views
Last Modified: 2012-05-04
Hello, I'm a beginner in Delphi. Could anyone please tell me

1)how to send simple messages between client and server using timer functions(connection established,timeout)
2)how to Acknowledge the  received messages
3)how to send hidden messages between client and server
(if possible please let me know the full code)

Thankyou for your time and help in advance.
Comment
Watch Question

Commented:
You can start at this by going to the Delphi Help section titled "Developing Internet Applications". There are many ways to accomplish what you would like to do, including the use of TServerSocket and TClientSocket (from the Internet tab in you Delphi components) through using the Indy server and client support (see http://www.indyproject.org/Indy.html).

If you want messages to pass back and forth in the background (hidden) you need to use multithreading support or asynchronous network I/O. If you protocol is TCP rather than UDP, no formal acknowledgement would be required because TCP "guarantees" delivery. It might not hurt, though, to add your own acknowledgment (ACK) message.

Commented:
The following is source code taken from the Indy Demo folder, an "Echo" server and client:

{***************************************************************
 *
 * Project  : EchoServer
 * Unit Name: mainform
 * Purpose  : Demonstrates ECHO server
 * Date     : 21/01/2001  -  13:05:54
 * History  :
 *
 ****************************************************************}

unit mainform;

interface

uses
  {$IFDEF Linux}
  QForms, QControls, QDialogs, QStdCtrls,
  {$ELSE}
  Windows, Messages, Graphics, Controls, Forms, Dialogs, StdCtrls,
  {$ENDIF}
  SysUtils, Classes,
  IdBaseComponent, IdComponent, IdTCPServer, IdEchoServer;

type
  TForm1 = class(TForm)
    IdECHOServer1: TIdECHOServer;
    Label1: TLabel;
    lblStatus: TLabel;
    btnExit: TButton;
    procedure FormActivate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure IdECHOServer1Connect(AThread: TIdPeerThread);
    procedure IdECHOServer1Disconnect(AThread: TIdPeerThread);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$IFDEF Linux}{$R *.xfm}{$ELSE}{$R *.DFM}{$ENDIF}

{No coding is required.  Echo server is ready to go by setting Active to True}
 procedure TForm1.FormActivate(Sender: TObject);
begin
  try
    IdECHOServer1.Active := True;
  except
    ShowMessage('Permission Denied. Cannot bind reserved port due to security reasons');
    Application.Terminate;
  end;
end;


procedure TForm1.btnExitClick(Sender: TObject);
begin
if IdECHOServer1.active then
    IdECHOServer1.active := false;
Application.terminate;
end;

procedure TForm1.IdECHOServer1Connect(AThread: TIdPeerThread);
begin
lblStatus.caption := '[ Serving client ]';
end;

procedure TForm1.IdECHOServer1Disconnect(AThread: TIdPeerThread);
begin
lblStatus.caption := '[ idle - waiting next client ]';
end;

end.
---------------------------------------------------------------
{***************************************************************
 *
 * Project  : echoclient
 * Unit Name: main
 * Purpose  : Demonstrates usage of the ECHO client
 * Date     : 21/01/2001  -  12:59:11
 * History  :
 *
 ****************************************************************}

unit main;

interface

uses
  {$IFDEF Linux}
  QControls, QForms, QStdCtrls, QGraphics, QDialogs,
  {$ELSE}
  Windows, Messages, Graphics, Controls, Forms, Dialogs, StdCtrls,
  {$ENDIF}
  SysUtils, Classes, IdComponent, IdTCPConnection, IdTCPClient, IdEcho,
  IdBaseComponent;

type
  TformEchoTest = class(TForm)
    edtSendText: TEdit;
    lblTextToEcho: TLabel;
    lblTotalTime: TLabel;
    edtEchoServer: TEdit;
    lblEchoServer: TLabel;
    btnConnect: TButton;
    btnDisconnect: TButton;
    IdEcoTestConnection: TIdEcho;
    lblReceivedText: TLabel;
    Button1: TButton;
    lablTime: TLabel;
    lablReceived: TLabel;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnConnectClick(Sender: TObject);
    procedure btnDisconnectClick(Sender: TObject);
    procedure edtEchoServerChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  formEchoTest: TformEchoTest;

implementation
{$IFDEF Linux}{$R *.xfm}{$ELSE}{$R *.DFM}{$ENDIF}

procedure TformEchoTest.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  IdEcoTestConnection.Disconnect;
end;

procedure TformEchoTest.btnConnectClick(Sender: TObject);
begin
  try
    IdEcoTestConnection.Connect;
    {we only can echo after we connect to the server}
    edtSendText.Enabled := True;
    edtSendText.color := clWhite;
    btnConnect.Enabled := False;
    btnDisconnect.Enabled := True;
  except
    IdEcoTestConnection.Disconnect;
  end; //try..except
end;

procedure TformEchoTest.btnDisconnectClick(Sender: TObject);
begin
  IdEcoTestConnection.Disconnect;
  btnConnect.Enabled := True;
  edtSendText.Enabled := False;
  edtSendText.color := clSilver;
  btnDisconnect.Enabled := False;
end;

procedure TformEchoTest.edtEchoServerChange(Sender: TObject);
begin
  IdEcoTestConnection.Host := edtEchoServer.Text;
end;

procedure TformEchoTest.Button1Click(Sender: TObject);
begin
  {This echos the text to the server}
  lablReceived.Caption := IdEcoTestConnection.Echo ( edtSendText.Text );
  {This displays the round trip time}
  lablTime.Caption := IntToStr ( IdEcoTestConnection.EchoTime );
end;

end.

Commented:
Could you describe a little more what your needs are concerning "timeout"? What do you picture happening that should timeout?

Author

Commented:
Hello sftweng, thankyou for suggesting me some tips to solve the task. I'm practising with the demos now in indy project. . Let me explain what i meant by ''timeout''.. ie setting timer for message handling and to display ''timeout'' if the message couldnt be exchanged between client and server.

Commented:
But if the messages are "hidden", wouldn't you just want to continue to rety sending? Or is it that not all messages are hidden?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Sftweng, Thankyou for ur reply. All messages are hidden (sorry for my delay reply as I had to confirm.)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.