chrips
chrips

Reputation: 5296

When connecting to Java socket, why does getInetAddress() return /0:0:0:0:0:0:0:1?

I connect via MS Telnet which is where I see the output.

I know 0:0:0:0:0:0:0:0 would be ALL or wildcard... but where does it come up with 0:0:0:0:0:0:0:1 ?

What's the point of giving me this? Why not give me an IP such as 127.0.0.1?

Is this a virtual MAC address? Javadoc wasn't very informative.

public InetAddress getInetAddress() Returns the address to which the socket is connected. If the socket was connected prior to being closed, then this method will continue to return the connected address after the socket is closed.

Returns: the remote IP address to which this socket is connected, or null if the socket is not connected. From https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getInetAddress()

MAIN:

public static void main(String[] args) throws IOException {
/*
        * The actual work of the server socket is performed by an instance
        * of the {@code SocketImpl} class.
*/

        // Open a port to accept connections on 8080
        ServerSocket srvrSocket = new ServerSocket(8080);

        // Block until someone connects
        while (true) {

            Socket socket = srvrSocket.accept();

            handle(socket);
        }
    }


    private static void handle(Socket socket) throws IOException {

        try(
                socket;
                InputStream is = socket.getInputStream();
                OutputStream os = socket.getOutputStream();
                ) {

            // Print welcome
            DataUtil.writeStringAsInts(os, socket.getInetAddress().toString());
.
.
.

Upvotes: 2

Views: 559

Answers (1)

Pasupathi Rajamanickam
Pasupathi Rajamanickam

Reputation: 2052

IPV6 has representation of 0:0:0:0:0:0:0:1 for localhost, when we try to connect server socket from local, we may get 0:0:0:0:0:0:0:1 sometimes we may get ::1

The localhost (loopback) address, 0:0:0:0:0:0:0:1, and the IPv6 unspecified address, 0:0:0:0:0:0:0:0, are reduced to ::1 and ::, respectively https://en.wikipedia.org/wiki/IPv6_address

Upvotes: 3

Related Questions