How could I temporarily disable a network connection in Java?
Open Start. Search for Device Manager, and click the top result. Expand the Network adapters category. Right-click the adapter you want, and select the Disable device option.
If you disable the Ethernet network adapter, any and all Ethernet connections that connect through it will be disabled. An Ethernet adapter, like a wireless adapter can connect to different networks but if you disable the adapter, it doesn't matter which network you plug into your system, it will not connect.
A fairly complicated but feasible way to do this is to create a custom socket implementation factory. You can register it by calling Socket.setSocketImplFactory()
.
Your custom factory will then have to return custom socket implementations that simply throw an IOException
at every connection attempt.
Note that this only stops outgoing connections, if you want to stop your application accepting incoming connections as well, you'll have to play a similar trick on ServerSocket
s.
Would these work? On windows:
Runtime.getRuntime ().exec ("ipconfig/release");
On linux ( assuming the network interface is called eth0:
Runtime.getRuntime ().exec ("ifconfig eth0 down");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With