我在学习netty时,练习的是DISCARD服务,在netty里面,丢弃服务就是Server端不去对Client端发送数据进行处理,直接进行释放,为了展示效果,我添加了部分反馈代码,以下是我的代码,
Client端
package com.nettyTest.NettyDemo;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class Client {
public static void main(String[] args) throws Exception {
//创建工程线程组
EventLoopGroup workerGroup = new NioEventLoopGroup();
//创建辅助数组,ServerBootstrap负责初始化netty服务器,并且开始监听端口的socket请求。
Bootstrap b = new Bootstrap(); // (1)
//将工程线程组加入到公作组里面
b.group(workerGroup); // (2)
//指定处理通信管道类型
b.channel(NioSocketChannel.class); // (3)
//把ClientHandler添加到ServerBootstrap处理服务器端发过来的信息
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast( new ClientHandler());
}
});
// 启动客户端并加载连接端口127.0.0.1:8080
ChannelFuture cf1 = b.connect("127.0.0.1", 8080).sync(); // (5)
//从管道里面写入数据Unpooled.copiedBuffer将字节数组 bytep[]转成缓冲流 Buffer
cf1.channel().write(Unpooled.copiedBuffer("777".getBytes()));
// flush()把缓冲区的内容强制的写出
cf1.channel().flush();
// 等待连接关闭.
cf1.channel().closeFuture().sync();
//关闭工作线程
workerGroup.shutdownGracefully();
}
}
ClietHandler处理Server发过来的信息
package com.nettyTest.NettyDemo;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ClientHandler extends ChannelHandlerAdapter {
//重写channelRead方法,
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// TODO Auto-generated method stub
//创建缓冲区buf
ByteBuf buf = (ByteBuf) msg;
//创建字节数组data
byte[] data=new byte[buf.readableBytes()];
//将缓冲区buf的内容写入data
buf.readBytes(data);
//将字节数组data转为字符串request,指定字符集
String request = new String(data, "utf-8");
System.out.println("Client: "+request);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
ctx.close();
}
}
Server端
package com.nettyTest.NettyDemo;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class Server {
public static void main(String[] args) throws Exception{
//创建接收线程组,接收Client端发过来的信息
EventLoopGroup bossGroup=new NioEventLoopGroup();
//创建工作线程组,处理实际业务
EventLoopGroup workerGroup=new NioEventLoopGroup();
//创建辅助类负责初始化netty服务器,并且开始监听端口的socket请求
ServerBootstrap b=new ServerBootstrap();
//把两个线程组加入到辅助类Bootstrap里面去
b.group(bossGroup,workerGroup)
//指定处理通信管道类型
.channel(NioServerSocketChannel.class)
//把ServerHandler添加到ServerBootstrap处理服务器端发过来的信息
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ServerHandler());
}
});
//服务器开始监听端口8080
ChannelFuture f=b.bind(8080).sync();
//等待连接关闭
f.channel().closeFuture().sync();
//关闭接收线程组
bossGroup.shutdownGracefully();
//关闭工作线程组
workerGroup.shutdownGracefully();
}
}
ServHandler处理 Client端发过来的信息,同时也可以发送信息给Client端
package com.nettyTest.NettyDemo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ServerHandler extends ChannelHandlerAdapter {
//重写channelRead方法,
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// TODO Auto-generated method stub
//创建缓冲区buf
ByteBuf buf = (ByteBuf) msg;
//创建字节数组data
byte[] data=new byte[buf.readableBytes()];
//将缓冲区buf的内容写入data
buf.readBytes(data);
//将字节数组data转为字符串request,指定字符集
String request = new String(data, "utf-8");
System.out.println("Server: "+request);
//服务器给客户端的响应
String response ="我是反馈的信息";
//从管道里面写入数据Unpooled.copiedBuffer将字节数组 bytep[]转成缓冲流 Buffer
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
ctx.close();
}
}
一开始的时候我直接启动Client端代码,然后是DISCARD服务报错,报错代码在下面,
Exception in thread "main" java.net.ConnectException: Connection refused: no further information: /127.0.0.1:8080
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:223)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:276)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:531)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
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)
然后我去百度一下,网上提供了三个方法进行排查 ,
1,防火墙进行拦截,导致Client连接被拦截,然后我去关闭了防火墙,发现仍然报错
2,端口被占用,我去使用netstat -ano | findstr "8080" 查看,没有发现端口被占用
3.要看你的server是否真正的运行起来了,比如server在启动过程某些组件加载异常,会导致服务启动不成功,我重启了server端,然后启动了Client端,然后错误解决了。