这两天简单看了一下Netty框架的东西。
Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。也就是说,Netty 是一个基于NIO的客户,服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用。Netty相当简化和流线化了网络应用的编程开发过程,例如,TCP和UDP的socket服务开发。
所以参照官方介绍
AsyncSocket 是基于CFSocket与CFStream封装的TCP/IP socket的网络库,它提供了异步操作,本地cocoa类的delegate支持。主要关键特新如下:
1.队列的可选超时的非阻塞的读和写。比如:你告诉它读写的内容,他将在完成的时候通知你.
2.socket的自动接收。如果你告诉它接受连接,它将为每个连接建立新的实例供你调用。你也可以选择立即断开连接。
3.支持Delegate,delegate方法中包含错误、连接,接收,完整的读写、进度、以及断开连接。
4.不基于线程(thread)而基于Run-loop.虽然你可以主线程或者子线程中可以使用,但是木有必要。它使用NSRunLoop异步调用委托的方法。委托方法包括一个socket参数,允许区分多个实例。
5.自包装在一个类中。你不需要操作流或Socket。它会自己处理。
6.支持基于IPv4、IPv6的TCP流。
github地址:
当然首先第一步就是下载和安装Netty和AsyncSocket。
第二步:服务端的创建:
1.HelloServer
package com.nettypro.io; 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 HelloServer { /* * 创建服务端监听端口 */ private static final int portNumber = 8080; public static void main(String[] args) throws InterruptedException { EventLoopGroup boosGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boosGroup, workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new HelloServerInitializer()) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6); //服务器绑定端口监听 ChannelFuture channelFuture = bootstrap.bind(portNumber).sync(); //监听服务器关闭监听 channelFuture.channel().closeFuture().sync(); }finally{ boosGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
2.HelloServerInitializer
package com.nettypro.io; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; public class HelloServerInitializer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel arg0) throws Exception { // TODO Auto-generated method stub //ChannelPipeline 可以理解为消息传送通道 通道一旦建立 持续存在 ChannelPipeline channelPipeline = arg0.pipeline(); //为通道添加功能 //字符串解码 编码 channelPipeline.addLast("decoder",new StringDecoder()); channelPipeline.addLast("encoder", new StringEncoder()); //添加自主逻辑 channelPipeline.addLast(new HelloServerHandler()); } }
3.HelloServerHandler
package com.nettypro.io; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Scanner; import javax.xml.crypto.Data; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; public class HelloServerHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext arg0, String arg1) { // TODO Auto-generated method stub System.out.println(arg0.channel().remoteAddress()+" ----channelRead0"); //收到消息直接打印 System.out.println(arg0.channel().remoteAddress()+" MSG: "+ arg1); //回复消息 Scanner scanner = new Scanner(System.in); String msgString = scanner.nextLine()+"\n"; System.out.println(arg0.channel().remoteAddress()+" msgString: "+ msgString); arg0.writeAndFlush(msgString); } /** * channel被激活时调用 */ @Override public void channelActive(ChannelHandlerContext ctx){ // TODO Auto-generated method stub System.out.println(ctx.channel().remoteAddress()+" ----Acrive"); try { ctx.writeAndFlush("Welcome you to here"+InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } } }
第三步:客户端的创建:
#import "ViewController.h" #import <sys/socket.h> #import <netinet/in.h> #import <arpa/inet.h> #import <unistd.h> #import "AsyncSocket.h" @interface ViewController ()<AsyncSocketDelegate> @property (nonatomic, retain) NSTimer *heartTimer; @property (nonatomic, retain) AsyncSocket *ay; @property (strong, nonatomic) IBOutlet UITextField *msgTF; @property (strong, nonatomic) IBOutlet UITextView *showTV; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.ay = [[AsyncSocket alloc] initWithDelegate:self]; [self.ay connectToHost:@"localhost" onPort:8080 error:nil]; self.ay.delegate = self; NSString *msg = @"HelloNetty"; [self.ay writeData:[msg dataUsingEncoding:NSUTF8StringEncoding] withTimeout:10.0f tag:101]; [self.ay readDataWithTimeout:-1 tag:0]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)sendAction:(UIButton *)sender { if (self.msgTF.text.length != 0) { self.showTV.text = [NSString stringWithFormat:@"%@\n客户端说:%@",self.showTV.text,self.msgTF.text]; [self.ay writeData:[self.msgTF.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:10.0f tag:101]; [self.ay readDataWithTimeout:-1 tag:0]; self.msgTF.text = nil; } } #pragma mark #pragma mark --AsyncSocketDelegate-- - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"msg-----%@",msg); if (self.showTV.text.length == 0) { self.showTV.text = [NSString stringWithFormat:@"服务器说:%@",msg]; } else { self.showTV.text = [NSString stringWithFormat:@"%@\n服务器说:%@",self.showTV.text,msg]; } [self.ay readDataWithTimeout:-1 tag:0]; } - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag { [self.ay readDataWithTimeout:-1 tag:0]; } -(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port { NSLog(@"didConnectToHost %@------%d",host,port); [self.ay readDataWithTimeout:-1 tag:0]; } -(void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag { NSLog(@"Received bytes: %lu",(unsigned long)partialLength); } @end
运行结果: