How to disconnect from localhost?

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?

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.

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

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)

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.