iOS socket第三方库 AsyncSocket(GCDAsyncSocket)

socket第三方库 AsyncSocket(GCDAsyncSocket)


Socket描述了一个IP、端口对。它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息。所以,Socket一定包含了通信的双发,

即客户端(Client)与服务端(server)。

1)服务端利用Socket监听端口;

2)客户端发起连接;

3)服务端返回信息,建立连接,开始通信;

4)客户端,服务端断开连接。


1、套接字(socket)概念

套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。

应用层通过传输层进行数据通信时,TCP会遇到同时为多个应用程序进程提供并发服务的问题。多个TCP连接或多个应用程序进程可能需要通过同一个TCP协议端口传输数据。为了**区别不

同的应用程序进程和连接**,许多计算机操作系统为应用程序与TCPIP协议交互提供了套接字(Socket)接口。应用层可以和传输层通过Socket接口,区分来自不同应用程序进程或网络

连接的通信,实现数据传输的并发服务。


2、建立socket连接

建立Socket连接至少需要一对套接字,其中一个运行于客户端,称为ClientSocket,另一个运行于服务器端,称为ServerSocket

套接字之间的连接过程分为三个步骤:服务器监听,客户端请求,连接确认。

服务器监听:服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态,等待客户端的连接请求。

客户端请求:指客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号

,然后就向服务器端套接字提出连接请求。

连接确认:当服务器端套接字监听到或者说接收到客户端套接字的连接请求时,就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发

给客户端,一旦客户端确认了此描述,双方就正式建立连接。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。


3SOCKET连接与TCP连接

创建Socket连接时,可以指定使用的传输层协议,Socket可以支持不同的传输层协议(TCPUDP),当使用TCP协议进行连接时,该Socket连接就是一个TCP连接。


4Socket连接与HTTP连接

由于通常情况下Socket连接就是TCP连接,因此Socket连接一旦建立,通信双方即可开始相互发送数据内容,直到双方连接断开。但在实际网络应用中,客户端到服务器之间的通信往往

需要穿越多个中间节点,例如路由器、网关、防火墙等,大部分防火墙默认会关闭长时间处于非活跃状态的连接而导致Socket 连接断连,因此需要通过轮询告诉网络,该连接处于活跃状

态。

HTTP连接使用的是请求响应的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后,服务器端才能回复数据。

很多情况下,需要服务器端主动向客户端推送


iphone的标准推荐CFNetwork

C库编程.但是编程比较烦躁。在其它OS往往用类来封装的对Socket函数的处理。比如MFCCAsysncSocket.iphone也有类似于

开源项目.cocoa

AsyncSocket, 官方网站:http://code.google.com/p/cocoaasyncsocket/ 它用来简化


CFnetwork的调用.

一、在项目引入ASyncSocket

1、下载ASyncSocket库源码

2、把ASyncSocket库源码加入项目:只需要增加RunLoop目录中的AsyncSocket.hAsyncSocket.mAsyncUdpSocket.hAsyncUdpSocket.m四个文件。

3、在项目增加CFNetwork框架

Framework目录右健,选择Add-->Existing Files...    ,

选择 CFNetwork.framework


二、TCP客户端

1、在controller头文件定义AsyncSocket对象

import

import "AsyncSocket.h"


@interface HelloiPhoneViewController : UIViewController

{

    UITextField *textField;

    AsyncSocket *asyncSocket;

}


@property (retain, nonatomic) UITextField *textField;

- (void)buttonPressed:(id)sender;

- (void)textFieldDoneEditing:(id)sender;


@end


2、在需要联接地方使用connectToHost联接服务器

其中initWithDelegate的参数中self是必须。这个对象指针中的各个Socket响应的函数将被ASyncSocket所调用.


asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];

NSError *err = nil;

if (![asyncSocket connectToHost:host on:port error:&err])

{

    NSLog(@"Error: %@", err);

}


3、增加Socket响应事件

因为initWithDelegate把将当前对象传递进去,这样只要在当前对象方法实现相应方法.


4、关于NSData对象

无论SOCKET收发都采用NSData对象.它的定义是 http://developer.apple.com/library/mac

/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html

NSData主要是带一个(id)data指向的数据空间和长度 length.

NSString 转换成NSData 对象

NSData *xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];

NSData 转换成NSString对象

NSData *data;

NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


5、发送数据

AsyncSocket  writeData    方法来发送数据,它有如下定义

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;


以下是一个实例语句.

NSData *aData= [@"test data" dataUsingEncoding:NSUTF8StringEncoding];

[sock writeData:aData withTimeout:-1 tag:1];

onSocket重载函数,有如定义采用是专门用来处理SOCKET的发送数据的:

- (void)onSocket(AsyncSocket *)sock didWriteDataWithTag:(long)tag

{

    NSLog(@"thread(%),onSocket:%p didWriteDataWithTag:%d",[[NSThread currentThread] name], sock,tag);

}


6、接收Socket数据.

onSocket重载函数,有如定义采用是专门用来处理SOCKET的接收数据的.

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

在中间将其转换成NSString进行显示.

NSString *aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"===%@",aStr);

[aStr release];


Asyncsocket的例子

下面是用开源的库Asyncsocket的例子:


//

//  SocketDemoViewController.h

//  SocketDemo

//

//  Created by xiang xiva on 10-7-10.

//  Copyright 2010 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "AsyncSocket.h"

#define SRV_CONNECTED 0

#define SRV_CONNECT_SUC 1

#define SRV_CONNECT_FAIL 2

#define HOST_IP @"192.168.110.1"

#define HOST_PORT 8080


@interface SocketDemoViewController : UIViewController

{

    UITextField *inputMsg;

    UILabel *outputMsg;

    AsyncSocket *client;

}


@property (nonatomic, retain) AsyncSocket *client;

@property (nonatomic, retain) UITextField *inputMsg;

@property (nonatomic, retain) UILabel *outputMsg;


- (int)connectServer:(NSString *)hostIP port:(int)hostPort;

- (void)showMessage:(NSString *)msg;

- (void)sendMsg;

- (void)reConnect;

- (void)textFieldDoneEditing:(id)sender;

- (void)backgroundTouch:(id)sender;


@end


//

//  SocketDemoViewController.m

//  SocketDemo

//

//  Created by xiang xiva on 10-7-10.

//  Copyright 2010 __MyCompanyName__. All rights reserved.

//


#import "SocketDemoViewController.h"


@implementation SocketDemoViewController


@synthesize inputMsg, outputMsg;

@synthesize client;

/*

 // The designated initializer. Override to perform setup that is required before the view is loaded.

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

 if (self) {

 // Custom initialization

 }

 return self;

 }

 */


/*

 // Implement loadView to create a view hierarchy programmatically, without using a nib.

 - (void)loadView {

 }

 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    //[super viewDidLoad];

    [self connectServer:HOST_IP port:HOST_PORT];

    // 监听读取

}


// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return YES;

}


- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc that aren't in use.

}


- (void)viewDidUnload {

    self.client = nil;

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (int)connectServer:(NSString *)hostIP port:(int)hostPort

{

    if (client == nil)

    {

        client = [[AsyncSocket alloc] initWithDelegate:self];

        NSError *err = nil;

        //192.168.110.128

        if (![client connectToHost:hostIP onPort:hostPort error:&err])

        {

            NSLog(@"%@ %@", [err code], [err localizedDescription]);

            

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[@"Connection failed to host "

                                                                     stringByAppendingString:hostIP]

                                                            message:[[[NSString alloc]initWithFormat:@"%@",[err code]] stringByAppendingString:[err localizedDescription]]

                                                           delegate:self

                                                  cancelButtonTitle:@"OK"

                                                  otherButtonTitles:nil];

            [alert show];

            [alert release];

            //client = nil;

            return SRV_CONNECT_FAIL;

        }

        else

        {

            NSLog(@"Conectou!");

            return SRV_CONNECT_SUC;

        }

    }

    else

    {

        [client readDataWithTimeout:-1 tag:0];

        return SRV_CONNECTED;

    }

    

}


- (void)reConnect

{

    int stat = [self connectServer:HOST_IP port:HOST_PORT];

    switch (stat)

    {

        case SRV_CONNECT_SUC:[self showMessage:@"connect success"];break;

        case SRV_CONNECTED:[self showMessage:@"It's connected,don't agian"];break;

        default:break;

    }

}


- (void)sendMsg

{

    NSString *inputMsgStr = self.inputMsg.text;

    NSString * content = [inputMsgStr stringByAppendingString:@"rn"];

    NSLog(@"%a",content);

    NSData *data = [content dataUsingEncoding:NSISOLatin1StringEncoding];

    [client writeData:data withTimeout:-1 tag:0];

    

    //[data release];

    //[content release];

    //[inputMsgStr release];

    //继续监听读取

    //[client readDataWithTimeout:-1 tag:0];

}


#pragma mark -

#pragma mark close Keyboard

- (void)textFieldDoneEditing:(id)sender

{

    [sender resignFirstResponder];

}


- (void)backgroundTouch:(id)sender

{

    [inputMsg resignFirstResponder];

}


#pragma mark socket uitl


- (void)showMessage:(NSString *)msg

{

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert!"

                                                    message:msg

                                                   delegate:nil

                                          cancelButtonTitle:@"OK"

                                          otherButtonTitles:nil];

    [alert show];

    [alert release];

}



#pragma mark socket delegate


- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port

{

    [client readDataWithTimeout:-1 tag:0];

}


- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err

{

    NSLog(@"Error");

}


- (void)onSocketDidDisconnect:(AsyncSocket *)sock

{

    NSString *msg = @"Sorry this connect is failure";

    [self showMessage:msg];

    [msg release];

    client = nil;

}


- (void)onSocketDidSecure:(AsyncSocket *)sock

{

    

}


- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

{

    NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"Hava received datas is :%@",aStr);

    self.outputMsg.text = aStr;

    [aStr release];

    [client readDataWithTimeout:-1 tag:0];

}


#pragma mark dealloc


- (void)dealloc

{

    [client release];

    [inputMsg release];

    [outputMsg release];

    [super dealloc];

}


@end

发布了784 篇原创文章 · 获赞 131 · 访问量 138万+
展开阅读全文

oc中,我用GCDAsyncSocket连上了socket,但是无法往服务器发送数据

01-04

#import "GCDSocketManager.h" #import "GCDAsyncSocket.h" #define SocketHost @"61.191.45.94" #define SocketPort 8091 @interface GCDSocketManager()<GCDAsyncSocketDelegate> //握手次数 @property(nonatomic,assign) NSInteger pushCount; //断开重连定时器 @property(nonatomic,strong) NSTimer *timer; //重连次数 @property(nonatomic,assign) NSInteger reconnectCount; @end @implementation GCDSocketManager //全局访问点 + (instancetype)sharedSocketManager { static GCDSocketManager *_instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } //可以在这里做一些初始化操作 - (instancetype)init { self == [super init]; if (self) { self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; } return self; } #pragma mark 请求连接 //连接 - (void)connectToServer { self.pushCount = 0; self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; // [self.socket connectToHost:SocketHost onPort:SocketPort error:&error]; [self.socket connectToHost:SocketHost onPort:SocketPort error:&error]; if (error) { NSLog(@"SocketConnectError:%@",error); } } #pragma mark 连接成功 //连接成功的回调 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { NSLog(@"socket连接成功"); [self sendDataToServer]; } //连接成功后向服务器发送数据 - (void)sendDataToServer { NSString *jsonData = @"kUOhLq1vFfPqzx5WtcJr0ZWmZ89uM9KEx4bthINy3HL2QD8oLjBGXhVv2gAlDjfwtTp0HrwnGLYxAloJ+ABzqzIS2OBG6MwDJLwQmJVPajgcNZhLZKFzmdvWbo+7HR+hSN2Co4DQRQfUm2n8HsDjAyNVUgpmO39NJ4XY085oGPE/dHkq/t5Y7A=="; NSData *cmdData = [jsonData dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"socket发送数据--->%@",cmdData); //发送 [self.socket writeData:cmdData withTimeout:-1 tag:0]; //读取数据 [self.socket readDataWithTimeout:-1 tag:100]; } //连接成功向服务器发送数据后,服务器会有响应 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"收到的数据-->%@",msg); [self.socket readDataWithTimeout:-1 tag:200]; //服务器推送次数 self.pushCount++; //在这里进行校验操作,情况分为成功和失败两种,成功的操作一般都是拉取数据 } #pragma mark 连接失败 //连接失败的回调 - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { NSLog(@"Socket连接失败"); self.pushCount = 0; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *currentStatu = [userDefaults valueForKey:@"Statu"]; //程序在前台才进行重连 if ([currentStatu isEqualToString:@"foreground"]) { //重连次数 self.reconnectCount++; //如果连接失败 累加1秒重新连接 减少服务器压力 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 * self.reconnectCount target:self selector:@selector(reconnectServer) userInfo:nil repeats:NO]; self.timer = timer; } } //如果连接失败,5秒后重新连接 - (void)reconnectServer { self.pushCount = 0; self.reconnectCount = 0; //连接失败重新连接 NSError *error = nil; [self.socket connectToHost:SocketHost onPort:SocketPort error:&error]; if (error) { NSLog(@"SocektConnectError:%@",error); } } #pragma mark 断开连接 //切断连接 - (void)cutOffSocket { NSLog(@"socket断开连接"); self.pushCount = 0; self.reconnectCount = 0; [self.timer invalidate]; self.timer = nil; [self.socket disconnect]; } @end sendDataToServer中我调用writeData给服务器发送数据无法发送 问答

iOS AsyncSocket 获取数据问题

08-27

iOS AsyncSocket 获取数据时,获取得到数据,但是数据获取不完。如:从服务端获取图片:图片获取不完 代码如下: - (instancetype)init { self = [super init]; if (self) { if (_asyncSocket) { _asyncSocket = nil; } if (![_asyncSocket isConnected]) { NSLog(@"AsyncSocket AsyncSocket AsyncSocket"); //连接服务端 _asyncSocket = [[AsyncSocket alloc] initWithDelegate:self]; [_asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; NSError *error = nil; [_asyncSocket connectToHost:socketHost onPort:socketPort withTimeout:-1 error:&error]; if (error) { [_asyncSocket disconnect]; } } } return self; } //方法 - (void)methodName:(NSString *)method parameter:(NSString *)parameter data:(NSString *)data{ NSString *strContent = [NSString stringWithFormat:@"%@$%@$%@\n\n",method,parameter,data]; NSData *dataContent = [strContent dataUsingEncoding:NSUTF8StringEncoding]; [_asyncSocket writeData:dataContent withTimeout:-1 tag:0]; } #pragma mark socketDelegate -(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port { NSLog(@"socket连接成功"); } -(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"string %@",string); [self parserStr:string]; [_asyncSocket readDataWithTimeout:-1 tag:0]; } //返回的数据 转换 - (void)parserStr:(NSString *)str{ NSArray *ary = [str componentsSeparatedByString:@"$"]; NSString *isTrue = [ary objectAtIndex:1]; if ([isTrue isEqualToString:@"false"]) { [OMGToast showWithText:[ary objectAtIndex:2]]; }else { [self.singDelegate sendSingleData:[ary objectAtIndex:1] data:[ary objectAtIndex:0]]; } } -(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag { NSLog(@"_asySocket didWriteDataWithTag"); [sock readDataWithTimeout:-1 tag:0]; //如果是心跳包的数据就不用读取了 } -(void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err { NSLog(@"_asySocket willDisconnectWithError:%@",err); } -(void)onSocketDidDisconnect:(AsyncSocket *)sock { NSLog(@"_asySocket onSocketDidDisconnect"); [OMGToast showWithText:socketDidDisconnect]; } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览