Board index » delphi » Re: What Indy component to use as serversocket (D7)??

Re: What Indy component to use as serversocket (D7)??


2003-09-07 09:50:24 PM
delphi272
To the Winsock group:
I was adviced on the nativeapi group to post this in winsock instead,
so here is a multiple post, please excuse the duplicate...
-------------------
Thanks for helping me!
I *think* I understand what you mean but there are a few items that I
am not quite getting yet, possibly because I have not worked a lot
with Indy components. Only used IdSMTP before. For general comm I used
before the old client and server winsocks of D4-D5 and they are quite
different. And I can not get my new project to work at all with them...
First:
If I put a TIdTCPServer on my form and set the port value to something
and then use Active := true to start listening, then when a client
tries to connect I want to get hold of this and instantiate the
handler for the client communication.
What I had done before is to get the socket in the OnConnect of the
server and then instantiate my handler, which has a property
Socket:TCustomWinSocket. I set the property to the socket received in
the connect and then I expect the handler to take care of the rest in
its On... events.
Now using the TIdTCPServer.OnConnect I don't see anything similar to
what I was using, so what can I do here? Obviously I must change my
handler object so it uses some other kind of socket to receive the
communication events. But here is where I am again lost.
I can not find anything useful in the AThread.Data object...
And the reference you mention to the servers OnExecute event seems to
me to mean that the actual communications tasks are handled by the
server rather than by the handlers themselves. I really want this to
be located inside the handlers so the server does not do anything
itself except manage the connections. Or did I get this all wrong,
(probably).
I also have problems with the disposal of the handler objects on
disconnect. The handler will receive the disconnect trigger (I assume)
but it cannot really destroy itself, so how can I go about that?
Is this possible?:
procedure TfrmPortServerTest.IdTcpServerConnect(AThread:
TIdPeerThread);
begin
with MyHandler.Create(AThread) do
begin
//set up whatever needed for the handler
end;
end;
or maybe:
procedure TfrmPortServerTest.IdTcpServerConnect(AThread:
TIdPeerThread);
begin
AThread.Data := MyHandler.Create(AThread);
MyHandler.Connection := AThread.Connection;
end;
procedure TfrmPortServerTest.IdTcpServerDisconnect(AThread:
TIdPeerThread);
begin
// do nothing
end;
In this case I instantiate a handler in OnConnect with the owner set
to AThread. Then on the disconnect I don't do anything myself. Will
this work like I expect such that the TIdTCPServer will free the
thread and the thread Free will Free my handler and therefore I don't
need to do anything special here except code proper actions in my
handler's Destroy method??
Second:
Where is the OnSocketEvent event located (or corresponding Indy
event)?
I need to put my code in the handler somewhere and since I can not find
the socket I used before then I am wondering what will trigger when
the client sends data to the server?
My handler is already built and I need to modify the interface now to
use Indy stuff. I have event procedures that grab the client data and
processes them. Where do i do this now with Indy?
Sorry to ask seemingly daft questions, but I am really new to this
line of thinking in Indy. Is there some "Indy for Dummies" document
out there somewhere?
TIA,
Bo
On Sun, 7 Sep 2003 11:35:27 +0200, "Danny Heijl"
<XXXX@XXXXX.COM>writes:
Quote
TIdTCPServer handles all this for you automatically.

Each client connection causes TIdTCPServer to start a new thread and
activate your OnConnect/OnDisconnect/OnExecute events in the context of this
thread. You can also configure thread pooling.

Instantiate your special server class in the OnConnect event and assign it
to the Data property of the TIdPeerThread, handle everything else in the
OnExecute event of TIdTCPServer (where you pick up your server class
reference from the Data property of the TIdPeerThread), and destroy your
special server class in the OnDisconnect event with FreeAndNil (if you don't
destroy it Indy will).

Danny
---

"Bo Berglund" <XXXX@XXXXX.COM>schreef in bericht
news:XXXX@XXXXX.COM...
>I have a problem using the TServerSocket from the Internet components
>so I wanted to find out which Indy component I can replace it with.
>But there are so many.... I cannot find the simple server socket.
>
>I want to create a server application that listens on a port and can
>accept multiple connections. For each connection an instance of my
>special server class shall be created and it will then handle the
>communication until the client disconnects. At this time the instance
>shall be destroyed (by the server???).
>How can I do this? Are there any good easy to understand examples?
>
>/Bo


 
 

Re: What Indy component to use as serversocket (D7)??

... snip ...
Quote
Now using the TIdTCPServer.OnConnect I don't see anything similar to
what I was using, so what can I do here? Obviously I must change my
handler object so it uses some other kind of socket to receive the
communication events. But here is where I am again lost.
I can not find anything useful in the AThread.Data object...
And the reference you mention to the servers OnExecute event seems to
me to mean that the actual communications tasks are handled by the
server rather than by the handlers themselves. I really want this to
be located inside the handlers so the server does not do anything
itself except manage the connections. Or did I get this all wrong,
(probably).
I am afraid you will have to turn your logic "inside out". You should read
from the socket on AThread.Connection in the OnExecute event. Indy is
blocking. Perhaps you could fire the On.. events yourself after you have
read some data in order to recycle your old logic ?
Quote

I also have problems with the disposal of the handler objects on
disconnect. The handler will receive the disconnect trigger (I assume)
but it cannot really destroy itself, so how can I go about that?
Is this possible?:
Option 2 should work.
Quote

procedure TfrmPortServerTest.IdTcpServerConnect(AThread:
TIdPeerThread);
begin
with MyHandler.Create(AThread) do
begin
//set up whatever needed for the handler
end;
end;

or maybe:

procedure TfrmPortServerTest.IdTcpServerConnect(AThread:
TIdPeerThread);
begin
AThread.Data := MyHandler.Create(AThread);
MyHandler.Connection := AThread.Connection;
end;



procedure TfrmPortServerTest.IdTcpServerDisconnect(AThread:
TIdPeerThread);
begin
// do nothing
end;

In this case I instantiate a handler in OnConnect with the owner set
to AThread. Then on the disconnect I don't do anything myself. Will
this work like I expect such that the TIdTCPServer will free the
thread and the thread Free will Free my handler and therefore I don't
need to do anything special here except code proper actions in my
handler's Destroy method??

The owner will not do anything but Indy will try to free anything
AThread.Data points to according to the documentation. You can also free the
object yourself but don't forget to set AThread.Data to Nil then.
Quote
Second:
Where is the OnSocketEvent event located (or corresponding Indy
event)?
I need to put my code in the handler somewhere and since I can not find
the socket I used before then I am wondering what will trigger when
the client sends data to the server?
My handler is already built and I need to modify the interface now to
use Indy stuff. I have event procedures that grab the client data and
processes them. Where do i do this now with Indy?

See above. No On... events, you simply read and wait for the data to arrive.
AThread.Connection represents the client TCP connection and encapsulates the
socket.
Quote
Is there some "Indy for Dummies" document
out there somewhere?
Indy articles :
www.atozedsoftware.com/indy/Articles.iwp
Danny
---