Java ServerSocket accept call missing or queuing connections?

38 Views Asked by At

I have a simple Java server socket hosted on localhost. It has a while loop that kicks off connection threads that handle various data processing functions. The server socket is being used within a TCP proxy architecture to act as the middleman between a web page and my application, all running on Android (i.e. to locally host a remote webpage within my application).

The accept call waits / blocks for new connections as is typical. However, it seems to only get triggered every other call by the rest API buttons in the embedded web view. To simplify:

  1. Button pressed -> accept still waiting / blocking
  2. Button pressed again -> accept call goes through then immediately again accept call goes through.

It is expected that for these interactions a new connection is made. However, they seem to be 'queuing' and only getting pushed through to the accept call every other interaction. Has anyone experienced behavior like this? I have tried setting almost all options on the server socket but none seem to make a difference.

The code follows the simple pattern:

ServerSocket server = new ServerSocket();
server.bind(new InetSocketAddress("localhost", 0));

while (!cancel.get()) {
    Socket socket = server.accept();
    // Create new thread and pass in socket to handle input and output streams...
}

enter image description here

0

There are 0 best solutions below