Java - 获取服务器侦听端口的IP地址

时间:2018-05-07 14:04:52

标签: java

我有一台服务器在某个端口上侦听客户端请求。 9000

现在我的客户端并不知道服务器ip但是知道发送请求的端口..我如何从客户端知道在侦听某个端口时服务器的IP是什么?

服务器和客户端都在同一个物理网络上。 服务器始终在端口9000上侦听任何客户端请求。 客户端向服务器发送请求并期望结果...... 有时服务器会更改IP(因为DHCP)。所以我希望我的客户端在更改时知道服务器IP ...

2 个答案:

答案 0 :(得分:2)

您可以将IP地址和端口视为房屋的街道地址(≈IP地址)和公寓编号(≈端口)。从港口号码中扣除IP地址与从公寓号码中扣除街道地址一样不可能。

答案 1 :(得分:1)

如果节点位于同一本地网络中(相同的地址类,中间没有路由器), 您应该使用UDP广播进行“网络发现”。

示意性地:
- 客户端广播呼叫并等待响应 - 服务器接收呼叫(UDP boradcast呼叫不需要IP地址,只需要端口) 并回答客户(做一些握手) - 客户端收到UDP数据包,它包含发送者节点(服务器)的IP地址
客户端:

    public void broadcastCall(){
    try {
        //Open a random port to send the package
        DatagramSocket c = new DatagramSocket();
        c.setBroadcast(true);

        byte[] sendData = "DISCOVER_SERVER_REQUEST".getBytes();

        /* 
         * Try the 255.255.255.255 broadcast 
         * (or use the boradcas address of you network class like 192.168.1.255) 
         * port 9100
         */
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, 
                InetAddress.getByName("255.255.255.255"), 9100);
        c.send(sendPacket);
        System.out.println(getClass().getName() + ">>> Request packet sent to: 255.255.255.255 (DEFAULT)");

        //Wait for a response

        byte[] recvBuf = new byte[15000];
        DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
        c.receive(receivePacket);

        //We have a response
        System.out.println(getClass().getName() + ">>> Broadcast response from server: " + receivePacket.getAddress().getHostAddress());

        /*
         *  NOW you have the server IP in receivePacket.getAddress()
         */

        //Close the port!
        c.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}


服务器端:

    public void broadcastResponder() {
    try {
        /*
         * open receive datagram broadcast socket port 9100
         */
        DatagramSocket socket = new DatagramSocket(9100, InetAddress.getByName("0.0.0.0"));
        socket.setBroadcast(true);

        System.out.println(getClass().getName() + ">>>Ready to receive broadcast packets!");

        //Receive a packet
        byte[] recvBuf = new byte[15000];
        DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
        socket.receive(packet); // This method blocks until a datagram is received

        //Packet received
        System.out.println(getClass().getName() + ">>>Discovery packet received from: " + packet.getAddress().getHostAddress());

        byte[] sendData = "DISCOVER_SERVER_RESPONSE".getBytes();
        //Send a response
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, packet.getAddress(), packet.getPort());
        socket.send(sendPacket);
        System.out.println(getClass().getName() + ">>>Sent packet to: " + sendPacket.getAddress().getHostAddress());
        // close socket
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}