如何解决当我使用 Netty 在此端口上创建 UDP 监视器时,为什么端口状态不是监听?
这是创建频道的代码:
public class StunServer {
private StunServer(){
boolean win = GeneralUtil.windows();
if(win){
System.out.println("windows");
this.group = new NioEventLoopGroup();
}else{
System.out.println("linux");
this.group = new EpollEventLoopGroup();
}
this.bootstrap = new Bootstrap();
bootstrap.group(group).channel(win?NioDatagramChannel.class:EpollDatagramChannel.class)
// .option(ChannelOption.SO_BROADCAST,true)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline()
.addLast(new StunUdpMsgDecoder())
.addLast(new StunUdpMsgEncoder())
.addLast(new StunUdpMsgHandler());
}
});
}
private static StunServer instance = new StunServer();
public static StunServer getInstance(){
return instance;
}
private final Bootstrap bootstrap;
private final EventLoopGroup group;
public Channel getChannel(int port){
System.out.println("bind port:"+port);
ChannelFuture channelFuture = bootstrap.bind(port).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if(!channelFuture.isSuccess()){
channelFuture.cause().printStackTrace();
channelFuture.channel().close();
}
}
}).syncUninterruptibly();
return channelFuture.channel();
}
public void stop(){
group.shutdownGracefully().syncUninterruptibly();
}
}
这是主要方法:
public static void main(String[] args) throws Exception{
StunServer server = StunServer.getInstance();
try {
Channel channel = server.getChannel(VarEnums.server1.getPort());
channel.closeFuture().sync();
}catch(Exception e){
e.printStackTrace();
}finally {
server.stop();
}
}
很简单,我很确定消息发送成功。但是解码器没有被执行。 发现端口没有监听。
如你所见。它应该是这样的?我猜。
无论如何,伙计们帮助我,谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。