lucasdc lucasdc - 1 year ago 135
Java Question

JNLP opening too many connections and causing BindException: Address already in use

We have a customer that is using our jnlp to open an application. In this .jnlp, there are like 30 resources to be downloaded (ex.

<jar href="https://url/to/resource.jar" download="lazy"/>
). We could note (via
netstat -ano
) that many connections are opened when jnlp is executed.

In our application, we have some code that downloads some files and in the middle of its execution, the exception
BindException: Address already in use
is thrown when we try to call
. And if we try to open jnlp again, our application is not even executed because the same exception is thrown (this time on Java Console). If we wait a few minutes and check the ports, we can see that they've disappeared from
netstat -ano
and then again we try to open with jnlp and the error again occurs in our application.

It's important to say that when the application is called with
java -jar
, the problem does not occur (because the resources don't need to be downloaded and no connections are open).

This problem is happening in ONLY ONE costumer, so it MUST be something related to their configuration. We tried to add MaxUserPort with value 65534 and TcpTimedWaitDelay with no success. This costumer is not using firewall, neither antivirus, but they said that the problem started occuring as soon as they migrated to Windows Terminal Server. Their Java version is 8u101.

Any ideas what could be affecting our application and a way to solve it? Any help is appreciated.

Snippet of code:

public InputStream connect(String urlToConnect) {
URL url = null;
InputStream is = null;

try {
url = new URL(urlToConnect);
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection();
is = httpsConnection.getInputStream();
} catch (IOException e) {
URLConnector.logger.error("Could not open connection on URL: "+urlToConnect+" -> "+e);
} finally {
if(is != null) {
try { is.close(); } catch(IOException e){}

return is;

Answer Source

Solved it editing the registry that opens jnlp files: HKCR > JNLPFile > Shell > Open > Command. Instead of ""C:\Program Files\Java\jre${VERSION}\bin\javaws.exe" "%1"", I used ""C:\Program Files\Java\jre${VERSION}\bin\javaws.exe" -J-Dhttp.keepAlive=false "%1""

More info about Persistent Connections in java can be found here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download