Home > Mobile >  How to trace Java and the local port it attempts to open?
How to trace Java and the local port it attempts to open?

Time:11-09

Working with a Java client/server application and no access to source code.

The client application uses the URLConnection object when making calls to the server. Most of the time it works but sporadically it fails with the following error.

java.net.BindException:Address already in use

When it fails it will do so for a number of attempts and then all of a sudden work fine again.

In an attempt to capture the URL calls, the application was started via the command line and referenced the logging.properties file with the following entry.

 sun.net.www.protocol.http.HttpURLConnection.level = ALL

This generated some good information but there was no reference as to the port it was attempting to open on the client side.

  • Question What entry needs to be added to the logging.properties file to capture the port the client is attempting to use?

CodePudding user response:

You will have to look at the stack trace of the exception to find out where it is triggered - maybe you can find the right class to enable logging for.

But it is not guaranteed that an application logs the network address it is trying to open.

When you describe that after some attempts it works again, you can try to find out with the command netstat what ports are actually opened, and also which processes use them. Look out for the state "TIME_WAIT": such connections are actively closed, but the networking stack still doesn't reuse the port as "old" packages from the recently opened connection could be on the way to the receiver. After a configured timeout, that port is then reusable again.

Find more details about this state in this SO question and its answers.

You also could try to attach a debugger to the active application, probably best with a built in decompiler or a plugin for that (usually IDEs like IntelliJ IDEA are able to do that) so you can set a breakpoint to the call of the method that throws the exception and inspect its parameters to find out about the port.

CodePudding user response:

In logging.properties you can increase log level. But if the programmer never placed a call to logging with the relevant data you will not see any relevant message in the logs anyway.

A safe way to figure out which socket the application is trying to open is to use strace.

Prefix your call to your application using strace, and maybe figure out which filters allow you to see network stuff only:

strace ... java ...

The first three dots need to be replaced with options for strace (and the filter for network traffic), the second three dots have to be replaced with everything your application needs to run.

strace will be a layer between an application (java) and the kernel, and it will print to console all system calls including the parameters and result codes. So you can see how the JVM is asking the OS to open a port and the OS responds with "nope, address already in use".

  • Related