Receiving Unicast On Multicast Socket
I have a server sending unicast UDP packets to 192.168.1.101, port 55555. My Android device has IP 192.168.1.101. My Android device has a multicast socket bound on port 55555 joine
Solution 1:
Can a multicast socket receive datagrams which are not addressed to the multicast group it's joined?
Yes it can. It can join zero or more multicast groups. That doesn't affect its unicast capabilities.
Solution 2:
Adding the below code for reference.. We are able to receive both Multicast and Unicast messages in the same port.
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
publicclassMulticastReceiver {
publicstaticvoidmain(String[] args)throws Exception {
intmcPort=1800;
StringmcIPStr="239.255.255.250";
MulticastSocketmcSocket=null;
InetAddressmcIPAddress=null;
mcIPAddress = InetAddress.getByName(mcIPStr);
mcSocket = newMulticastSocket(mcPort);
System.out.println("Multicast Receiver running at:"
+ mcSocket.getLocalSocketAddress());
mcSocket.joinGroup(mcIPAddress);
booleanvar=true;
while(var){
DatagramPacketpacket=newDatagramPacket(newbyte[1024], 1024);
System.out.println("Waiting for a multicast message...");
mcSocket.receive(packet);
System.out.println("packet length is " +packet.getLength());
Stringmsg=newString(packet.getData(),0,1024);
System.out.println("[Multicast Receiver] Received:" + msg);
}
mcSocket.leaveGroup(mcIPAddress);
mcSocket.close();
}
}
Post a Comment for "Receiving Unicast On Multicast Socket"