Link to home
Start Free TrialLog in
Avatar of TheLeader
TheLeader

asked on

Indy TCP (Client/Server)

Greetings...
I Need a very Clear example of how to send & receive(commands and string)using indy TCP Client, Server Client ?
also the Server has a password to establish the connection from the client.
thank you for your time.
Avatar of alsantos
alsantos

I think that it's a lot of work to a few points.

alsantos :)
Avatar of TheLeader

ASKER

I already Increased it, and if it a lot of works, theres no worry to increased it again ;)
For the server:
- put TIdTCPServer component on your form
- adjust the port setting on the property inspector.
- for auto activation, set the property Active to True on the property inspector.
- Indy TCP server works in command/response mechanism, to add a command in your protocol,
  click on property CommandHandlers property on your property inspector then add your commands there.
- On each Command you can define the command string and use the OnCommand event to define
  your own actions.
- You can also use response (numericcode, text, etc) to standardize your responses.

Run your test server and test it first with a telnet application connecting to the defined ports.

For the client:
- Try the ReadLn and WriteLn methods from the TIdTCPClient component to read/write strings
   from/to the server.

An example code is more difficult to be read... this explanation will help more, I think.

regards,
huferry
how about checking the examples ?
basic Chat should be enough
ok I will try it "huferry"
but what about the server password ?

"Lee_Nover"
chat example(string) will be good but what about the commands & the server password !
ASKER CERTIFIED SOLUTION
Avatar of alsantos
alsantos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
now, the client side, it's more simple to understand.

unit u_Client;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient;

type
  TForm1 = class(TForm)
    IdTCPClient1: TIdTCPClient;
    Button1: TButton;
    Button2: TButton;
    lbCommunication: TListBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button3: TButton;
    Button4: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure IdTCPClient1Disconnected(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
  if IdTCPClient1.Connected then
    try
      IdTCPClient1.Disconnect;
    except on E : Exception do
      ShowMessage(E.Message);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  lbCommunication.Items.Clear;
  with IdTCPClient1 do
  begin
    Host := Edit1.Text;
    Port := StrToint(Edit2.Text);
    try
      Connect;
      lbCommunication.Items.Add(idTCPClient1.ReadLn);
      except on E : Exception do
         ShowMessage(E.Message);
      end;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  LCommand, LInString : String;
begin
  LCommand := Edit3.Text;

  with IdTCPClient1 do
  begin
    try
      WriteLn(LCommand);
      LInString := ReadLn;

      lbCommunication.Items.Insert(0,'We Send -> ' + LCommand);
      lbCommunication.Items.Insert(0,'Server Send -> ' + LInString);

      except
      on E : Exception do
        ShowMessage(E.Message);
      end;
  end;
end;

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
  lbCommunication.Items.Insert(0,'Disconnected from remote server');
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
var
    LString : String;
begin
  LString := IdTCPClient1.ReadLn;
  lbCommunication.Items.Insert(0,'Connected to remote server');
  lbCommunication.Items.Insert(0,'Server send -> ' + LString);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  with IdTCPClient1 do
  begin
    try
      WriteLn('Password alsantos');

      lbCommunication.Items.Insert(0,'We send the password alsantos');
      lbCommunication.Items.Insert(0,'Server send -> ' + ReadLn);

      except
      on E : Exception do
        ShowMessage(E.Message);
      end;
  end;
end;

end.

alsantos ;)
If you want, I can send the source code for your email.

alsantos
yes I would like to
mido@ksaguys.com
"Alsantos" your code is perfect thank you :)
but could you explain what do you mean by
except on E : Exception do
when it will do the except ?
Thanks TheLeader...

I use try/except to protect the code. So, I use:

Try
    .... // it will try the code here, but it can generate an error, for example
          // the IdTCPClient1 isn't connected to a server.
          // so it will generate an error and the Except will catch this error
          // and show a message on the screen with the cause of this error
    { here the code that can generate an exception(error) }
Except
  On E: Exception do
           showmessage(E.Message); { here catch the error an show the error message }

alsantos
I wrote this comment at friend's house. But it's me.
alsantos :)
TheLeader, and if the code dont't generate an error, it just ignore the on E: Exception do ....

alsantos
Well, Thank "alsantos"
you really deserve A :)
here you go...
Thanks TheLeader :)

alsantos
Hi, I've just tried your code above but it just locks up. Would it be possible to send me the code also, as this is a perfect example of a simple client-server I want to study.

Thanks... naru@eclipticdreams.com
hi i tried to start the server with button1, but nothing happend.
procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    IdTcpServer1.DefaultPort:=StrToInt(Edit1.Text);
    IdTCPServer1.Active := true;
    ListBox1.Items.Append('Server started')

  except on E : Exception do
      ListBox1.Items.Append('Server not started');
  end;
end;

could u also send me the source code..? nhatiboy@hotmail.com
thanx in advance
masterchow you can use IdTcpServer1.Bindings property in design mode to simply set the HostIP and Port like:     127.0.0.1:80
alsantos cannot help you anymore, the community moderators close their account.
my app use ado to handle database, and i try to use seek method, but it says "provider doesn't support this method" (index and seek method). can master help me to solve my BIG problem ...
^(~,~)^
I have to write a network management program in Delphi for monitoring switches and discovering when a switch fails. It should retrieve the status of the monitored device from its MIB file.
Could you provide me with any tips on how to design this. I have never programmed in Delphi before and am a complete newbie. But I understand all about network management.
The information will be sent to a Server every X seconds and any changes should be sent to the monitored devices, such as traps. I would be very grateful.
Thanks
Eilis
Dear TheLeader,

Can you send me the source code from alsantos about indy client server, caused of alsantos account has been closed. My email japriku2000@yahoo.com

Thanks
Dear TheLeader
Can u send me a compiled program to email : drago666@abv.bg
 Thanks
Dear TheLeader,
 Could you send me the source code to email: dunglq78@gmail.com

thanks alot