`

netty入门example

阅读更多

版本:3.2.1.Final

服务器端

package com.test.net;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

/**
 * 服务器端
 * @author root
 *
 */
public class TestNettyServer
{

	public static void main(String[] args)
	{
		//1.创建服务器端启动器
		ServerBootstrap serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
		//2.创建一个管道工厂,返回一个管道,并为该管道绑定处理器
		serverBootstrap.setPipelineFactory(new ChannelPipelineFactory()
		{
			@Override
			public ChannelPipeline getPipeline() throws Exception
			{
				return Channels.pipeline(new HelloServerChannelHandler());
			}
		});
		//3.绑定端口
		serverBootstrap.bind(new InetSocketAddress(8989));
		System.out.println("服务器启动,端口是8989");
	}

	/**
	 * 事件处理器
	 * @author root
	 *
	 */
	private static class HelloServerChannelHandler extends SimpleChannelHandler
	{
		public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
		{
			System.out.println("One client has conneted...");
		}

	}

}

 

客户端:

package com.test.net;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

/**
 * 客户端
 * @author root
 *
 */
public class TestNettyClient
{

	public static void main(String[] args)
	{
		//1.创建客户端启动器
		ClientBootstrap clientBootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
		//2.创建一个管道工厂,返回一个管道,并为该管道绑定处理器
		clientBootstrap.setPipelineFactory(new ChannelPipelineFactory()
		{

			@Override
			public ChannelPipeline getPipeline() throws Exception
			{
				return Channels.pipeline(new HelloClientChannelHandler());
			}
		});
		//绑定要连接的ip和端口
		clientBootstrap.connect(new InetSocketAddress("localhost", 8989));

	}
	
	/**
	 * 事件处理器
	 * @author root
	 *
	 */
	private static class HelloClientChannelHandler extends SimpleChannelHandler
	{

		@Override
		public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
		{
			System.out.println("connected...");
		}
		
	}

}

 

 

分别运行服务器端和客户端

 

分享到:
评论

相关推荐

    netty入门例子--(不是翻译官方文档)

    这个“netty入门例子”旨在帮助初学者理解Netty的基本用法和特性,而不是简单地翻译官方文档,它提供了几个开发模板,以便于深入理解Netty中的消息异步通信机制和TCP通信的封装。 首先,Netty的核心是它的异步模型...

    Netty+maven的入门程序

    在 Group ID 和 Artifact ID 字段中输入项目的基本标识,例如 "com.example" 和 "netty-maven",接着完成其他必要的配置。 2. **配置 pom.xml**: Maven 使用 `pom.xml` 文件来管理项目信息和依赖。在这个项目中,你...

    netty+protobuf入门案例.

    这个入门案例展示了如何使用Netty处理网络连接,并通过Protobuf进行高效的数据交换。实际项目中,你可能需要处理更复杂的协议,例如心跳检查、错误处理、多消息类型的解析等。但这个基础已经足够帮助你理解这两项...

    Netty实践学习案例

    在 "177.netty-learning-example__sanshengshui" 这个压缩包中,我们可以预期找到一些 Netty 实践的示例代码和教程,这可能包括以下部分: 1. **基础入门**:这些示例可能会涵盖如何创建一个简单的 Netty 服务器和...

    Netty原理与应用

    package org.jboss.netty.example.discard; @ChannelPipelineCoverage("all") public class DiscardServerHandler extends SimpleChannelHandler { @Override public void messageReceived(ChannelHandlerContext...

    netty官方例子

    Netty 是一个高性能、异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。这个“netty官方例子”压缩包提供了一系列的...同时,这些例子也适用于初学者入门,帮助他们快速上手Netty框架。

    netty-learning-example:Netty实践学习案例,见微知著!带着你的心,跟着教程。我相信你行欧

    『基础-入门篇』 :OK_hand: :netty-helloworld b。 『基础-通讯协议篇』 :OK_hand: :netty-http :OK_hand: netty-springboot-protobuf :OK_hand: netty-mqtt C。 『中级-数据传输篇』 Netty碰上关系型...

    avro-rpc程序示例

    **Avro RPC快速入门** 在"avro-rpc-quickstart-master"这个压缩包中,包含了Avro RPC的快速启动示例,包括客户端和服务器端的源代码。以下是快速启动的步骤: 1. **构建环境**: 确保已经安装了Java开发工具(JDK)...

Global site tag (gtag.js) - Google Analytics