iOS开发-AsynSocket

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

导入AsynSocket库,导入CFNetwork系统库

1.新建single view工程

ViewController.h文件

    #import <UIKit/UIKit.h>  
    #import "AsyncSocket.h"  
      
    @interface ViewController : UIViewController <AsyncSocketDelegate> {  
        NSMutableArray *_socketArray;  
        AsyncSocket *_sendSocket;//发送  
        AsyncSocket *_recvSocket;//接收  
    }  
    - (IBAction)sendClick:(id)sender;  
    @property (retain, nonatomic) IBOutlet UITextField *ipField;  
    @property (retain, nonatomic) IBOutlet UITextField *sendField;  
    @property (retain, nonatomic) IBOutlet UITextView *msgView;  
    - (IBAction)conClick:(id)sender;  
    - (IBAction)sendClick:(id)sender;  
    @end  

2.ViewController.m文件
    #import "ViewController.h"  
      
    @implementation ViewController  
    @synthesize ipField;  
    @synthesize sendField;  
    @synthesize msgView;  
      
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
          
        //初始化socket数组,存放socket  
        _socketArray = [[NSMutableArray alloc] init];  
          
        //实例化客户端和服务器socket  
        _sendSocket = [[AsyncSocket alloc] initWithDelegate:self];  
        _recvSocket = [[AsyncSocket alloc] initWithDelegate:self];  
          
        //服务器段开始监听  
        [_recvSocket acceptOnPort:5566 error:nil];  
    }  
      
    //服务器端接收到新的socket  
    - (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket {  
        //服务器端将收到的socket存入数组  
        [_socketArray addObject:newSocket];  
        //开始接收数据  
        [newSocket readDataWithTimeout:-1 tag:0];  
    }  
      
    //服务器端接收到信息  
    - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {  
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
        self.msgView.text = [NSString stringWithFormat:@"%@%@:%@\n", self.msgView.text, sock.connectedHost, string];  
        [sock readDataWithTimeout:-1 tag:0];//循环  
    }  
      
    //连接  
    - (IBAction)conClick:(id)sender {  
        if (_sendSocket.isConnected) {  
            [_sendSocket disconnect];  
        }  
        [_sendSocket connectToHost:self.ipField.text onPort:5566 withTimeout:30 error:nil];  
    }  
      
    //发送消息  
    - (IBAction)sendClick:(id)sender {  
        NSData *data = [self.sendField.text dataUsingEncoding:NSUTF8StringEncoding];  
        [_sendSocket writeData:data withTimeout:30 tag:0];  
        self.sendField.text = @"";  
    }  
      
    - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port  
    {  
        NSLog(@"连接服务器成功");  
    }  
      
    - (void)onSocketDidDisconnect:(AsyncSocket *)sock  
    {  
        NSLog(@"断开连接");  
    }  
      
      
      
    - (void)dealloc {  
        [ipField release];  
        [sendField release];  
        [msgView release];  
        [ipField release];  
        [super dealloc];  
    }  
      
    - (void)viewDidUnload {  
        [self setIpField:nil];  
        [super viewDidUnload];  
    }  
    @end