简体   繁体   English

为什么套接字不使用ip而不是localhost连接?

[英]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. 我正在尝试通过IP地址连接到服务器,但是它不起作用。 Both are in the same machine and when I change ipAddress to localhost it works. 两者都在同一台机器上,当我将ipAddress更改为localhost时,它可以工作。 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. EDİT:当我将连接从WiFi更改为互联网电缆时,套接字已连接到新的IP地址。 But I dont know why :( 但我不知道为什么:(

I am trying to connect to server via ip address but it does not work. 我正在尝试通过IP地址连接到服务器,但是它不起作用。 Both are in the same machine and when I change ipAddress to localhost it works. 两者都在同一台机器上,当我将ipAddress更改为localhost时,它可以工作。 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. 原因是因为服务器代码中的ServerSocket未绑定到您尝试从客户端连接的IP地址。 So, in that case, the ServerSocket is bound to the default loopback address, ie,the localhost OR 127.0.0.1 . 因此,在那种情况下,ServerSocket绑定到默认的回送地址,即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. 您需要编辑服务器端代码,以将ServerSocket的IP地址与IP( 46.155.17.100 )绑定到构造函数中,或者以后再绑定它。 It would be done by replacing the ServerSocket initialisation with the following : 可以通过将ServerSocket初始化替换为以下内容来完成:

// 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). 网络基础(摘要):使用IP地址时,您的计算机将查看IP是否与自己的NIC位于同一子网中。 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. 因此,当您使用公用地址时,您会将流量从计算机发送到路由器,然后再发送到ISP /服务提供商,在此处,该流量仅路由回到路由器IP,因此您的路由器需要打开以接受流量端口8082。使用localhost或127.0.0.1告诉计算机将其路由到自身。

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". 首先尝试“ telnet 46.155.17.100 8802”。 If telnet can't connect to the server, the problem is with the server, or with the firewall. 如果telnet无法连接到服务器,则问题出在服务器或防火墙上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从套接字中获取IP从Java中的localhost连接 - get IP from socket connect from localhost in java 为什么RMI localhost客户端使用no localhost ip连接RMI localhost服务器 - Why RMI localhost client uses a no localhost ip to connect the RMI localhost server 如何在spring中使用localhost而不是localhost IP? - How to use localhost instead of localhost IP in spring? Kafka 生产者连接到本地主机而不是真实 IP - Kafka producer is connecting to localhost instead of the real IP 为什么我的客户端套接字无法连接到ServerSocket? - Why does my client Socket not connect to my ServerSocket? 为什么Socket.connect使用SocketAddress而不是InetSocketAddress? - Why does Socket.connect uses a SocketAddress rather than a InetSocketAddress? 为什么SSLSocketFactory.createSocket返回Socket而不是SSLSocket? - Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket? 为什么 socket connect() 方法超时? netcat (nc) 工作正常 - Why does socket connect() method timeout? netcat (nc) works fine 检测插槽是否有错误的IP并且无法连接 - Detect if a socket has a wrong IP and cannot connect 套接字无法通过移动互联网连接到服务器,因为我使用主机名而不是IP,但可以在Eclipse和WIFI中使用 - Socket cannot connect to server via mobile internet because I use hostname instead of IP but works in i.e. Eclipse and via WIFI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM