简体   繁体   中英

Why socket does not connect with ip instead of localhost?

I am trying to connect to server via ip address but it does not work. Both are in the same machine and when I change ipAddress to localhost it works. What would be the reason for that?

try {
    String ipAddress = "46.155.17.100";
    int port = 8082;

    // Create a socket to connect to the server
    socket = new Socket(ipAddress, port);

    toServer = new ObjectOutputStream(socket.getOutputStream());

    fromServer = new ObjectInputStream(socket.getInputStream());

} catch (Exception ex) {
    connected = false;
    try {
        if (socket != null) {
            socket.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

EDİT : When I change my connection from WiFi to internet cable, socket is connected with new ip address. But I dont know why :(

I am trying to connect to server via ip address but it does not work. Both are in the same machine and when I change ipAddress to localhost it works. What would be the reason for that?

The reason for that is because your ServerSocket in the server code is not bound to the IP-Address which you're trying to connect from the client. So, in that case, the ServerSocket is bound to the default loopback address, ie,the localhost OR 127.0.0.1 .

You need to edit your server side code to bind the IP-Address of ServerSocket with the IP( 46.155.17.100 ) either in the constructor OR binding it later. It would be done by replacing the ServerSocket initialisation with the following :

// int port = 4444, backlog = 5;
String bindaddr = "46.155.17.100";
ServerSocket server = new ServerSocket(port,backlog,InetAddress.getByName(bindAddr));
// your server-side code continues below.

Networking fundamentals (In a Gist): When using a IP address, your computer is going to look to see if the IP is on the same sub-net as its own NIC(s). If not the computer will defer to routing to your default gateway (router) in determining the location of the address. So when you use the public address you are sending the traffic from your computer to your router, and then out to the ISP/Service provider where it simply gets routed back to your routers IP and thus your router needs to be open for the traffic on port 8082. Using localhost or 127.0.0.1 tells your computer to route it to itself.

In Steps:
Destination "46.155.17.100"
Computer is this in my local network?(My own network is 192.168.0.0/24) (My Address is 192.168.0.X)
    - This is not on my network, let me send it to my default gateway
Computer sends data to gateway -> 192.168.0.1 (router)
The Router says, is this on any network I know of?
        - Nope i do not own 46.155.17.100, -> Your router send data to its own default gateway which should be the (ISP Router)
ISP Router says hey i know where this is -> sends it back to your router.

first try "telnet 46.155.17.100 8802". If telnet can't connect to the server, the problem is with the server, or with the firewall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM