简体   繁体   English

Java-获取在端口上监听的服务器的IP地址

[英]Java - getting IP address of server listening on a port

I have a server listening on a certain port for client request.. Ex. 我有一台服务器正在侦听某个端口上的客户端请求。 9000 9000

Now my client doesn't know the server ip but knows the port to send requests.. How do I know from the client side what is the IP of the server on listening on that certain port? 现在我的客户端不知道服务器ip,但知道发送请求的端口。我如何从客户端知道侦听该端口的服务器的IP是什么?

Both server and client is on the same physical network.. The server always listen on port 9000 for any clients request.. The clients send requests to the server and expects a result... Sometimes the server changes IP (because of DHCP).. So i want my client to know the server IP when it change... 服务器和客户端都在同一物理网络上。服务器始终在端口9000上侦听任何客户端请求。客户端将请求发送到服务器,并期望结果...有时服务器更改IP(由于DHCP)。因此,我希望我的客户端在服务器IP更改时知道服务器IP ...

You can think of the IP address and the Port as the street address of a house (≈ IP address) and an apartment number (≈ Port). 您可以将IP地址和端口视为房屋的街道地址(≈IP地址)和公寓号(≈Port)。 Deducing the IP address from a Port number is just as impossible as deducing a street address from an apartment number. 从端口号推断IP地址就像从公寓号推断街道地址一样不可能。

If the nodes are in the same local network (same address class and no router in between), you should use a UDP broadcast to make a "network discovery". 如果节点在相同的本地网络中(相同的地址类别,并且两者之间没有路由器),则应使用UDP广播进行“网络发现”。

Schematically: 示意图:
- The client broadcasts a call and wait for response. -客户端广播呼叫并等待响应。
- The server receive the call (the UDP boradcast call do not needs an IP address, only the port is required) and answer to the client (do some handshake). -服务器接收呼叫(UDP广播呼叫不需要IP地址,仅需要端口)并应答客户端(进行一些握手)。
- The client receive the UDP packet, it contains the IP address of the sender node (the server). -客户端接收UDP数据包,其中包含发送方节点(服务器)的IP地址。

Client Side: 客户端:

    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();
    }
}


Server side: 服务器端:

    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();
    }
}

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

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