I am working on a system consisting of a server and an Android app, which has to contact said server. In the app, it is currently possible to specify the server address, but we now want the client to automatically look for the server on the local network to make it more usable. My current understanding of the problem is that I can send a message to the local broadcast address at 255.255.255.255, and then this message will reach all IP's in the local network. So far, I came up with the following:
try {
DatagramSocket discoveryBroadcaster = new DatagramSocket();
discoveryBroadcaster.send(new DatagramPacket(msg.getBytes(),msg.getBytes().length,
InetAddress.getByName("255.255.255.255"),8000));
}
catch (IOException e) {
e.printStackTrace();
}
We communicate with the server through a RESTful interface. I would like the message we send to be a GET request so that we can implement the response on the server as an endpoint just like everything else. The problem is that the only way I know of to create a GET request is through the httpurlconnection class, which needs a target URL first. Therefore, my questions are:
- Is it possible to compose a GET request as a string and then send it to the broadcast address?
- Will sending a GET request to the broadcast address work?
- Is the above a correct or viable approach to solving this problem?