-1

I am wondering how I can detect if a socket is coming from localhost, that means the sockets is open on pc1 and connects to a server on pc1, i tried the address but i hav'nt got it to work, is there a call like isLocalhost() or somthing like that where i can test for a localhost socket?

Tried code:

    if(socket.getInetAddress().getHostName().equalsIgnoreCase("127.0.0.1")) {

    }

but sadly that didn't work, I also tried with the ipv6 but also that didn't trigger the if statement.

Thanks for your time.

Pransh Tiwari
  • 3,983
  • 1
  • 32
  • 43
  • 1
    You are getting `HostName` and comparing it with the IP address of `127.0.0.1`. You can use `getHostAddress()` instead of `getHostName()`. However `getHostAddress()` is not going to return `127.0.0.1` in all networks. – STaefi Dec 09 '18 at 09:27
  • 1
    Possible duplicate of [How to determine an incoming connection is from local machine](https://stackoverflow.com/questions/1542424/how-to-determine-an-incoming-connection-is-from-local-machine) – STaefi Dec 09 '18 at 09:33

1 Answers1

1

Its socket.getInetAddress().isLoopbackAddress()