Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A simple client/server (Delphi 2006 Win32)

Status
Not open for further replies.

Tomi71

Programmer
Oct 16, 2007
4
FI
Hello.

I'm new to Delphi and already lovin'.

Now I would need just a very simple TcpClient that can connect to a server and receive messages from it.

I've tried to look at examples but so far they are too complex.

I know I need a streamreader of sorts to attach to the socket and then read the incoming lines.

Thank you.
 
I've always used the Indy components for this. It can be a little tricky at first because the TidTCPServer is multi-threaded - I'll get to that in a moment.

I use Delphi 6, so I'm not sure what advances have been made in the later versions of Delphi for multi-thread debugging, but I've found using a log file to output messages at certain points in my code the easiest way to figure out what's going on in what order.

Drop a TidTCPServer (Srv) and a TidTCPClient (Cli) on your form. Set Srv.Port=20001, set Cli.Port=20001, set Cli.Host=localhost

These values are for testing only, the Cli.Host will contain the IP address or computer name of the PC running the server program.

Attach code to Cli.OnConnected - this will contain the commands you will send to the Srv. Attach code to the Srv.OnExecute - this will contain commands that will respond to the Cli and send commands back.

Add a button or something to the form and call Cli.Connect - this will initiate the connection.

Your code in Cli.OnConnect will look like
Code:
[b]procedure[/b] TForm1.IdTCPClient1Connected(Sender: TObject);
[b]var[/b]
  f : Boolean;
[b]begin[/b]
  [b]try[/b]
    [b]with[/b] IdTCPClient1.IOHandler [b]do[/b]
    [b]begin[/b]
      WriteLn([teal]'HELLO'[/teal]);
      f := SameText(ReadLn, [teal]'OK'[/teal]);
    [b]end[/b];
  [b]finally[/b]
    IdTCPClient1.Disconnect;
  [b]end[/b];
  [b]if[/b] f [b]then[/b]
    ShowMessage([teal]'Success'[/teal])
  [b]else[/b]
    ShowMessage([teal]'Unknown response'[/teal]);
[b]end[/b];

Your code in Srv.OnExecute will look like:
Code:
[b]procedure[/b] TForm1.IdTCPServer1Execute(AContext: TIdContext);
[b]begin[/b]
  [b]try[/b]
    [b]with[/b] AContext.Connection.IOHandler [b]do[/b]
    [b]begin[/b]
      [b]if[/b] SameText(ReadLn, [teal]'HELLO'[/teal]) [b]then[/b]
        WriteLn([teal]'OK'[/teal])
      [b]else[/b]
        WriteLn([teal]'GOAWAY'[/teal]);
    [b]end[/b];
  [b]finally[/b]
    AContext.Connection.Disconnect;
  [b]end[/b];
[b]end[/b];

A minor bug on my system means I must manually add idContext to the uses clause of the program containing a TIdTCPServer.

You need to remember that everything with the OnExecute event will execute in a separate thread, so be careful what variables you change and what components you look at. In your actual program if your program containing the Srv component will do anything but listen for connections, then you need to read up on using multiple threads and what issues they can cause.

That should get you started. Post back with more questions.
 
Thanks Griffyn, I needed this too :D
I will try it out when I get home from work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top