I've been having this problem for about 4-5 months now. It all started when I joined my boyfriend's hosted server using hamachi. A couple nights later, it started giving me that error. I've recently just factory reset my pc. I tried joining my brother's server (LAN) by typing in the ip or "localhost", but it still gave me the same "failed to connect to the server" message. I've tried:
- turning the Firewall on/off
- uninstalling and installing minecraft
- restarting the router (even getting a new one)
- changing port number to 25565
- factory resetting my pc
I've tried every solution I could find, but none worked so far. I guess not a lot of people come across this problem. They said theirs were fixed in a couple of weeks, but my problem has been occurring for almost half a year now.
I'm running Java version 8 update 171.