When a M-SEARCH command is issued, all the devices that offer a service must reply with the IP address of the service they offer.
My cellphone has 2 interfaces (among others):
- 192.168.1.5: the wifi's interface
- 25.156.35.4: the mobile network interface
Depending on what interface do I receive the M-SEARCH I must reply using the wifi's IP or the mobiel one.
How can I figure out what interface received the request? I am looking for a robust way of doing this. Looking for 192.168... so doesnt look like a good solution.
I listen to the M_SEARCH this way:
MulticastSocket clientSocket;
clientSocket = new MulticastSocket(1900);
clientSocket.joinGroup(InetAddress.getByName("239.255.255.250") );
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
clientSocket.receive(dp);
final String msg = new String(dp.getData(), 0, dp.getLength());
if (msg.contains("M-SEARCH"))
{
DatagramSocket resSocket = new DatagramSocket(null);
String req = "HTTP/1.1 200 OK\r\n";
req += "LOCATION: http://" + IP_TO_SPECIFY + "/index.html \r\n";
req += "HOST: "+android.os.Build.MODEL+"\r\n";
req += "EXT: \r\n";
req += "ST: upnp:rootdevice\r\n";
byte [] sendData = req.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, dp.getAddress(), dp.getPort());
resSocket.send(sendPacket);
resSocket.close();
}
}
here is what CheapCast is using :-