Well, I dealt with this problem as follows: Pending-state connections on the socket are a kind of "middle_state", which means that you cannot control / reject them. The backlog socket parameter can be used / ignored / handled differently using a specific virtual machine. This means that you have to accept a specific connection to get the related object and manage it.
Use one thread to accept the connection, pass the received connection to the second thread for processing. Create a variable for the number of active connections. Now that the number of active compounds is less than the desired maximum, accept the connection, increase the number by 1 and go to the second thread for processing. Otherwise, immediately accept the connection and close.
In addition, in the thread of the connection process, than completed, reduce the number of active connections by 1 to the point, there is another free channel.
EDT: just made a stub for server-side machining for Java.Net NIO. Can be adapted for OP needs:
package servertest; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; public class Servertest extends Thread { final int MAXIMUM_CONNECTIONS = 3; int connectionnumber = 0; public static void main(String[] args){ new Servertest().start(); } @Override public void run() { try { ServerSocket sc = new ServerSocket(33000, 50, InetAddress.getLoopbackAddress()); while (sc.isBound()) { Socket connection = sc.accept(); if(connectionnumber<=MAXIMUM_CONNECTIONS){ new ClientConnection(connection).start(); connectionnumber++; } else {
source share