0

I created and debugged a simple net application that uses AsynchronousSocketChannel and AsynchronousServerSocketChannel and they work fine when accessed via localhost, but even if I move the server application to another device it refuses to connect at all. I first noticed this when I tried to port-forward the application, but upon moving it onto a device on the same network and connecting via the IPv4 it still didn't work. I checked both devices and they both are allowing Java through the firewall and there is nothing blocking the port number I am using (I have also tested various other ports). The client can't even connect to the server if it's on the same machine and you use that machine's IPv4. It literally only works on localhost. Has anyone else ever had this issue?

This is how the server channel is opened:

serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress("127.0.0.1", port));
liaquore
  • 403
  • 8
  • 22

1 Answers1

0

Fixed it by removing the IP part of the InetSocketAddress:

serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(port));

Replacing 127.0.0.1 with the IPv4 of the machine has the same effect. If someone could explain this that would be great, because every example I've seen online has binded to localhost. Are AsynchronousServerSocketChannels not supposed to be accessed remotely or something?

liaquore
  • 403
  • 8
  • 22
  • The whole idea of a *server* is to be accessed remotely by a client. Why do you want to **explicitly** bind to `127.0.0.1` or the IPv4 address? Which network interfaces does your device have where it doesn't run? – Kayaman Jul 30 '20 at 11:42
  • In examples online I saw them bind to `127.0.0.1` so I assumed you're meant to do that. – liaquore Jul 30 '20 at 11:43
  • But when that didn't work, you were completely lost. So assuming things is kinda dangerous. I'm asking because of [this](https://stackoverflow.com/questions/7382602/what-is-the-difference-between-127-0-0-1-and-localhost). – Kayaman Jul 30 '20 at 11:48
  • Well, it seemed to make sense and EVERY website was doing it, so there wasn't much else to go off of. I couldn't find an Oracle document that specified the correct way to open a server socket channel. – liaquore Jul 30 '20 at 11:50