Java socket "successfully" connect to any IP on port 80

Viewed 198

I'm trying to open a socket to a remote server, but I want to get an exception thrown if the other side is not responding. For some reason, any IP which I try with PORT 80 seems to "connect" even if it's a completely random IP.

I'll try to convey the issue with an example.

This will timeout and raise an exception:

Socket s = new Socket();
s.connect(new InetSocketAddress("1.2.3.4", 81), SOME_TIMEOUT_CONST);

On the other hand, the following will continue normally as if it connected successfully:

Socket s = new Socket();
s.connect(new InetSocketAddress("1.2.3.4", 80), SOME_TIMEOUT_CONST);

I can't seem to figure out the reason for that.

If this is the expected behavior, how can I check that in case 2 it did not actually connect?

Edit: I ran the same code on a friend's pc, and both cases timed out and raised an exception. So it's probably something with my Mac. No idea what...

0 Answers
Related