netty传递字符串(Server)

配置引导类为

装配ServerBootStrap
            serverBootstrap.group(eventLoopGroup)
            .channel(NioServerSocketChannel.class)//指定通道类型为NioServerSocketChannel,一种异步模式
            .localAddress("localhost",port)//设置InetSocketAddress让服务器监听某个端口已等待客户端连接。
            .childHandler(new ChannelInitializer() {

             }

等待连接:

 ChannelFuture channelFuture = serverBootstrap.bind().sync();

Handler继承

 ChannelInboundHandlerAdapter

EchoServer类

package server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
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.nio.NioServerSocketChannel;

public class EchoServer{
	private final int port;
	public EchoServer(int port) {
		this.port = port;
	}
	public void start() throws Exception{
		EventLoopGroup eventLoopGroup = null;
		try{
			//创建ServerBootStrap---server端引导类
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			//实例一个eventLoopGroup用来处理事件(线程池)
			eventLoopGroup = new NioEventLoopGroup();
			//装配ServerBootStrap
			serverBootstrap.group(eventLoopGroup)
			.channel(NioServerSocketChannel.class)//指定通道类型为NioServerSocketChannel,一种异步模式
			.localAddress("localhost",port)//设置InetSocketAddress让服务器监听某个端口已等待客户端连接。
			.childHandler(new ChannelInitializer() {

				@Override
				protected void initChannel(Channel ch) throws Exception {
					// TODO Auto-generated method stub
					ch.pipeline().addLast(new EchoServerHandler());
				}
			});
			// 最后绑定服务器等待直到绑定完成,调用sync()方法会阻塞直到服务器完成绑定
			// 然后服务器等待通道关闭,因为使用sync(),所以关闭操作也会被阻塞。
			ChannelFuture channelFuture = serverBootstrap.bind().sync();
			System.out.println("开始监听,端口为:" + channelFuture.channel().localAddress());
			channelFuture.channel().closeFuture().sync();
		    }finally{
			eventLoopGroup.shutdownGracefully().sync();
		    }
	}
	public static void main(String[] args) throws Exception {
		new EchoServer(20000).start();
	}
}

EchoServerHandler类

package server;

import java.util.Date;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class EchoServerHandler extends ChannelInboundHandlerAdapter{
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("server 开始读取数据");
		ByteBuf buf = (ByteBuf)msg;
		byte[] req = new byte[buf.readableBytes()];
		buf.readBytes(req);
		String body = new String(req,"UTF-8");
		System.out.println("接受客户端的数据:"+body);
		//发送数据
		System.out.println("向客户端发送数据");
		String currentTime = new Date(System.currentTimeMillis()).toString();
		ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
		ctx.write(resp);
	}
	
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("服务端发送数据完毕");
		ctx.flush();
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
	
}

 

你可能感兴趣的:(netty传递字符串(Server))