how to see the max connection number to a TCP/IP port

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to see the max connection number to a TCP/IP port

Jump to solution
2,145 Views
Weining
Contributor III

Could anybody advise me how to set or limit the connection number to a TCP/IP port?

 

I am using MCF52259 and MQX 3.7 to implement a Telnet server type application. the server will sopport 2 connections on port 23 and 1 connection on port 24.

 

how do i set the parameters so that the application only accept 2 connections on port 23, and when the third coneection request comes, the application will reject it right away (the client wil know no TCP connection is available )?

 

Thank you!

 

Weining

0 Kudos
1 Solution
973 Views
trailman
Contributor V

Hi Weining,

 

In fact this seems simple to implement : all you need in in the server.c file of the demo.

But this workaround has to be implemented for each server, so you will have to modify the BSP for all servers you use (httpd, telnetd, ...) and also in your applicatin if a server is implemented in it (unless you modify accept() in BSP to do that, if possible)

 

To implement the workaround for each server :

- add a variable counting the connexions that are beeing serviced (increment on accept(), decrement on close())

- add a RTCS_selectall() before accept()

- if the socket returned by RTCS_selectall is matching the one returned by listen(), check iif the max connection count has been reached

- if yes, call shutdown(sock, FLAG_ABORT_CONNECTION) to abort; otherwise call accept() to do normal processing

 

View solution in original post

0 Kudos
8 Replies
973 Views
trailman
Contributor V

Hi wein,

 

I also want to limit the number of incoming connections to a TCP service to the number of tasks servicing it.

The reason is that every extra connection waiting to be serviced (accepted) consumes some memory for nothing (as this kind of connections is hanging till another is closed), and can lead to a run out of memory in case of several incomming connections.

So I also want to refuse them with a "connection refused" (for example).

 

I first tried to set the backlog (second) parameter of listen() (socket specific value), but I discovered it was unused by MQX

 

I had a look to rtcs/source/tcpip/tcp.c and found RTCSCFG_TCP_MAX_CONNECTIONS.

I tried to set it to 3 (global count for all services); this was OK till I found out that this also applied to outcoming connections. So if my MQX board has 3 open connections as a client to a server, no client can connect to my MQX board.

 

I also had a look to memory polls to set a specific and limited memory pool for RTCS memory allocations; however the memory allocated even if not fully used can not be used for other purposes.

 

So I still searching for a solution but don't have enough time to work on it right now.

 

Probably the best thing would be to implement the backlock parameter of listen() to have a per socket setting.

 

Please let me know if you find a solution to this problem.

0 Kudos
973 Views
Weining
Contributor III

Hi trailman,

 

Thanks a lot for sharing your experience.

 

Actually I had tried using backlog of listen(), as you did, but finally found that MQX ignores that parameter. And I cannot use RTCSCFG_TCP_MAX_CONNECTIONS either since I need to keep the connections  to 2 different ports independently, like the problem you face.

 

I still try to find the solution, and just sent a technical request to Freescale. I will share what I learn when I get a result.

 

Thank you,

 

Weining

0 Kudos
973 Views
trailman
Contributor V

Hi Weining,

 

Did you find a solution ?

 

0 Kudos
973 Views
Weining
Contributor III

Hi Trailman,

 

Sorry for not watching this thread in these few days since I just come back from a long trip.

 

I got an answer form Freescale but it still is not what I need. Freescale technical support recommended to use RTCS_selectall() function to watch the incoming connection request, then the firmware decides to accept /reject the connection. I just feel that from the view of client side, the client will see the server accept then reject the connect ion. What I really want is the server never accepts a new connection if it already has enough connections.

 

The example code from Freescale is a little long, if you need latter I can select the major part and paste them here.

 

Do you have any ideas for this problem?

 

Weining

 

0 Kudos
973 Views
trailman
Contributor V

Hi Weining,

 

I have no other idea, but RTCS_selectall() may be an acceptable workaround.

Could you post the full code as a zipped attachment (rather than pasting a partial code) ?

 

However, I guess you're right : each incomming connection will be queued in socket untill rejected by the workaround, so some memory will be allocated during this time. However if rejection is fast enough no memory runout should occur, except if the server is attacked by many incomming connection requests.

 

This will never be as good as implementing the backlog parameter to listen().

 

0 Kudos
973 Views
Weining
Contributor III

Thanks Trailman for your comment.

 

the files from Freescale are attached. Good luck.

 

 

Weining 

0 Kudos
974 Views
trailman
Contributor V

Hi Weining,

 

In fact this seems simple to implement : all you need in in the server.c file of the demo.

But this workaround has to be implemented for each server, so you will have to modify the BSP for all servers you use (httpd, telnetd, ...) and also in your applicatin if a server is implemented in it (unless you modify accept() in BSP to do that, if possible)

 

To implement the workaround for each server :

- add a variable counting the connexions that are beeing serviced (increment on accept(), decrement on close())

- add a RTCS_selectall() before accept()

- if the socket returned by RTCS_selectall is matching the one returned by listen(), check iif the max connection count has been reached

- if yes, call shutdown(sock, FLAG_ABORT_CONNECTION) to abort; otherwise call accept() to do normal processing

 

0 Kudos
973 Views
Weining
Contributor III

Thanks Trailman again.

 

 

Actually I implemented this workaround before but just felt too much work--- my application needs http server, telnet server...... functions; as you mentioned i have to add/ modify this for each server. And I don't  like to modify BSP, otherwise when the MQX is upgraded i might need to redo the BSP.

0 Kudos