Board index » delphi » Indy TCP/IP Server/Client

Indy TCP/IP Server/Client

Hi,

I am using the Indy Component TCP/IP Server /Client. I have an application
which is used as a server and another is the client. Now I want to send a
request to the server which should recognise the request and return the
required information back to the client but somehow I cannot find away out
how to send a request to the server.

I tried to send a Write cmd from the client to the server but It seems that
the server does not find the message. How do I know from the server when a
string has been send to it so that then I can check what the request is and
send back the result.

Thanks
Ivan

 

Re:Indy TCP/IP Server/Client


There are demos for Indy provided at the webpage showing what you want. I
don't know if my demo already included in the package there but it shows the
using of ReadBuffer/WriteBuffer and how to solve the problem that you don't
know when the server sends something to the client.

Helge.

Re:Indy TCP/IP Server/Client


Try using a WriteLn('YOUR STRING');
check if you have the host adres correct and the port
The port is default set to 0, this should be something else like 80 on both
sides

"Ivan Sammut" <samm...@hotmail.com> schreef in bericht
news:3b6a668c_1@dnews...

Quote
> Hi,

> I am using the Indy Component TCP/IP Server /Client. I have an application
> which is used as a server and another is the client. Now I want to send a
> request to the server which should recognise the request and return the
> required information back to the client but somehow I cannot find away out
> how to send a request to the server.

> I tried to send a Write cmd from the client to the server but It seems
that
> the server does not find the message. How do I know from the server when a
> string has been send to it so that then I can check what the request is
and
> send back the result.

> Thanks
> Ivan

Re:Indy TCP/IP Server/Client


I have tried to do that but when I send  a message to the server on which
event should I trigger the read on the server

Ivan

Quote
"Fons Seesink" <FSees...@actisoft.nl> wrote in message

news:3b6a693e$2_2@dnews...
Quote
> Try using a WriteLn('YOUR STRING');
> check if you have the host adres correct and the port
> The port is default set to 0, this should be something else like 80 on
both
> sides

> "Ivan Sammut" <samm...@hotmail.com> schreef in bericht
> news:3b6a668c_1@dnews...
> > Hi,

> > I am using the Indy Component TCP/IP Server /Client. I have an
application
> > which is used as a server and another is the client. Now I want to send
a
> > request to the server which should recognise the request and return the
> > required information back to the client but somehow I cannot find away
out
> > how to send a request to the server.

> > I tried to send a Write cmd from the client to the server but It seems
> that
> > the server does not find the message. How do I know from the server when
a
> > string has been send to it so that then I can check what the request is
> and
> > send back the result.

> > Thanks
> > Ivan

Re:Indy TCP/IP Server/Client


Indy is not based on events. It's blocking. Read the intro on the Indy
website to give you a better idea. You put the code of the server in the
OnExecute event and call ReadLn.

Re:Indy TCP/IP Server/Client


Here is what I am doing

Server
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var a : String;
begin
Form1.Caption := 'Executed';
a := ATHread.Connection.ReadLn;
Form1.Caption := a;
end;

Client
procedure TForm1.Button1Click(Sender: TObject);
begin
cl1.Connect;
cl1.WriteLn('Ivan');
end;

procedure TForm1.cl1Connected(Sender: TObject);  file://TTcpClientSocket
begin
Form1.Caption := 'Connected';
end;

I try to send the word "Ivan" from the Client to the server but somehow the
server never gets the word. Can you see any possible error. By the way there
is connection between the two ports.
Ivan

"Hadi Hariri - Team Indy" <hadi...@pbe.com> wrote in message
news:3b6a736d_1@dnews...

Quote
> Indy is not based on events. It's blocking. Read the intro on the Indy
> website to give you a better idea. You put the code of the server in the
> OnExecute event and call ReadLn.

Re:Indy TCP/IP Server/Client


Create a client with a TMemo and a TButton on it, as well as the
TIdTCPClient and TIdAntifreeze components from the Indy package. Of the
TIdTCPClient set the Host = your computers IP and set the Port = what ever
port you wish, lets say 9000.

In the OnClick of the button put in this code:

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  IdTCPClient1.Connect;
  try
    IdTCPClient1.WriteLn('Hello Server !!');
    Memo1.Lines.Append(IdTCPClient1.ReadLn());
  finally
    IdTCPClient1.Disconnect;
  end;
end;

Thats it for the Client.

-----------------

For the server create a form with a memo, TIdTCPServer and TIdAntifreeze.
For the TIdTCPServer set the Bindings property to :9000, the DefaultPort to
9000.

In the forms OnCreate event activate the server by setting:

    IdTCPServer1.Active := True;

In the OnExecute event of the TIdTCPServer put this code:

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
  try
    Memo1.Lines.Append(AThread.Connection.ReadLn());
    AThread.Connection.WriteLn('Hello Client !!');
  finally
    AThread.Connection.Disconnect;
  end;
end;

==================

Hope this helps you.

Best regards

S?ren

Re:Indy TCP/IP Server/Client


You should NOT update the GUI from within OnExecute. OnExecute is run i
nthe context of a thread. PLease look at the demos first, it shows you the
basics. Also read the articles on the website.

Re:Indy TCP/IP Server/Client


Quote
>procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
>begin
>  try
>    Memo1.Lines.Append(AThread.Connection.ReadLn());
>    AThread.Connection.WriteLn('Hello Client !!');
>  finally
>    AThread.Connection.Disconnect;
>  end;
>end;

Actually this is incorrect. You shouln't be updated the GUI from within
OnExecute. It's a thread!

Re:Indy TCP/IP Server/Client


"Hadi Hariri - Team Indy" <hadi...@pbe.com> skrev i en meddelelse
news:3b6c4041_1@dnews...

Quote

> Actually this is incorrect. You shouln't be updated the GUI from within
> OnExecute. It's a thread!

Point well taken. :-) It just happen to work. Lucky me I guess. :-)

Best regards
S?ren Tauber Lassen
s...@tauberdata.nospam.dk

Re:Indy TCP/IP Server/Client


It will work normally, but what happens after that is, well.... :)

Other Threads