 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Konstantin N. Terskikh Guest
|
Posted: Tue Dec 30, 2003 7:04 am Post subject: Multithreaded server & Indy |
|
|
Hi, All.
I'm new in Indy & Delphi (I was a unix programmer befor).
I need a multithreaded server with blocking sockets.
On unix I can write something like this (sort of C++-pseudocode):
// client data
typedef struct _CLIENT_DATA {
int n; // socket
} CLIENT_DATA;
....
// main loop
int n;
CLIENT_DATA *cd;
....
while (true) {
// wait for new connection
n = accept();
// create new client data
cd = new CLIENT_DATA;
cd->n = n;
// create new thread, pass client data to it and run that thread
pthread_create(..., taskClient, ..., cd, ...);
// continue waiting for new connection
}
....
taskClient(void* vdata)
{
// get client data
CLIENT_DATA *cd = (CLIENT_DATA*)vdata;
// read from/write to client socket
... recv(cd->n, ...);
... send(cd->n, ...);
// and so on
// close client socket & free the client data
close(cd->n);
delete cd;
...
}
Please, tell me how can I implement this approach using Delphi & Indy?
I really need this.
Please, point me to good documentation/examples and/or describe it step
by step.
Thank you.
--
Konstantin N. Terskikh
|
|
| Back to top |
|
 |
Tony Caduto Guest
|
Posted: Tue Dec 30, 2003 7:40 am Post subject: Re: Multithreaded server & Indy |
|
|
I think Synapse would work better for you, check it out, it's exactly what
you are looking for.
http://www.ararat.cz/synapse/
It's a library, not a component, so you just add blcksock to your uses and
away you go.
See the echo server demo included with the download, you can use it as a
basis for your multiuser servers.
Tony
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote
| Quote: | Hi, All.
I'm new in Indy & Delphi (I was a unix programmer befor).
I need a multithreaded server with blocking sockets.
On unix I can write something like this (sort of C++-pseudocode):
// client data
typedef struct _CLIENT_DATA {
int n; // socket
} CLIENT_DATA;
...
// main loop
int n;
CLIENT_DATA *cd;
...
while (true) {
// wait for new connection
n = accept();
// create new client data
cd = new CLIENT_DATA;
cd->n = n;
// create new thread, pass client data to it and run that thread
pthread_create(..., taskClient, ..., cd, ...);
// continue waiting for new connection
}
...
taskClient(void* vdata)
{
// get client data
CLIENT_DATA *cd = (CLIENT_DATA*)vdata;
// read from/write to client socket
... recv(cd->n, ...);
... send(cd->n, ...);
// and so on
// close client socket & free the client data
close(cd->n);
delete cd;
...
}
Please, tell me how can I implement this approach using Delphi & Indy?
I really need this.
Please, point me to good documentation/examples and/or describe it step
by step.
Thank you.
--
Konstantin N. Terskikh
|
|
|
| Back to top |
|
 |
Konstantin N. Terskikh Guest
|
Posted: Tue Dec 30, 2003 7:56 am Post subject: Re: Multithreaded server & Indy |
|
|
Tony Caduto wrote:
| Quote: | I think Synapse would work better for you, check it out, it's exactly what
you are looking for.
http://www.ararat.cz/synapse/
It's a library, not a component, so you just add blcksock to your uses and
away you go.
See the echo server demo included with the download, you can use it as a
basis for your multiuser servers.
Tony
|
Good library, but I'm still waiting for solution implemented with Indy.
Anyway, thak you.
| Quote: |
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote in message
news:3ff12369$1 (AT) newsgroups (DOT) borland.com...
Hi, All.
I'm new in Indy & Delphi (I was a unix programmer befor).
I need a multithreaded server with blocking sockets.
On unix I can write something like this (sort of C++-pseudocode):
// client data
typedef struct _CLIENT_DATA {
int n; // socket
} CLIENT_DATA;
...
// main loop
int n;
CLIENT_DATA *cd;
...
while (true) {
// wait for new connection
n = accept();
// create new client data
cd = new CLIENT_DATA;
cd->n = n;
// create new thread, pass client data to it and run that thread
pthread_create(..., taskClient, ..., cd, ...);
// continue waiting for new connection
}
...
taskClient(void* vdata)
{
// get client data
CLIENT_DATA *cd = (CLIENT_DATA*)vdata;
// read from/write to client socket
... recv(cd->n, ...);
... send(cd->n, ...);
// and so on
// close client socket & free the client data
close(cd->n);
delete cd;
...
}
Please, tell me how can I implement this approach using Delphi & Indy?
I really need this.
Please, point me to good documentation/examples and/or describe it step
by step.
|
--
Konstantin N. Terskikh
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 30, 2003 8:05 am Post subject: Re: Multithreaded server & Indy |
|
|
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote
| Quote: | I need a multithreaded server with blocking sockets.
|
Exactly what Indy does well.
| Quote: | On unix I can write something like this (sort of C++-pseudocode):
|
Indy handles all of that for you. All you have to do is provide an event
handler for the TIdTCPServer.OnExecute event and do your reading and writing
from there, ie:
procedure TForm1::IdTCPServer1Execute(AThread: TIdPeerThread);
begin
// read from/write to client socket
AThread->Connection->ReadBuffer(...);
AThread->Connection->WriteBuffer(...);
// and so on...
AThread->Connection->Disconnect;
end;
Simply set the TIdTCPServer's Active property to True when you are ready for
the server to listen for connections, and set it to False to stop listening.
Gambit
|
|
| Back to top |
|
 |
Konstantin N. Terskikh Guest
|
Posted: Tue Dec 30, 2003 8:48 am Post subject: Re: Multithreaded server & Indy |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: | I need a multithreaded server with blocking sockets.
Exactly what Indy does well.
On unix I can write something like this (sort of C++-pseudocode):
Indy handles all of that for you. All you have to do is provide an event
handler for the TIdTCPServer.OnExecute event and do your reading and writing
from there, ie:
procedure TForm1::IdTCPServer1Execute(AThread: TIdPeerThread);
begin
// read from/write to client socket
AThread->Connection->ReadBuffer(...);
AThread->Connection->WriteBuffer(...);
// and so on...
AThread->Connection->Disconnect;
end;
Simply set the TIdTCPServer's Active property to True when you are ready for
the server to listen for connections, and set it to False to stop listening.
|
Yes, I know that. I've already worked out on such code.
May be I just don't understand how Indy works.
Imagine: many clients connecting to a server simultaneously. So, we must
accept each connection as soon as possible and pass each newely
created socket to a new thread of execution. In each thread we must read
and write from/to client's socket, close this client's socket and
terminate thread. All threads works in parallel.
Can I achieve this using technique mentioned above?
--
Konstantin N. Terskikh
|
|
| Back to top |
|
 |
Martin James Guest
|
Posted: Tue Dec 30, 2003 12:16 pm Post subject: Re: Multithreaded server & Indy |
|
|
| Quote: |
Imagine: many clients connecting to a server simultaneously. So, we must
accept each connection as soon as possible and pass each newely
created socket to a new thread of execution. In each thread we must read
and write from/to client's socket, close this client's socket and
terminate thread. All threads works in parallel.
Can I achieve this using technique mentioned above?
|
It's actually quite difficult to achieve anything but this with Indy. Indy
has, as standard, a listener thread & managed server-client threads. The
onExecute event mentioned my Remy is fired from a client-specific 'peer
thread' that calls the onExecute event in a loop.
You have no work to do. It's all in there already.
If you use the Synapse units, you have to make your own listening thread, do
the listen/accept yourself and then create or dequeue a thread to hand off
the accepted socket to. It's not difficult, but you have to do it, unlike
Indy where it's all done for you.
Rgds,
Martin
|
|
| Back to top |
|
 |
Tony Caduto Guest
|
Posted: Tue Dec 30, 2003 1:21 pm Post subject: Re: Multithreaded server & Indy |
|
|
From the C sample code it looked like that's what Konstantin wanted to do,
that's why I recomeneded Synapse.
| Quote: |
If you use the Synapse units, you have to make your own listening thread,
do
the listen/accept yourself and then create or dequeue a thread to hand off
the accepted socket to. It's not difficult, but you have to do it, unlike
Indy where it's all done for you.
Rgds,
Martin
|
|
|
| Back to top |
|
 |
Tony Caduto Guest
|
Posted: Tue Dec 30, 2003 1:24 pm Post subject: Re: Multithreaded server & Indy |
|
|
Indy is more component based, if you want to code like you where doing in C
then Synapse is for you.
Did you even download it and check it out? I don't understand why you would
want to wait for a solution in INDY considering the code you posted.
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote
| Quote: | Tony Caduto wrote:
I think Synapse would work better for you, check it out, it's exactly
what
you are looking for.
http://www.ararat.cz/synapse/
It's a library, not a component, so you just add blcksock to your uses
and
away you go.
See the echo server demo included with the download, you can use it as a
basis for your multiuser servers.
Tony
Good library, but I'm still waiting for solution implemented with Indy.
Anyway, thak you.
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote in message
news:3ff12369$1 (AT) newsgroups (DOT) borland.com...
Hi, All.
I'm new in Indy & Delphi (I was a unix programmer befor).
I need a multithreaded server with blocking sockets.
On unix I can write something like this (sort of C++-pseudocode):
// client data
typedef struct _CLIENT_DATA {
int n; // socket
} CLIENT_DATA;
...
// main loop
int n;
CLIENT_DATA *cd;
...
while (true) {
// wait for new connection
n = accept();
// create new client data
cd = new CLIENT_DATA;
cd->n = n;
// create new thread, pass client data to it and run that thread
pthread_create(..., taskClient, ..., cd, ...);
// continue waiting for new connection
}
...
taskClient(void* vdata)
{
// get client data
CLIENT_DATA *cd = (CLIENT_DATA*)vdata;
// read from/write to client socket
... recv(cd->n, ...);
... send(cd->n, ...);
// and so on
// close client socket & free the client data
close(cd->n);
delete cd;
...
}
Please, tell me how can I implement this approach using Delphi & Indy?
I really need this.
Please, point me to good documentation/examples and/or describe it step
by step.
--
Konstantin N. Terskikh
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 30, 2003 6:46 pm Post subject: Re: Multithreaded server & Indy |
|
|
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote
| Quote: | Imagine: many clients connecting to a server simultaneously. So,
we must accept each connection as soon as possible and pass
each newely created socket to a new thread of execution.
|
As I said, Indy is already doing exactly that for you. TIdTCPServer is
already multithreaded. Internally, it uses a listening thread to accept new
connections and then spawns new TIdPeerThread (or descendant) instances for
each conection. The OnExecute event is triggered in the context of those
client threads, and the AThread parameter lets you know which thread is
triggering the event at that time.
| Quote: | In each thread we must read and write from/to client's socket,
close this client's socket and terminate thread.
|
The code I showed you earlier does exactly that. Well, not exactly, the
code had some C++ syntax mixed, sorry about that. The actual code is as
follows:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
// read from/write to client socket
AThread.Connection.ReadBuffer(...);
AThread.Connection.WriteBuffer(...);
// and so on...
AThread.Connection.Disconnect;
end;
Or simplier:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
with AThread.Connection do
begin
// read from/write to client socket
ReadBuffer(...);
WriteBuffer(...);
// and so on...
Disconnect;
end;
end;
That is all you need for what you are asking for.
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Dec 30, 2003 6:47 pm Post subject: Re: Multithreaded server & Indy |
|
|
"Tony Caduto" <acaduto (AT) amsoftwaredesign (DOT) com> wrote
| Quote: | From the C sample code it looked like that's what Konstantin
wanted to do, that's why I recomeneded Synapse.
|
Sure, *if* he wanted to handle everything manually. Remember, he said that
code was for unix. He probably didn't have the kinds of ready-made
libraries that Win32 and Linux have for sockets. Why re-invent the wheel if
the work is already done for you?
Gambit
|
|
| Back to top |
|
 |
Charles Stack Guest
|
Posted: Thu Jan 01, 2004 12:45 am Post subject: Re: Multithreaded server & Indy |
|
|
Unix has plenty of ready made libraries for doing socket work. A quick look
on freshmeat.net will unveil many different projects for just about anything
you might be looking for.
On the Windows side, his options are quite good. I've found Synapse to be a
logical choice when developing client oriented software without the overhead
associated with INDY. DxSock is still, by far, the best server oriented
library out there. www.dxsock.com
Charles
|
|
| Back to top |
|
 |
Konstantin N. Terskikh Guest
|
Posted: Thu Jan 01, 2004 4:53 am Post subject: Re: Multithreaded server & Indy |
|
|
Tony Caduto wrote:
| Quote: | Indy is more component based, if you want to code like you where doing in C
then Synapse is for you.
|
No, I don't want C-like code. I need a solution for my task.
| Quote: | Did you even download it and check it out?
|
Yes of course.
I don't understand why you would
| Quote: | want to wait for a solution in INDY considering the code you posted.
|
Because Indy is a standard component(s) in Delphi (7, Enterprise, at least).
Thanx anyway for link to that really good library.
--
Konstantin N. Terskikh
|
|
| Back to top |
|
 |
Konstantin N. Terskikh Guest
|
Posted: Thu Jan 01, 2004 4:53 am Post subject: Re: Multithreaded server & Indy |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: | Imagine: many clients connecting to a server simultaneously. So,
we must accept each connection as soon as possible and pass
each newely created socket to a new thread of execution.
As I said, Indy is already doing exactly that for you. TIdTCPServer is
already multithreaded. Internally, it uses a listening thread to accept new
connections and then spawns new TIdPeerThread (or descendant) instances for
each conection. The OnExecute event is triggered in the context of those
client threads, and the AThread parameter lets you know which thread is
triggering the event at that time.
|
Ok, the last question:
Does the OnExecute (or IdTCPServer1Execute) method works in parallel for
all threads?
| Quote: | In each thread we must read and write from/to client's socket,
close this client's socket and terminate thread.
The code I showed you earlier does exactly that. Well, not exactly, the
code had some C++ syntax mixed, sorry about that. The actual code is as
follows:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
// read from/write to client socket
AThread.Connection.ReadBuffer(...);
AThread.Connection.WriteBuffer(...);
// and so on...
AThread.Connection.Disconnect;
end;
That is all you need for what you are asking for.
|
Thanx.
--
Konstantin N. Terskikh
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Jan 01, 2004 8:52 am Post subject: Re: Multithreaded server & Indy |
|
|
"Konstantin N. Terskikh" <kostus (AT) infotech (DOT) akadem.ru> wrote
| Quote: | Does the OnExecute (or IdTCPServer1Execute) method works
in parallel for all threads?
|
Yes. Each thread triggers the OnExecute handler independantly of each other
thread. In fact, every event handler that has an AThread parameter is like
that. They are all triggered in the content of client threads, each thread
running in parallel, the AThread parameter letting the event handlers know
which thread is calling them at any given moment.
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Jan 02, 2004 8:28 am Post subject: Re: Multithreaded server & Indy |
|
|
"Charles Stack" <charles> wrote
| Quote: | Unix has plenty of ready made libraries for doing socket work.
|
Perhaps, but it seemed from Konstantin's original code that he wasn't
actually using anything like that, but was instead programming the sockets
directly.
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|