我的要求是从服务器向所有连接的客户端广播UDP消息。
我正在使用netty 4.x.x来实现客户端服务器。我不是我无法将消息广播到所有连接的客户端。
服务器代码
public class UDPServer {
private static final Logger logger = LogManager.getLogger(UDPServer.class.getName());
private final ServerConfig conf;
/*
* Constructor of Server class
*/
public UDPServer(ServerConfig conf) {
this.conf = conf;
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
EventLoopGroup udpGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(udpGroup)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("udpDecoder", new MessageToMessageDecoder<DatagramPacket>() {
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
out.add(msg.content());
msg.retain();
}
});
p.addLast("handler", new UDServerPHandler());
p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65535, 2, 2));
}
})
.option(ChannelOption.SO_BROADCAST, true);
// b.bind(9998).sync().channel().closeFuture().sync();
b.bind(9998).channel().closeFuture().sync();
} finally {
udpGroup.shutdownGracefully();
}
}
/**
* Main method to run the class
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
new UDPServer(new ServerConfig()).run();
}
}
服务器处理程序
package com.quantotrade.shamse.udp;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import java.util.Random;
public class UDServerPHandler extends SimpleChannelInboundHandler<DatagramPacket> {
private static final Random random = new Random();
private static final String[] quotes = {
"ONE",
"TWO",
"THREE",
"FOUR",
};
private static String nextQuote() {
int quoteId;
synchronized (random) {
quoteId = random.nextInt(quotes.length);
}
return quotes[quoteId];
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
//System.err.println(packet.content().toString(CharsetUtil.UTF_8));
//if ("Quote".equals(packet.content().toString(CharsetUtil.UTF_8))) {
// ctx.write(new DatagramPacket(Unpooled.copiedBuffer("Quote" + nextQuote(), CharsetUtil.UTF_8), packet.sender()));
ctx.channel().writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("Quote" + nextQuote(), CharsetUtil.UTF_8), packet.sender()));
//}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
}
客户代码
public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
//private final EventExecutor executor = new NioEventLoopGroup();
//private final ChannelGroup channels = new DefaultChannelGroup();
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
String response = msg.content().toString(CharsetUtil.UTF_8);
if (response.startsWith("Quote")) {
System.out.println("Quote:" + response.substring(5));
//ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("Quote", CharsetUtil.UTF_8), msg.sender()));
// channels.add(ctx.channel());
}
}
请帮助