62,635
社区成员




package 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.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
public class MyNettyServer {
ChannelFuture f = null;
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
private void run(int port) {
try {
// Configure the server.
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
// 有连接到达时会创建一个Channel
@Override
protected void initChannel(SocketChannel channel) throws Exception {
System.out.println("has a request....");
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
// 在Channel队列中,添加自己的Handler处理业务
pipeline.addLast(new HttpServerInboundHandler());
}
});
// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
f = b.bind(port).sync();
System.out.println("Server statrted ...");
// 应用程序会一直等待,直到channel关闭
f.channel().closeFuture().sync();
} catch (Exception e) {
}
}
private void shutdown() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
MyNettyServer server = new MyNettyServer();
server.run(8000);
}
}
package server;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class HttpServerInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("server receive message : "+ msg);
ctx.channel().writeAndFlush("server response message : " + msg + "...");
ctx.close();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("read complete...");
ctx.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.out.println("throw exption...");
ctx.close();
}
}
package client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MySocketClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket client=new Socket("127.0.0.1", 8000);
PrintWriter pw=new PrintWriter(client.getOutputStream());
pw.write(“hello!”);
pw.flush();
pw.close();
br.close();
}
}