Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable network connection

Tags:

java

How could I temporarily disable a network connection in Java?

like image 480
Jony Avatar asked Feb 03 '11 23:02

Jony


People also ask

How do I enable and disable Network connections in Windows 10?

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.

What happens if I disable LAN?

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.


2 Answers

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 ServerSockets.

like image 167
biziclop Avatar answered Oct 04 '22 03:10

biziclop


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");
like image 29
Satyajit Avatar answered Oct 04 '22 05:10

Satyajit