Java ServerSocket. Why is the IP address 0.0.0.0, yet I can still connect remotely?

21,369

Solution 1

You bound to the any address which means you are listening on all interfaces. You can accept connections from that come in from any defined interface on the server including localhost and any IP addresses you have defined in case you are multi-homed.

You could bind to just 127.0.0.1 and only accept connections from localhost. You could bind to a specific IP address and only accept connections on that interface.

Solution 2

0.0.0.0 Means that all ips are being used. If you are trying to connect remotely, make sure you open up the port you are using in you firewall and/or router.

Share:
21,369
NullVoxPopuli
Author by

NullVoxPopuli

I do stuff

Updated on July 09, 2022

Comments

  • NullVoxPopuli
    NullVoxPopuli almost 2 years

    Now, I want to connect remotely, I just want to know why the server tells me it doesn't have an IP address.

    Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=13380]
    

    Relevant code:

    private ServerSocket        serverSocket    = null;
    private Thread              thread          = null;
    private int                 clientCount     = 0;
    
    /**
     * Constructor
     * 
     */
    public ControlListener(int port)
    {
        try
        {
            System.out.println("Binding to port " + port + ", please wait  ...");
            this.serverSocket = new ServerSocket(port);
            System.out.println("Server started: " + this.serverSocket);
            start();
        }
        catch (IOException ioe)
        {
            System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
        }
    }
    

    from link: http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html example 4 is what I followed