Java slow socket.connect() -


below source code of client , server. client connects (concurrently) server , immediatly closes connection. when threads done, waits 2 minutes , connects again. confused fact simple connect needs around 3 seconds! of time connect needs around 0-32ms.

here typical output client:

... connect 23 [ms]: 16 connect 22 [ms]: 32 connect 21 [ms]: 32 connect 15 [ms]: 32 connect 14 [ms]: 16 connect 13 [ms]: 16 connect 11 [ms]: 32 connect 25 [ms]: 3016 

this seems happen if client , server on different hosts. windows , linux comparable behaviour java 1.6.23

to start server 2 parameters needed: [port] [thread pool size]

to start client 3 parameters needed: [host] [port] [thread pool size]

for example used 150 thread pool size server , 25 thread pool size client.

can explain behaviour?

----- server -----

package de.test.server;  import java.io.ioexception; import java.io.inputstream; import java.net.serversocket; import java.net.socket; import java.util.concurrent.executorservice; import java.util.concurrent.executors;  public class serverapp {     public static void main(string[] args) throws ioexception {       system.out.println("server running...");        final int port = integer.parseint(args[0]);       final int threads = integer.parseint(args[1]);       final executorservice executorservice = executors             .newfixedthreadpool(threads);       serversocket serversocket = new serversocket(port);       while (true) {          final socket clientsocket = serversocket.accept();           executorservice.execute(new runnable() {             @override             public void run() {                try {                   inputstream = clientsocket.getinputstream();                   int read = is.read();                   if (read != -1) {                      system.out.println("should not happen");                   }                } catch (ioexception e) {                   throw new runtimeexception(e);                } {                   close(clientsocket);                   system.out.println("connection closed");                }             }              private void close(final socket connection) {                try {                   connection.close();                } catch (ioexception e1) {                   throw new runtimeexception(e1);                }             };          });       }    } } 

----- client -----

package de.test.client;  import java.io.ioexception; import java.net.inetsocketaddress; import java.net.socket; import java.net.unknownhostexception; import java.util.concurrent.countdownlatch; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.atomic.atomiclong;  public class connectapp {     public static void main(string[] args) throws interruptedexception {       final string host = args[0];       final int port = integer.parseint(args[1]);       final int thread_count = integer.parseint(args[2]);        final executorservice executorservice = executors             .newfixedthreadpool(thread_count);       final atomiclong threadcounter = new atomiclong(0);       while (true) {          final countdownlatch donesignal = new countdownlatch(thread_count);          (int = 0; < thread_count; i++) {             executorservice.execute(new runnable() {                @override                public void run() {                   socket socket = null;                   try {                      long start = system.currenttimemillis();                      socket = new socket();                       socket.settcpnodelay(true);                      socket.connect(new inetsocketaddress(host, port));                      system.out.println(socket.gettcpnodelay());                      long stop = system.currenttimemillis();                      system.out.println("connect "                            + threadcounter.incrementandget() + " [ms]: "                            + (stop - start));                   } catch (unknownhostexception e) {                      throw new runtimeexception(e);                   } catch (ioexception e) {                      throw new runtimeexception(e);                   } {                      close(socket);                      donesignal.countdown();                   }                 }                 private void close(socket socket) {                   try {                      if (socket != null)                         socket.close();                   } catch (ioexception e1) {                      throw new runtimeexception(e1);                   }                }             });          }          donesignal.await();          system.out.println("waiting 2 minutes...");          thread.sleep(1000 * 60 * 2);       }    } } 

all sockets trying connect @ once. since trying connect same single threaded server, 1 accept, 50 (by default) in backlog , reset may not connect or have wait particularly long time.

i suggest trying 40 ms spacing between attempting connect see if problem.

btw: call threadcounter.incrementandget() why going , down? use

for (int = 0; < thread_count; i++) {     final int threadcount = i; 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -