How to disconnect from localhost?
Asked Answered
S

5

8

Is it possible to disconnect from localhost?

I'm writing a Node.js WebSocket server, and I want to test locally what would happen if the connection closes erroneously.

If I were testing remotely, I'd just Turn Wi-Fi Off, but that doesn't disconnect my connection to localhost.

Thoughts?

Steffi answered 28/8, 2012 at 15:10 Comment(0)
C
1

localhost is just an alias in your hosts file. If you remove that alias then you'll be effectively "disconnecting" from localhost.

Ceolaceorl answered 28/8, 2012 at 15:12 Comment(1)
I tried this but it doesn't work. I am using Manjaro linux.Sadonia
B
1

I don't know of any way you would do what your asking except perhaps to block the ports or the program you are running on your localhost via its firewall.

Baziotes answered 28/8, 2012 at 15:12 Comment(0)
C
1

localhost is just an alias in your hosts file. If you remove that alias then you'll be effectively "disconnecting" from localhost.

Ceolaceorl answered 28/8, 2012 at 15:12 Comment(1)
I tried this but it doesn't work. I am using Manjaro linux.Sadonia
R
1

As David mentioned, you can block ports with a simple firewall.

For example on OSX, to block localhost on port 8080

$ sudo ipfw add deny tcp from any to localhost 8080

Will return a response like:

00100 deny tcp from any to 127.0.0.1 dst-port 8080

And then to remove the rule:

sudo ipfw delete 00100

(ipfw is deprecated in favor of pfctl, but I still find it simpler for these purposes)

Ramsgate answered 28/2, 2013 at 22:27 Comment(1)
Sadly, ipfw was removed in Yosemite.Ramsgate
D
1

try this:

disconnect

sudo ifconfig lo down
# or
sudo ip link set dev lo down 

connect

sudo ifconfig lo up
# or
sudo ip link set dev lo up 

NOTE: 'lo' stands for loopback device 127.0.0.1

Dwarfish answered 27/4, 2024 at 23:7 Comment(0)
S
0

Instead of using localhost, I use the IPv4 address (which can be obtained using ipconfig call or looking into Local Area Connection) to access local machine, and then, to simulate network failure, I just disable Local Area Connection. It helps me test network failures on local machine.

Sauter answered 8/5, 2014 at 10:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.