MulticastSocket.setNetworkInterface issue
- Dominant language
- Java
- Stars
- 6k
- Forks
- 999
- Avg merge
- 19h 20m
- Merged PRs (30d)
- 14
Description
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class UDPMulticastClient {
public static void main(String[] args) throws IOException{
System.out.println("in main");
receiveUDPMessage("230.0.0.0", 4321); }
public static void receiveUDPMessage(String ip, int port) throws
IOException {
System.setProperty("java.net.preferIPv4Stack", "true");
System.out.println("in function");
byte[] buffer=new byte[1024];
MulticastSocket socket=new MulticastSocket(4321);
InetAddress group=InetAddress.getByName("230.0.0.0");
Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration addressesFromNetworkInterface = networkInterface.getInetAddresses();
while (addressesFromNetworkInterface.hasMoreElements()) {
InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
if (inetAddress.isSiteLocalAddress()
&& !inetAddress.isAnyLocalAddress()
&& !inetAddress.isLinkLocalAddress()
&& !inetAddress.isLoopbackAddress()
&& !inetAddress.isMulticastAddress()) {
socket.setNetworkInterface(NetworkInterface.getByName(networkInterface.getName()));
}
}
}
System.out.println("before joining group");
socket.joinGroup(group);
System.out.println("after joining group");
while(true){
System.out.println("Waiting for multicast message...");
DatagramPacket packet=new DatagramPacket(buffer,
buffer.length);
socket.receive(packet);
String msg=new String(packet.getData(),
packet.getOffset(),packet.getLength());
System.out.println("[Multicast UDP message received] >> "+msg);
if("OK".equals(msg)) {
System.out.println("No more message. Exiting : "+msg);
break;
}
}
socket.leaveGroup(group);
socket.close();
}
}
By Compiling and Running through javac it is working fine.
But by using,
j2objcc -g -o UDPMulticastClient UDPMulticastClient.m
./UDPMulticastClient UDPMulticastClient
It got terminated at socket.setNetworkInterface.
Contributor guide
Assessment
This issue has not been assessed yet.