RTC Forums
April 27, 2024, 01:37:59 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: How to restart listening  (Read 7155 times)
YuraZ
Newbie
*
Posts: 39


« on: March 20, 2013, 03:45:06 PM »

Hello,

I'm using TRtcHttpServer with properties:
FixupRequest.RemovePrefix := true;
      MaxHeaderSize := 0;
      MultiThreaded := true;
      RestartOn.ListenError := false;
      RestartOn.ListenLost := false;

Sometimes I need restart my app server as:
self.f_connection.StopListenNow;
// my server actions
self.f_connection.Listen;

But after execution of Listen event OnConnectionListerStart is not executed. If I try this again then Listening is successfull.
If I call self.f_connection.StopListenNow and self.f_connection.Listen separately, such as on click different buttons, all is ok.
I cant use Restart method, because "// my server actions" must be executed when all connected clients are disconnected.
How I can solve my problem?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: March 20, 2013, 04:12:13 PM »

OnListen event will be triggered after the Server listener starts. It does NOT happen immediately, it is an asynchronous event. But I'm wondering ... why do you need to restart the Server listener? The only reason anyone would need to do this is to change the Server Port number. But that is never done on an active Server. Once you set up the Server, you will have it listening on the same Port until you need to close the Application.

Can you provide more details about what you are trying to do?

Best Regards,
Danijel Tkalcec
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #2 on: March 20, 2013, 04:38:18 PM »

I need to disconnect all clients before some actions will be not executed and when some actions executed no clients to be connected.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: March 20, 2013, 04:45:33 PM »

Why? There is no technical reason for a Server to have to disconnect all clients before executing actions. At least none that I can think of. So ... what is it you need to do, which requires all clients to be disconnected before it can be performed?
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #4 on: March 20, 2013, 04:51:36 PM »

Client connects to server, server returns number of need connections to do at client. After that client occurs need count of connections. And this count is calculated on these "some actions". Dont ask me for what purposes is it need Smiley Server application is very complex and not easy.
I need only disconnect users, do some actions, and start listening of server. How I can do this in one executable function/procedure?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #5 on: March 20, 2013, 05:25:38 PM »

The answer is that you can NOT do this and your should NOT need to do this. There is a forced wait period by the sockets API (not the RTC SDK) which makes it technically impossible to start a new Listener on the exact same Port which was closed shortly before that. This has to do with the fact that TCP/IP communication is not instant and TCP/IP packets can take some time to travel from one side to another. There is also a period of about 2 minutes during which the same incoming TCP/IP Port will remain unavailable (locked) for new connections after it was disconnected by the Server. This is required to ensure that packets received on that same Port number are really coming from the original Client which had opened the Port.

Normal communication between TCP/IP Clients and Server is ...

1. Server stats listening on a Port.
2. Client sends a "conect" request to the Server on its listening Port.
3. Server accepts the Client connection and assigns that connection a unique Port on the Server.
4. Client and Server can now send data in both directions, each side using a unique Port number to identify the other side.
5. When there is no more need for communication, Client will send a packet to the Server to notify the Server that it no longer requires the connection, and the Client will close his side of the socket.
6. Server will receive the "connection closed" message from the Client and close his side of the Socket.
7. Now, the Port number used by the Client is free for use by other incoming Clients and new connections.

But ... if the Server simply "drops" a connection from the Client, then the Port number used by that Client will remain blocked for some time, to ensure that any packets the original Client might be sending to the Server do not get mixed up with packets which might be coming from some other Client who would get that same Port number after connecting. And if a Server Listener is stopped on one Port, then that Port will remain blocked for at least the period needed for all the client connections to be securely shutdown. And that is only one of the reasons why closing all client connections and stopping the Server Listener is a very bad idea, if your goal is to execute some action on the Server before you start the Server listener again.

Again ... there is absolutely NO technical reason why you would need to CLOSE all Client connections, stop the Server listener and then start the Server listener again on the exact same Port. None. If you THINK that you need to do this, then your design needs re-thinking.

I seriously think that you need to recosider your decisions which led to the conclusion that you have to close all connections on the Server, stop the Server listener, do something and then start the Server listener again. Stopping the Server listener is NOT like turning a Light switch OFF and ON. It is like blowing the House up with everyone inside, then rebuilding the House  from ground up and waiting for everyone to get back in. And I mean that literally.

Best Regards,
Danijel Tkalcec
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #6 on: March 20, 2013, 05:42:37 PM »

Unfortunatelly I cannot change architecture of my decision.
If you say that is not impossible, so how I can StopListen at OnClick of one button and Listen on another button? And its works correctly.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #7 on: March 20, 2013, 05:52:09 PM »

Because (A) your two consecutive mouse Clicks are both executed from the Main Thread, outside of the context of any RTC connections, and (B) there is a time difference between these two actions. These two facts make it possible for the TCP/IP stack to complete its actions and allow a new Listener to be started with your 2nd mouse click.

Best Regards,
Danijel Tkalcec
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #8 on: March 20, 2013, 05:56:33 PM »

But I did Sleep(10000) before start Listen after StopListen and this not effected. Between StopListen and Listen on buttons click was less 1 second.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #9 on: March 20, 2013, 06:10:13 PM »

1. Did you use StopListenNow or StopListen?

2. Did you implement all OnListen... events to get notified about what happened after you call Listen?

3. Was your function called by a user clicking on a button on the form, or was it called from a RTC remote function or some other event triggered by the RTC SDK?
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #10 on: March 20, 2013, 06:30:37 PM »

1) StopListenNow
2) Yes
3) Form
Logged
cesarliws
Guest
« Reply #11 on: March 20, 2013, 06:44:08 PM »

Hi YuraZ,


If StopListenNow in one mouse click event, followed by Listen in another mouse click event work, then calling Restart at the end of the 1st event should have the same effect.


Regards,


Cesar Romero
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #12 on: March 20, 2013, 07:51:22 PM »

f_connection.StopListenNow;
// some actions...
f_connections.Restart;

Is not helped.
Logged
YuraZ
Newbie
*
Posts: 39


« Reply #13 on: March 21, 2013, 06:45:08 AM »

This is my fault.
On event OnListenStart and OnListenStop i'd tried to change properties of VCL at form (Caption, Enabling buttons. etc.), because i thought that triggering of this events are in parallel threads.
So I'd deleted VCL-code from OnListenStart and OnListenStop and starting/stopping listening is successfull.
Thanks a lot for help.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.028 seconds with 16 queries.