前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发-AsynSocket

iOS开发-AsynSocket

原创
作者头像
用户8983410
修改2021-10-29 10:35:49
9050
修改2021-10-29 10:35:49
举报
文章被收录于专栏:代码小技巧分享分析

导入AsynSocket库,导入CFNetwork系统库

1.新建single view工程

ViewController.h文件

代码语言:javascript
复制
    #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  </pre> 


 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  </pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档