This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

NDK how to limit the number of client connection



Hello,

I would like to limit the maximum number of live client connection to the TCP socket server. If the client has already have 2 live socket connection how to tell the incoming client request that the server is full?

I found this one:

Ip.socketMaxConnections = 2;  // what is it good for?

and also set the accept backlog to 2 but no effect.   

Is there any built in solution? 

  • Hello Daniel,

    Did you try limiting it through "listen" call? One of parameters to listen is maxcon which is specifies the maximum length of the queue of pending connections. So if queue is full server will drop that connection.

    The Ip.socketMaxConnections = 2; sets Max Socket Connections allowed which is compared against one given during listen call if listen maxcon is higher than one set in .cfg file it is set to socketMaxConnections. 

     Regards,

    Prasad

  • Hello,
    Thank you for your reply.

    Of course I did, has no effect.
  • Hi Daniel,

    I'm looking internally to see if there is a way to do this, I'll have to get back to you tomorrow on what I find.


    Steve

  • Daniel,

    Unfortunately the NDK does not have support for capping the max number of (concurrent) TCP connections.


    As Prasad already mentioned, the maxConn parameter to listen() will set the maximum number of pending connections allowed on the socket (it's the length of the queue).   The config param Ip.socketMaxConnections will cap the value that's passed into listen() via the maxConn arg (e.g. if socketMaxConnections = 5, but a call is made like listen(s0, 20), the value of 20 will be forced down to the max value of 5).

    How many sockets is your server creating?

    More specifically, are you creating a single server socket, let's say "s0", and then accepting new connections ("s1", "s2", etc.) on "s0"?

    e.g. something similar to the following:

    s0 = socket();

    bind(s0, ...);

    listen(s0, <maxConn>);

    while((sn = accept(s0, ...)) // accept incoming connections on new sockets (s1, s2 ... sn)

    If so, and you have just a single server socket, listening/accepting (like above) in a single Task, then the easiest way for you to handle this might be to manage it yourself, within your Task.

    Steve

  • Dear Steven,

    Thank you for your reply. Yes, the best way is to manage the opened sockets in a variable and close the unnecessary connection request.