Link to home
Start Free TrialLog in
Avatar of MauricioMaia
MauricioMaia

asked on

TidTCPServer: Limiting bandwidth (Indy Components)

I´m making a server using TidTCPServer and I wanna to limit the bandwidth used by every client or even better (if possible) used by the whole  TCPServer.
I´m have assigned a TidIOServerHandler into the TCP server and at every connect I create a TidIOHandlerThrottle. Like that:

procedure TVideoForm.TCPServerConnect(AThread: TIdPeerThread);
var
  LClient: TIdTCPConnection;
  Throttle: TidIOHandlerThrottle;
  IOHandler: TidIOHandlerSocket;
begin
  LClient := AThread.Connection;
  Throttle:= TidIOHandlerThrottle.Create(nil);
  Throttle.BytesPerSec:=BandWitdh

  Throttle.ChainedHandler:=LClient.IOHandler;
  LClient.IOHandler:=Throttle;


end;


But it isn't working. The server closes the connection at every connect.
Can somebody help me with that?
Thanks..
Avatar of ceoworks
ceoworks

Avatar of MauricioMaia

ASKER

Thanks for the help Aktay Sancak,

The topic you said is using a TCPClient, I'm having problems using a TCPServer.
Because I don't know how I assing the Throttle to every client Thread that connets to the server.
Mauricio i didn't noticed that the link i gave you was your question, sorry about it :P

I had put an IdIOHandlerThrottle1 and IdIOHandlerSocket1 on the form then i had set IdIOHandlerThrottle1's IOHandler property to IdIOHandlerSocket1. And then in TcpServer's OnConnect :

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
  AThread.Connection.IOHandler := IdIOHandlerThrottle1;
end;

That's all you need to do.. Of course you can dynamically create IdIOHandlerThrottle and IdIOHandlerSocket and set IdIOHandlerThrottle's IOHandler property to IdIOHandlerSocket.

Cheers,

Oktay Sancak
Thanks Oktay,

Just one more question:
Can I use only one Throttle and IOHandlerSocket for all the server connections?
Or,like you said, I will have to create one instance of each for every connection?
When I create thing dinamically I need to call methods like open  or bind of the IOHandlers, or is everything transparent?
Thanks.
Mauricio.
Mauricio it depends on your needs. If you needs to have a complex system which have to limit each connection's bandwidth seperately, you should choose to create a new throttle for each. If you only wants to control the bandwidth of all the connections together, you don't need to create a new throttle for each of them.

<When I create thing dinamically I need to call methods like open  or bind of the IOHandlers, or is everything transparent?>

For to limit the bandwidth of the connections, you don't need to do anything else than i told you. Only don't forget to set IdIOHandlerThrottle's BitsPerSec, BytesPerSec properties :)

Regards,

Oktay Sancak
Thanks for the help Oktay.
I made what you said me with a HttpServer the codes look like that
The Throttle is chained with a TidIOHAndlerSocket

procedure TVideoForm.HTTPServerConnect(AThread: TIdPeerThread);
var
  LClient: TIdTCPConnection;
begin
  LClient := AThread.Connection;

  HttpThrottle.BytesPerSec:=128000;
  LClient.IOHandler:=HttpThrottle;


end;


But the server closes the connection. If I made a telnet localhost 80 the connection is lost. I get no response from the server. Do you know what is happening?
Thanks..
Mauricio.
ASKER CERTIFIED SOLUTION
Avatar of ceoworks
ceoworks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I made just like your code with my idTCPserver that sends JPG images.(The onExecute event sends the JPEGS)
If I put the  AThread.Connection.IOHandler := IdIOHandlerThrottle1; on the OnConnect event the server disconnects the client.
If I put the AThread.Connection.IOHandler := IdIOHandlerThrottle1; on the OnExecute event the client stays connected but no transfer is made. I have set the Bytespersecond to higher values.
And even using Bytespersecond=0 (bandwidth ilimited) there is no transfer.
The code you post above does not make any transfer, did you try to send a stream to see if it works?

Using the same code with a idHTTPServer if I put the code on the OnConnection event the server disconnects the client. When I put on the OnExecute, initially things seems to work,  but later I realize that the OnExecute is never called by the idHttpServer (I put a Breakpoint)
If I put on the onCommandGet the server disconnects the client.

I have talked with some guys on the Indy newsgroups they said me that the OnConnect is the right place for assing the throttle. But there the server disconnect the client.






Mauricio please take a look at Indy's related official demo about it from http://www.atozedsoftware.com/indy/Demos/Throttler-2.iwp

Regards,

Oktay
What is the result ? Could you solve your problem with this example ?
Sorry for the response delay ceoworks...
I have made a code based on the official demo, but using servers instead of clients..
It still does not work.. I have put the sources here http://www.campanhamg.com.br/indy.zip

Can you take a look?

Thanks...
Mauricio.