I wrote an asynchronous socketserver using java 7 nio2.
Here is the server operator.
public class AsyncJava7Server implements Runnable, CounterProtocol, CounterServer{ private int port = 0; private AsynchronousChannelGroup group; public AsyncJava7Server(int port) throws IOException, InterruptedException, ExecutionException { this.port = port; } public void run() { try { String localhostname = java.net.InetAddress.getLocalHost().getHostName(); group = AsynchronousChannelGroup.withThreadPool( Executors.newCachedThreadPool(new NamedThreadFactory("Channel_Group_Thread")));
and here is a snippet of client code that is trying to connect ...
public class AsyncJava7Client implements CounterProtocol, CounterClientBridge { AsynchronousSocketChannel asyncSocketChannel; private String serverName= null; private int port; private String clientName; public AsyncJava7Client(String clientName, String serverName, int port) throws IOException { this.clientName = clientName; this.serverName = serverName; this.port = port; } private void connectToServer() { Future<Void> connectFuture = null; try { log("Opening client async channel..."); asyncSocketChannel = AsynchronousSocketChannel.open();
I have a test that runs clients.
@Test public void testManyClientsAtSametime() throws Exception { int clientsize = 150; ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(clientsize + 1, new NamedThreadFactory("Test_Thread")); AsyncJava7Server asyncJava7Server = startServer(); List<AsyncJava7Client> clients = new ArrayList<AsyncJava7Client>(); List<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < clientsize; i++) {
It works super for about 100 clients. At about 140+, I get an exception loading in the client - when it tries to connect. An exception is: java.util.concurrent.ExecutionException: java.io.IOException: the remote computer refused a network connection.
My test is on one laptop running Windows 7. When it explodes, I check TCP connections and there are about 500 - 600 connections there - this is normal. AS I have similar JDK 1.0 java.net socket programs that can handle 4000 TCP connections.
No exceptions or any sly look at the server.
Therefore, I do not understand what might be wrong here. any ideas?