61FIT3NPR - W09 Tut Multicast Socket New
61FIT3NPR - W09 Tut Multicast Socket New
HANOI UNIVERSITY
@Override
public void run(){
try {
receiveUDPMessage("230.0.0.0", 4321);
}catch(IOException ex){
ex.printStackTrace();
}
}
}
UDPMulticastPublisher.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
}
}
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.lang.Thread;
public class MulticastPublisher {
private static final String MULTICAST_INTERFACE = "en0";
private static final int MULTICAST_PORT = 4567;
private static final String MULTICAST_IP = "230.0.0.0";
//int n=1;
public void sendMessage(String ip, String iface, int port,String message)
throws IOException {
DatagramChannel datagramChannel=DatagramChannel.open();
datagramChannel.bind(null);
NetworkInterface networkInterface=NetworkInterface.getByName(iface);
datagramChannel.setOption(StandardSocketOptions.IP_MULTICAST_IF,networkInterfa
ce);
ByteBuffer byteBuffer=ByteBuffer.wrap(message.getBytes());
InetSocketAddress inetSocketAddress=new InetSocketAddress(ip,port);
datagramChannel.send(byteBuffer,inetSocketAddress);
}
public static void main(String[] args) throws IOException {
MulticastPublisher mp=new MulticastPublisher();
int n = 1;
while(true) {
String s = "Hi there! This is the message number ";
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;