服务器代码:
public void server() throws InterruptedException {
final EchoServerHandler serverHandler = new EchoServerHandler();
EventLoopGroup group = new DefaultEventLoop();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group, group)
.channel(LocalServerChannel.class)
.localAddress(new LocalAddress("foo"))
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(serverHandler);
}
});
ChannelFuture channelFuture = bootstrap.bind().sync();
System.out.println(EchoServer.class.getName() + "--started and listening for connections on--" + channelFuture.channel().localAddress());
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
客户代码:
public void client() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(LocalChannel.class)
.remoteAddress(new LocalAddress("foo"))
.handler(new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect().sync();
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
和错误:
Exception in thread "main" io.netty.channel.ChannelException: connection refused
at io.netty.channel.local.LocalChannel$LocalUnsafe.connect(LocalChannel.java:375)
at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1245)
at io.netty.channel.ChannelHandlerInvokerUtil.invokeConnectNow(ChannelHandlerInvokerUtil.java:118)
at io.netty.channel.DefaultChannelHandlerInvoker.invokeConnect(DefaultChannelHandlerInvoker.java:238)
at io.netty.channel.PausableChannelEventExecutor.invokeConnect(PausableChannelEventExecutor.java:107)
at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:493)
at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:487)
at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:1018)
at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:241)
at io.netty.bootstrap.Bootstrap$3.run(Bootstrap.java:230)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:328)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
我浏览一下google和github上的一些相关信息,但是没有答案,我看一些例子并复制它,但是错误仍然在附近发生故障...... 这是我第一次来这里提问。