Netty4.x HelloWorld

服务器端代码如下:

  

package nettyStudy;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
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;
import io.netty.channel.socket.nio.NioSocketChannel;

public class HelloWorldServer {
	public static int currentIndex=1;
	public static void main(String[] args) {
		
		EventLoopGroup bossGroup=new NioEventLoopGroup();//接受连接请求
		EventLoopGroup workGroup=new NioEventLoopGroup();//单个工作
		try{
			ServerBootstrap b=new ServerBootstrap();//帮助开启一个服务器
			b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
					
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(new HelloWorldServerHandler(currentIndex++));	
				}
			}).option(ChannelOption.SO_BACKLOG, 128)//针对NioServerChannel
			.childOption(ChannelOption.SO_KEEPALIVE, true);//针对Channel
			//绑定端口接受进来的连接
			ChannelFuture f=b.bind(10086).sync();
			f.channel().closeFuture().sync();//关闭服务器
		}catch(Exception e)
		{
			
			
		}
	}
	
	/**
	 * 下面的类相当于针对单独的客户端进行处理
	 * @author zpc
	 *
	 */
	private static class HelloWorldServerHandler extends ChannelInboundHandlerAdapter
	{
		
		private int currentClientIndex;
		public HelloWorldServerHandler(int currentClientIndex) {
			this.currentClientIndex=currentClientIndex;
		}
		@Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("这是第"+currentClientIndex+"个连接");
		}
		@Override
		public void channelInactive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("这是第"+currentClientIndex+"个关闭");
		}
		
		
	}
}
客户端代码如下:
package nettyStudy;

import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class HelloWorldClient {
	public static void main(String[] args) {
		Bootstrap bootstrap=new Bootstrap();//客户端启动服务器
		bootstrap.channel(NioSocketChannel.class);
		bootstrap.handler(new HelloClientHandler());//指定handler
		bootstrap.group(new NioEventLoopGroup());//指定与服务器端通信的
		bootstrap.connect(new InetSocketAddress("localhost",10086));//连接地址和端口
		
	}
	
	private static class HelloClientHandler extends ChannelInboundHandlerAdapter
	{

		@Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			// TODO Auto-generated method stub
			
			System.out.println("连通了,我打印");
		}
		
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值