Question

Indy TCP (Client/Server)

Asked by: TheLeader

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.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-10-26 at 18:14:15ID20778629
Tags

indy

,

tcp

,

server

,

client

,

client/server

Topic

Delphi Programming

Participating Experts
12
Points
100
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Indy: Is it possible to...
    Hi! I use D5 and I think about migrating to Indy. Now I wonder if it is possible to avoid installing packages, patching the IDE etc. I think about something like putting the source code in a folder and use the components like TIndyWhatever.Create(Form1)... Any hint appreciat...
  2. Indy TCP Server
    I'm Using Indy (Indy sucks!!!!) and I want to send a string via TCP Server. In Delphi 3, with standard TCP Socket, that's really easy, but now, I use the "Writeln(stringtosend)" cmd. That works when I'm on a Socket Thread procedure, but I want to do this with a exte...
  3. Indy Components Question
    Trying to get a screenshot sender for a network based application. Have tried sending a streamed image over using Indy, however, the Server app will only load the image AFTER the client has disconnected. I want the client connection to stay active, the sending code is as foll...
  4. Indy TCP Client question
    Hello, I need to receive data from a TCP Server using Indy TCP Clinet (TidTCPClient). My problem is, the data is not a string (so I cant use TidTCPClient.ReadLn). It is an array of bytes which the array length may vary. I plan to use this way : With TCPClient do begin ...
  5. Indy TCP Server help
    Hi, I'm try to use the Indy TCP Server component to send a command to a client when the user presses a button, but I can't find any procedure or function that does this. The server only allows one connection, if that helps in any way. I can send and read data just fine in th...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: alsantosPosted on 2003-10-26 at 20:08:48ID: 9624779

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

alsantos :)

 

by: TheLeaderPosted on 2003-10-26 at 23:12:54ID: 9625281

I already Increased it, and if it a lot of works, theres no worry to increased it again ;)

 

by: huferryPosted on 2003-10-27 at 02:10:19ID: 9625888

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

 

by: Lee_NoverPosted on 2003-10-27 at 03:51:03ID: 9626253

how about checking the examples ?
basic Chat should be enough

 

by: TheLeaderPosted on 2003-10-27 at 04:18:34ID: 9626364

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 !

 

by: alsantosPosted on 2003-10-27 at 04:44:33ID: 9626480

Hi, TheLeader, it isn't what I expect, but I'll help you.

he server code, it only answer to authenticated user. The user have to send the command "Password alsantos" to authenticate. If send any other command before authenticate, the server disconnect the user. You have to add 2 commandhandlers, On the idTcpServer1, click on CommandHandlers, and add 2 TIdCommandHandler with name Password and Time and Command "Password" and "Time" respectly. "The OnCommand" event of Password will call the procedure TForm1.IdTCPServer1PasswordCommand(ASender: TIdCommand) and the Time will call procedure TForm1.IdTCPServer1TimeCommand(ASender: TIdCommand); .  Now, the full source code of the server :)

unit u_Server;

interface

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

type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Button1: TButton;
    Edit1: TEdit;
    lbProcesses: TListBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure IdTCPServer1TimeCommand(ASender: TIdCommand);
    procedure IdTCPServer1NoCommandHandler(ASender: TIdTCPServer;
      const AData: String; AThread: TIdPeerThread);
    procedure IdTCPServer1Connect(AThread: TIdPeerThread);
    procedure FormCreate(Sender: TObject);
    procedure IdTCPServer1PasswordCommand(ASender: TIdCommand);
    procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Authenticated: TStringList;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    IdTcpServer1.DefaultPort:=StrToInt(Edit1.Text);
    IdTCPServer1.Active := true;
    lbProcesses.Items.Append('Server started');
  except on E : Exception do
      lbProcesses.Items.Append('Server not started');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  IdTCPServer1.Active := false;
  lbProcesses.Items.Append('Server stopped');
end;

procedure TForm1.IdTCPServer1TimeCommand(ASender: TIdCommand);
begin
  if Authenticated.IndexOf(ASender.Thread.Connection.LocalName) >= 0 then
    ASender.Thread.Connection.WriteLn(FormatDateTime('hh:nn:ss',now))
  else begin
    ASender.Thread.Connection.WriteLn('User not authenticated. Closed');
    ASender.Thread.Connection.Disconnect;
  end;
end;

procedure TForm1.IdTCPServer1NoCommandHandler(ASender: TIdTCPServer;
  const AData: String; AThread: TIdPeerThread);
begin
  if Authenticated.IndexOf(AThread.Connection.LocalName) >= 0 then
    AThread.Connection.WriteLn('Command not recognised - try again!')
  else begin
    AThread.Connection.WriteLn('User not authenticated. Closed');
    AThread.Connection.Disconnect;
  end;
end;

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
  AThread.Connection.WriteLn('Welcome to my server. You have to send the password:');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Authenticated:=TStringList.Create;
end;

procedure TForm1.IdTCPServer1PasswordCommand(ASender: TIdCommand);
begin
  if ASender.Params.Strings[0] = 'alsantos' then
  begin
    Authenticated.Add(ASender.Thread.Connection.LocalName);
    ASender.Thread.Connection.WriteLn('Password accepted.');
    lbProcesses.Items.Add('User authenticated: ' + ASender.Thread.Connection.LocalName );
  end
  else begin
    lbProcesses.Items.Add('User not authenticated: ' + ASender.Thread.Connection.LocalName );
    ASender.Thread.Connection.WriteLn('User not authenticated. Closed');
    ASender.Thread.Connection.Disconnect;
  end;
end;

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
  if Authenticated.IndexOf(AThread.Connection.LocalName) >= 0 then
    Authenticated.Delete(Authenticated.IndexOf(AThread.Connection.LocalName));
  lbProcesses.Items.Add('User disconnected: ' + AThread.Connection.LocalName );
end;

end.

 

by: alsantosPosted on 2003-10-27 at 04:46:44ID: 9626490

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 ;)

 

by: alsantosPosted on 2003-10-27 at 04:51:09ID: 9626509

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

alsantos

 

by: TheLeaderPosted on 2003-10-27 at 14:20:06ID: 9630226

yes I would like to
mido@ksaguys.com

 

by: TheLeaderPosted on 2003-10-28 at 04:37:46ID: 9633183

"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 ?

 

by: emu10k1Posted on 2003-10-28 at 06:12:53ID: 9633791

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

 

by: alsantosPosted on 2003-10-28 at 06:16:03ID: 9633811

I wrote this comment at friend's house. But it's me.
alsantos :)

 

by: alsantosPosted on 2003-10-28 at 06:19:56ID: 9633834

TheLeader, and if the code dont't generate an error, it just ignore the on E: Exception do ....

alsantos

 

by: TheLeaderPosted on 2003-10-28 at 07:40:12ID: 9634439

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

 

by: alsantosPosted on 2003-10-28 at 07:42:36ID: 9634462

Thanks TheLeader :)

alsantos

 

by: NarusegawaPosted on 2004-02-18 at 04:09:27ID: 10391505

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

 

by: masterchowPosted on 2004-04-05 at 05:05:54ID: 10756569

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

 

by: mehdi_mPosted on 2004-07-14 at 00:20:26ID: 11546433

masterchow you can use IdTcpServer1.Bindings property in design mode to simply set the HostIP and Port like:     127.0.0.1:80

 

by: emu10k1Posted on 2004-07-14 at 05:06:09ID: 11547997

alsantos cannot help you anymore, the community moderators close their account.

 

by: vhisnusanPosted on 2004-09-13 at 01:36:35ID: 12042097

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 ...
^(~,~)^

 

by: EilisPosted on 2004-10-04 at 04:57:58ID: 12216322

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

 

by: mahfudPosted on 2004-12-20 at 04:18:33ID: 12865817

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

 

by: DragomirIvanovPosted on 2005-01-30 at 11:08:53ID: 13177472

Dear TheLeader
Can u send me a compiled program to email : drago666@abv.bg
 Thanks

 

by: dunglqPosted on 2005-10-05 at 20:15:12ID: 15027823

Dear TheLeader,
 Could you send me the source code to email: dunglq78@gmail.com

thanks alot

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...