Disconnect network connection

How to temporarily disable a network connection in Java?

+8
java
source share
6 answers

A rather complicated but feasible way to do this is to create a custom factory socket implementation. You can register it by calling Socket.setSocketImplFactory() .

Now your custom factory will have to return custom socket implementations that just throw an IOException on every connection attempt.

Note that this only stops outgoing connections, if you want the application to also accept incoming connections, you will have to play a similar trick on ServerSocket s.

+3
source share

Will they work? In the windows:

 Runtime.getRuntime ().exec ("ipconfig/release"); 

In linux (if the network interface is called eth0:

  Runtime.getRuntime ().exec ("ifconfig eth0 down"); 
+2
source share

Unfortunately, disconnecting a network connection is OS dependent, and I don’t know any standard Java APIs for accessing these OS features. You may have to do some OS-specific encoding, for example. through JNI or the execution of external programs, for each OS that you are going to support. A Java program must also be invoked with the appropriate privileges to enable them to interact with network interfaces.

+1
source share

(Assume this is under Windows, Vista or later)

You can disable the interface through WMI , which can be accessed through a bridge, such as JACOB .

+1
source share

Sniffy allows you to block outgoing network connections in your Java applications - it will throw a ConnectException whenever you try to establish a new connection to a limited host.

Just add -javaagent:sniffy.jar=5559 to your JVM arguments and point your browser to localhost:5559 - it will open a web page with all detected connections to downstream systems and controls to disable certain connections.

Sniffy connections console

If your application is based on a web interface, you can even do it directly from your application open in a browser - see the demo here: http://demo.sniffy.io/owners?lastName=

Click on the widget in the lower right corner, select the Network Connections tab, disconnect and connect to localhost:8967 (database) and reload the page to see it in action.

Disclaimer: I am the author of Sniffy

+1
source share

you can disable all interfaces using wmic path win32_networkadapter , where PhysicalAdapter = True disable call

0
source share

All Articles