gpt4 book ai didi

java - Netty Bootstrap java.lang.IllegalStateException : localAddress not set

转载 作者:行者123 更新时间:2023-11-29 05:16:08 24 4
gpt4 key购买 nike

我一直在尝试学习 Netty 并按照教程设置简单的服务器客户端连接,但每当我运行这部分代码时:

    public void start() throws Exception{

EventLoopGroup group = new NioEventLoopGroup();

try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host,port))
.handler(new ChannelInitializer<SocketChannel>() {

@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.bind().sync();
System.out.println("ChannelFuture bind is a success");
f.channel().closeFuture().sync();
System.out.println("ChannelFuture has been closed");
} finally {
group.shutdownGracefully().sync();
}
}

这是试图连接到端口 1234 上的 127.0.0.1 的客户端程序的一部分。我在设置在端口 1234 上运行在同一台计算机上的服务器程序后运行它。

我得到的错误是:

Exception in thread "main" java.lang.IllegalStateException: localAddress not set at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:235) at EchoClient.EchoClient.start(EchoClient.java:39) at EchoClient.EchoClient.main(EchoClient.java:57)

java:39 引用的行是“ChannelFuture f = b.bind().sync();”我不太明白 localAddress 是什么,如果它是客户端程序,为什么我需要一个本地地址。

如果有不同的话,我正在 Linux 虚拟机上运行这个程序。

最佳答案

不小心写了b.bind().sync();而不是 b.connect().sync();在客户端程序中

关于java - Netty Bootstrap java.lang.IllegalStateException : localAddress not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com