Discussion:
Stop and start a Binding (TIdSocketHandle)
(too old to reply)
Melih OZAL
2008-07-02 19:44:12 UTC
Permalink
I have two binding on a TCPServer and i want to stop and start any of them
but it seems to be imposible.

To stop a binding i use TCPServer.Bindings[1].Closesocket; and it works.

To start a binding
TCPServer.Bindings[1].AllocateSocket;
TCPServer.Bindings[1].Bind;
TCPServer.Bindings[1].Listen;
But this is not working. I can connect to the binding but OnConnect does not
fire.
Is there something that i miss. I am really tired and need help.

Thanks for any help...

Melih
Remy Lebeau (TeamB)
2008-07-03 01:25:09 UTC
Permalink
Post by Melih OZAL
I have two binding on a TCPServer and i want to stop
and start any of them but it seems to be imposible.
TIdTCPServer does not support that.
Post by Melih OZAL
To start a binding
That is not enough. TIdTCPServer is multi-threaded. You are bypassing its
ability to create a new thread to handle the new socket.
Post by Melih OZAL
I can connect to the binding but OnConnect does not fire.
Because there is no thread running to accept client connections from the new
socket.
Post by Melih OZAL
Is there something that i miss
No. TIdTCPserver simply does not support what you are asking.


Gambit
Remy Lebeau (TeamB)
2008-07-03 01:26:14 UTC
Permalink
Post by Remy Lebeau (TeamB)
TIdTCPServer does not support that.
To do what you are attempting, you will have to use multiple TIdTCPServer
objects, each one with a single Binding. You can then activate/deactivate
each TIdTCPServer as needed.


Gambit
Melih OZAL
2008-07-03 08:38:20 UTC
Permalink
Hi Remy,

Thanks for your advice but i cant get rid of this problem.
I want to have a TCP server and want to connect this server both
unsecure(port 4990) and over TLS (port 4991).
All resources are shared so i used multiple TIdTCPServer with a shared
threadscheduler but you said that this is wrong.
Now i solved this problem with binding. One binding for unsecure connectings
and the other handles TLS connections.
I want to be able to start/stop any of these servers.
Please give me an advice to solve this problem.

Thanks again.

Melih
Post by Remy Lebeau (TeamB)
Post by Remy Lebeau (TeamB)
TIdTCPServer does not support that.
To do what you are attempting, you will have to use multiple TIdTCPServer
objects, each one with a single Binding. You can then activate/deactivate
each TIdTCPServer as needed.
Gambit
Remy Lebeau (TeamB)
2008-07-03 17:18:07 UTC
Permalink
Post by Melih OZAL
I want to have a TCP server and want to connect this
server both unsecure(port 4990) and over TLS (port 4991).
As I explained to you earlier, you will have to use two separate
TIdTCPServer objects, each with a single Binding, in order to allow the type
of Binding restarts you want. A single TIdTCPServe with multiple Bindings
will not be able to do it. For example:

IdTCPServer1->Bindings->Clear();
TIdSocketHandle *Binding = IdTCPServer1->Bindings->Add();
Binding->IP = "...";
Binding->Port = 4990;
IdTCPServer1->Active = true;

IdTCPServer2->Bindings->Clear();
Binding = IdTCPServer2->Bindings->Add();
Binding->IP = "...";
Binding->Port = 4991;
IdTCPServer2->IOHandler = IdServerIOHandlerSSLOpenSSL1;
// configure IOHandler as needed...
IdTCPServer2->Active = true;

Both TIdTCPServers can share the same event handlers so you do not have to
duplicate your code twice.
Post by Melih OZAL
All resources are shared so i used multiple TIdTCPServer
with a shared threadscheduler but you said that this is wrong.
Correct. Each TIdTCPServer needs its own scheduler.
Post by Melih OZAL
Now i solved this problem with binding. One binding for
unsecure connectings and the other handles TLS connections.
You can do that with multiple TIdTCPServer objects as well. One
TIdTCPServer manages unsecured connections. The other TIdTCPServer manages
TLS connections. See above.
Post by Melih OZAL
I want to be able to start/stop any of these servers.
As I already explained, you cannot start/stop individual Bindings of a
single TIdTCPServer. With multiple TIdTCPServer objects setup as shown
above, you can simply set the Active property of each one when needed:

IdTCPServer1->Active = false;
...
IdTCPServer1->Active = true;


IdTCPServer2->Active = false;
...
IdTCPServer2->Active = true;



Gambit
Melih OZAL
2008-07-03 18:27:27 UTC
Permalink
I can get and lock the connected clients with IdTCPServer1.Context.LockList
on unsecure server.
But how can i get and lock unsecure and and TLS connected clients at the
same time.
Is it safe to use a lock both of them

lock1 := IdTCPServer1.Context.LockList;
lock2 := IdTCPServer2.Context.LockList;
Add lock2 list to lock1 list and perform operations on lock1.

Thanks,
Melih
Post by Remy Lebeau (TeamB)
Post by Melih OZAL
I want to have a TCP server and want to connect this
server both unsecure(port 4990) and over TLS (port 4991).
As I explained to you earlier, you will have to use two separate
TIdTCPServer objects, each with a single Binding, in order to allow the
type of Binding restarts you want. A single TIdTCPServe with multiple
IdTCPServer1->Bindings->Clear();
TIdSocketHandle *Binding = IdTCPServer1->Bindings->Add();
Binding->IP = "...";
Binding->Port = 4990;
IdTCPServer1->Active = true;
IdTCPServer2->Bindings->Clear();
Binding = IdTCPServer2->Bindings->Add();
Binding->IP = "...";
Binding->Port = 4991;
IdTCPServer2->IOHandler = IdServerIOHandlerSSLOpenSSL1;
// configure IOHandler as needed...
IdTCPServer2->Active = true;
Both TIdTCPServers can share the same event handlers so you do not have to
duplicate your code twice.
Post by Melih OZAL
All resources are shared so i used multiple TIdTCPServer
with a shared threadscheduler but you said that this is wrong.
Correct. Each TIdTCPServer needs its own scheduler.
Post by Melih OZAL
Now i solved this problem with binding. One binding for
unsecure connectings and the other handles TLS connections.
You can do that with multiple TIdTCPServer objects as well. One
TIdTCPServer manages unsecured connections. The other TIdTCPServer
manages TLS connections. See above.
Post by Melih OZAL
I want to be able to start/stop any of these servers.
As I already explained, you cannot start/stop individual Bindings of a
single TIdTCPServer. With multiple TIdTCPServer objects setup as shown
IdTCPServer1->Active = false;
...
IdTCPServer1->Active = true;
IdTCPServer2->Active = false;
...
IdTCPServer2->Active = true;
Gambit
Melih OZAL
2008-07-03 22:23:09 UTC
Permalink
Thanks soooooooooooo much... You are really great.

Melih
Post by Melih OZAL
I can get and lock the connected clients with
IdTCPServer1.Context.LockList on unsecure server.
You can do the same thing on the TLS server as well.
Post by Melih OZAL
But how can i get and lock unsecure and and TLS connected
clients at the same time.
They are two separate lists. You can lock and unlock them independantly
of each other.
Post by Melih OZAL
Is it safe to use a lock both of them
Yes.
Post by Melih OZAL
lock1 := IdTCPServer1.Context.LockList;
lock2 := IdTCPServer2.Context.LockList;
Add lock2 list to lock1 list and perform operations on lock1.
No, that is not safe to do. If you need both lists merged together, for
whatever reason, then you have to add the individual items to your own
myList := TList.Create;
try
lock1 := IdTCPServer1.Context.LockList;
try
lock2 := IdTCPServer2.Context.LockList;
try
myList.Capacity := lock1.Count + lock2.Count;
for I := 0 to lock1.Count-1 do mylist.Add(lock1.Items[I]);
for I := 0 to lock2.Count-1 do mylist.Add(lock2.Items[I]);
// use myList as needed...
finally
IdTCPServer2.Context.UnlockList;
end;
finally
IdTCPServer1.Context.UnlockList;
end;
finally
myList.Free;
end;
Gambit
Remy Lebeau (TeamB)
2008-07-03 22:15:54 UTC
Permalink
Post by Melih OZAL
I can get and lock the connected clients with
IdTCPServer1.Context.LockList on unsecure server.
You can do the same thing on the TLS server as well.
Post by Melih OZAL
But how can i get and lock unsecure and and TLS connected
clients at the same time.
They are two separate lists. You can lock and unlock them independantly of
each other.
Post by Melih OZAL
Is it safe to use a lock both of them
Yes.
Post by Melih OZAL
lock1 := IdTCPServer1.Context.LockList;
lock2 := IdTCPServer2.Context.LockList;
Add lock2 list to lock1 list and perform operations on lock1.
No, that is not safe to do. If you need both lists merged together, for
whatever reason, then you have to add the individual items to your own
temporary list instead, ie:

myList := TList.Create;
try
lock1 := IdTCPServer1.Context.LockList;
try
lock2 := IdTCPServer2.Context.LockList;
try
myList.Capacity := lock1.Count + lock2.Count;
for I := 0 to lock1.Count-1 do mylist.Add(lock1.Items[I]);
for I := 0 to lock2.Count-1 do mylist.Add(lock2.Items[I]);
// use myList as needed...
finally
IdTCPServer2.Context.UnlockList;
end;
finally
IdTCPServer1.Context.UnlockList;
end;
finally
myList.Free;
end;


Gambit

Loading...