In my network I have several devices (linux, c++) and one android smartphone.
Now, in order to recognize the devices in the network I am sending out a multicast udp packet from the android smartphone with the following code:
public class SSDPSocket {
SocketAddress mSSDPMulticastGroup;
MulticastSocket mSSDPSocket;
InetAddress broadcastAddress;
public SSDPSocket() throws IOException {
mSSDPSocket = new MulticastSocket(55325);
broadcastAddress = InetAddress.getByName(SSDPConstants.ADDRESS);
mSSDPSocket.joinGroup(broadcastAddress);
}
public void send(String data) throws IOException {
DatagramPacket dp = new DatagramPacket(data.getBytes(), data.length(), broadcastAddress, SSDPConstants.PORT);
mSSDPSocket.setTimeToLive(2);
mSSDPSocket.send(dp);
}
public void receive() {
// TODO: Implement!
}
}
The address to which I am sending the udp packet is 239.255.255.250
, port is 1900
.
This works fine, and I am able to receive the packets on my clients.
Now I have problems to implement the receive method. In this method I want to receive respones from all my special devices in the network. First the clients have to respond to the sender address (the ip of my smartphone) right? Then I would have some kind of loop in the receive()
method where I can receive incomming udp respones right?
Do I have to open a new DatagramSocket
for this? Or can I use the already opened MulticastSocket
for responses? In the examples I found everybody is using the opened MulticastSocket
for response, but how should this work since the MulticastSocket
is bound to 239.255.255.250
?