使用Netty-Java打包软件包的问题

问题描述

希望有人可以给我一些指导以解决我的问题。我不是很精通网络概念(我会的,但是现在我才刚开始:))。

仅需一点上下文。我正在尝试从Java应用程序使用Netflow。我有一个通过端口2055将Netflow UDP软件包发送到我的计算机的防火墙。我已连接到VPN,所以我为其分配了IP地址。我正在Ubuntu中工作,因此检查ifconfig可以看到正在接收这些软件包的接口。

到目前为止,一切都很好,我确定这些软件包即将到来,因为我以前是用wireshark检查过的(实际上我是在使用Graylog查看这些内容的。)

问题出在这里,我正在使用Netty来实现此目的,因此我有一个非常原始的实现来开始获取这些消息。

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.socketChannel;
import org.springframework.stereotype.Service;

import javax.annotation.postconstruct;

@Service
public class DefaultNettyCollector {

    int port = 2055;
    Channel channel;
    EventLoopGroup workGroup = new EpollEventLoopGroup(1);

    public ChannelFuture connectLoop() throws Exception {
        try{
            Bootstrap b = new Bootstrap();
            b.group(workGroup);
            b.channel(EpollSocketChannel.class);
            b.option(ChannelOption.so_KEEPALIVE,true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    socketChannel.pipeline().addLast(new NettyHandler());
                }
            });
            ChannelFuture channelFuture = b.connect("172.16.*.*",this.port).sync();
            this.channel = channelFuture.channel();

            return channelFuture;
        }finally{
        }
    }

}

我遇到此异常:

io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) Failed: Connection refused: /172.16.*.*:2055
Caused by: java.net.ConnectException: finishConnect(..) Failed: Connection refused
    at io.netty.channel.unix.Errors.throwConnectException(Errors.java:124)
    at io.netty.channel.unix.socket.finishConnect(Socket.java:243)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:672)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:649)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529)
    at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:465)
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)

好的,一些澄清:

  • 图像中的IP地址和代码被混淆了,我想我不应该这样做,但这只是为了在此处发布问题,我想这是一个私有IP地址,但这是我的公司,所以我不想共享这些信息。重要的是我的代码中的IP地址与 tun0 接口分配的IP地址相同。

  • 我使用相同端口的其他IP地址测试了我的代码包括0.0.0.0和127.0.0.1。同样的例外。

  • 我使用TCP端口测试了相同的代码,它起作用了。我开始从计算机上使用netcat创建的服务器发送消息。我能够收到消息。

  • 代码中的NettyHandler类如下:

  
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;

public class NettyHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        String message = byteBuf.toString(Charset.defaultCharset());
        System.out.println("Received Message : " + message);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }
}

我什至尝试使用此处理程序为UDP消息创建更具体的

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

public abstract class DefaultUdpMessageHandler<T> extends SimpleChannelInboundHandler<DatagramPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx,DatagramPacket msg) throws Exception {
        System.out.println(msg.content());
    }

}

这是我到目前为止尝试过的。希望这只是我缺乏网络概念,您可以为我可以检查的其他事情提供一些指导。

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)