Closing connections to ports , without the existence of socket bindings to the same in Java [closed]

StackOverflow https://stackoverflow.com/questions/21957766

  •  15-10-2022
  •  | 
  •  

سؤال

I have a small client server app , that involves the client pinging the server on a set of ports ( 90,6100,6200).

I use inet.isReachable(30000) to monitor whether the server responds to the client or not.

In the end , I want to make sure that when I re-run my application , any existing connections to these ports are removed.

How do I go about doing so ?

And is there a way to automatically close a connection when I am using InetAddress, which does not seem to have any method to terminate an existing connection to an address ?

هل كانت مفيدة؟

المحلول

"I want to make sure that when I re-run my application , any existing connections to these ports are removed."

The connections cannot stay open when your application exits - there is no process for them to be connected to. So in that sense, you don't have to worry about closing them on next launch.

"And is there a way to automatically close a connection when I am using InetAddress, which does not seem to have any method to terminate an existing connection to an address ?"

Yes - the connection will close when isReachable is responded to, or when the timeout expires (or some other error occurs). Just creating an InetAddress does not create a connection - it automatically creates and destroys them as needed to do its functionality. Connections are generally just short lived things that don't stay open unless you make them.

نصائح أخرى

The JVM will close the connection after the server responds to isReachable or the timeout expires, whichever comes first.

The answer to your vague question is that as connections don't survive the process they are created by, and as INetAddress"isReachable() doesn't leak connections, there is no actual problem here to solve.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top