package org.netty_client;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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 void connect(String host,int port) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap strap = new Bootstrap();
strap.group(group);
strap.channel(NioSocketChannel.class);
strap.option(ChannelOption.TCP_NODELAY, true);
strap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel arg0) throws Exception {
arg0.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture future = strap.connect(host, port).sync();
future.channel().closeFuture().sync();
System.out.println("close");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) {
new Client().connect("127.0.0.1", 8080);
}
}
package org.netty_client;
import java.io.UnsupportedEncodingException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ClientHandler extends ChannelHandlerAdapter{
private ByteBuf buf;
public ClientHandler() {
String r = "first request";
byte[] req = r.getBytes();
buf = Unpooled.buffer(req.length);
buf.writeBytes(req);
System.out.println("send request:" + r);
}
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(buf);
}
public void channelkRead(ChannelHandlerContext ctx,Object msg) throws UnsupportedEncodingException {
System.out.println(1);
ByteBuf buf = (ByteBuf)msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req,"UTF-8");
System.out.println("receive response:" + body);
}
public void exceptionCaught(ChannelHandlerContext ctx,Throwable e) {
e.printStackTrace();
ctx.close();
}
}
package org.netty_server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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 void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap server = new ServerBootstrap();
server.group(bossGroup,workerGroup);
server.channel(NioServerSocketChannel.class);
server.option(ChannelOption.SO_BACKLOG, 1024);
server.childHandler(new ChannelInitializer<SocketChannel>(){
@Override
protected void initChannel(SocketChannel arg0) throws Exception {
arg0.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture future = server.bind(port).sync();
System.out.println("启动服务端口:" + port);
future.channel().closeFuture().sync();
System.out.println("close");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
new Server().bind(8080);
}
}
package org.netty_server;
import java.io.UnsupportedEncodingException;
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{
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg) throws UnsupportedEncodingException {
ByteBuf buf = (ByteBuf)msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req,"UTF-8");
System.out.println("receive request:" + body);
String r = "response";
byte[] re = r.getBytes();
ByteBuf resp = Unpooled.copiedBuffer(re);
// resp.readBytes(re);
System.out.println("send response:" + r);
ctx.write(resp);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
public void exceptionCaught(ChannelHandlerContext ctx,Throwable e) {
e.printStackTrace();
ctx.close();
}
}
测试结果:
send request:first request
启动服务端口:8080
receive request:first request
send response:response
但是客户端没收到响应,有朋友能看下么,只是写个简单的例子而已