-
-
Save jcf/141cba67d8df2457aac7c68d80272cc0 to your computer and use it in GitHub Desktop.
Netty multicast demo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.filippov.netty_demo; | |
import io.netty.bootstrap.Bootstrap; | |
import io.netty.bootstrap.ChannelFactory; | |
import io.netty.channel.*; | |
import io.netty.channel.nio.NioEventLoopGroup; | |
import io.netty.channel.socket.DatagramPacket; | |
import io.netty.channel.socket.InternetProtocolFamily; | |
import io.netty.channel.socket.nio.NioDatagramChannel; | |
import java.net.*; | |
import java.util.Enumeration; | |
public class MulticastServer extends Thread { | |
private InetSocketAddress groupAddress; | |
public MulticastServer(InetSocketAddress groupAddress) { | |
this.groupAddress = groupAddress; | |
} | |
private class MulticastHandler extends SimpleChannelInboundHandler<DatagramPacket> { | |
@Override | |
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { | |
System.out.println("receive"); | |
} | |
} | |
public void run() { | |
EventLoopGroup group = new NioEventLoopGroup(); | |
try { | |
NetworkInterface ni = NetworkInterface.getByName("en1"); | |
Enumeration<InetAddress> addresses = ni.getInetAddresses(); | |
InetAddress localAddress = null; | |
while (addresses.hasMoreElements()) { | |
InetAddress address = addresses.nextElement(); | |
if (address instanceof Inet4Address){ | |
localAddress = address; | |
} | |
} | |
Bootstrap b = new Bootstrap() | |
.group(group) | |
.channelFactory(new ChannelFactory<NioDatagramChannel>() { | |
@Override | |
public NioDatagramChannel newChannel() { | |
return new NioDatagramChannel(InternetProtocolFamily.IPv4); | |
} | |
}) | |
.localAddress(localAddress, groupAddress.getPort()) | |
.option(ChannelOption.IP_MULTICAST_IF, ni) | |
.option(ChannelOption.SO_REUSEADDR, true) | |
.handler(new ChannelInitializer<NioDatagramChannel>() { | |
@Override | |
public void initChannel(NioDatagramChannel ch) throws Exception { | |
ch.pipeline().addLast(new MulticastHandler()); | |
} | |
}); | |
NioDatagramChannel ch = (NioDatagramChannel)b.bind(groupAddress.getPort()).sync().channel(); | |
ch.joinGroup(groupAddress, ni).sync(); | |
ch.closeFuture().await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (SocketException e) { | |
e.printStackTrace(); | |
} finally { | |
group.shutdownGracefully(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
InetSocketAddress groupAddress = new InetSocketAddress("239.255.27.1", 1234); | |
new MulticastServer(groupAddress).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment