Robin Green
Robin Green

Reputation: 33083

Verify that socket is being closed

How can I verify that my application is closing a particular HTTP connection?

The close logic is deep inside a complicated library so it's not obvious.

I tried to use the Eclipse debugger and put a breakpoint on java.io.Closeable but Eclipse said I can't put a breakpoint on an interface method.

Upvotes: 0

Views: 50

Answers (1)

Robin Green
Robin Green

Reputation: 33083

On Linux, run sudo ss -r -p | grep java at the command line to check if the socket is still open. If ss is not installed you can use netstat instead. (The Windows equivalent of grep is find, though I don't know the exact command to use on Windows.)

If you are prepared to grep only by destination server or port, you don't need the sudo or the -p option in that case.

Warning: Note that for HTTP, if the server sends a Content-Length header, or a type of response with an implied content length, or uses Transfer-Encoding: chunked then HTTP client libraries which support connection keep alive may keep the actual socket open even if you call close() on the InputStream (an additional clue here is that curl/libcurl will print a message saying the connection has been kept alive if you use curl to make a test request). But if the server doesn't do any of those things, this isn't possible, so the connection should be closed in that case.

Upvotes: 1

Related Questions