iOS开发-AsynSocket

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

导入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  

标签: idc 服务器 服务器端

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C语言中随机函数发生器rand()用法举例

下一篇:C语言经典算法之 - Algorithm Gossip: 三色棋