Reputation: 314
I have a server listening on a certain port for client request.. Ex. 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?
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...
Upvotes: 1
Views: 905
Reputation: 56
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".
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).
- The client receive the UDP packet, it contains the IP address of the sender node (the server).
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();
}
}
Upvotes: 2
Reputation: 5397
You can think of the IP address and the Port as the street address of a house (≈ IP address) and an apartment number (≈ Port). Deducing the IP address from a Port number is just as impossible as deducing a street address from an apartment number.
Upvotes: 2