• iOS socket 通讯 客户端和服务端(CocoaAsyncSocket 的用法)


    CocoaAsyncSocket 用法:

    客户端:
    #import "ViewController.h"
    #import "GCDAsyncSocket.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UITextField *ipField;
    @property (weak, nonatomic) IBOutlet UITextField *portField;
    @property (weak, nonatomic) IBOutlet UITextField *sendMsgField;
    @property (weak, nonatomic) IBOutlet UITextView *reciveMsgTextView;
    
    
    //客户端socket
    @property (nonatomic) GCDAsyncSocket *clientSocket;
    
    @end
    
    @implementation ViewController
    
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //1、初始化
    self.ipField.text = @"172.16.1.6";
    self.clientSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    }
    
    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)linkButtonAction:(id)sender {
    //连接服务器
    [self.clientSocket connectToHost:self.ipField.text onPort:self.portField.text.integerValue withTimeout:-1 error:nil];
    }
    - (IBAction)sendMsgButtonAction:(id)sender {
    NSData *data = [self.sendMsgField.text dataUsingEncoding:NSUTF8StringEncoding];
    //withTimeout -1 :无穷大
    //tag: 消息标记
    [self.clientSocket writeData:data withTimeout:-1 tag:0];
    }
    
    - (void)showMessageWithText:(NSString *)text {
    self.reciveMsgTextView.text = [self.reciveMsgTextView.text stringByAppendingFormat:@"%@
    ", text];
    }
    
    #pragma mark - GCDAsynSocket Delegate
    - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
    [self showMessageWithText:@"链接成功"];
    [self showMessageWithText:[NSString stringWithFormat:@"服务器IP : %@", host]];
    [self.clientSocket readDataWithTimeout:-1 tag:0];
    }
    
    //收到消息
    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [self showMessageWithText:text];
    [self.clientSocket readDataWithTimeout:-1 tag:0];
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    }
    
    @end
    

      

    服务端:
    #import "ViewController.h"
    #import "GCDAsyncSocket.h"
    
    @interface ViewController ()<GCDAsyncSocketDelegate>
    
    /** 端口号 **/
    @property (weak, nonatomic) IBOutlet UITextField *portField;
    
    /** 发送消息 **/
    @property (weak, nonatomic) IBOutlet UITextField *sendMsgField;
    
    /** 连接端口号按钮 **/
    @property (weak, nonatomic) IBOutlet UIButton *linkPortButton;
    
    /** 发送消息按钮 **/
    @property (weak, nonatomic) IBOutlet UIButton *sendMsgButton;
    
    /** 接受消息 **/
    @property (weak, nonatomic) IBOutlet UITextView *reciveMsgTextView;
    
    
    
    //服务器socket(开放端口,监听客户端socket的链接)
    @property (nonatomic) GCDAsyncSocket *serverSocket;
    
    //保护客户端socket
    @property (nonatomic) GCDAsyncSocket *clientSocket;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //初始化服务器socket
    self.serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    }
    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
    }
    
    /** 连接端口 **/
    - (IBAction)linkPortButtonAction:(id)sender {
    NSError *error = nil;
    BOOL result = [self.serverSocket acceptOnPort:self.portField.text.integerValue error:&error];
    if (result && error == nil) {
    //开放成功
    [self showMessageWithText:@"连接成功"];
    }
    }
    
    
    /** 发消息 **/
    - (IBAction)sendMsgButtonAction:(id)sender {
    NSData *data = [self.sendMsgField.text dataUsingEncoding:NSUTF8StringEncoding];
    //withTimeout -1: 一直等
    //tag:消息标记
    [self.clientSocket writeData:data withTimeout:-1 tag:0];
    }
    
    - (void)showMessageWithText:(NSString *)text {
    self.reciveMsgTextView.text = [self.reciveMsgTextView.text stringByAppendingFormat:@"%@
    ",text];
    }
    
    #pragma mark - 服务器socket Delegate
    - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket{
    //保存客户端的socket
    self.clientSocket = newSocket;
    [self showMessageWithText:@"链接成功"];
    
    [self showMessageWithText:[NSString stringWithFormat:@"服务器地址:%@ -端口: %d", newSocket.connectedHost, newSocket.connectedPort]];
    [self.clientSocket readDataWithTimeout:-1 tag:0];
    }
    
    //收到消息
    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [self showMessageWithText:text];
    [self.clientSocket readDataWithTimeout:-1 tag:0];
    }
    
    
    @end
    

      

     gitHub 地址:https://github.com/lc081200/cocoaAsynSocketExample

  • 相关阅读:
    由VMnet引起的browser-sync故障解决方案
    Gen8折腾日记
    实变函数笔记(1)——集合与基数
    密码学笔记(6)——复杂度及其相关内容
    微分几何笔记(1)——参数曲线、内积、外积
    密码学笔记(5)——Rabin密码体制和语义安全性
    密码学笔记(4)——RSA的其他攻击
    密码学笔记(2)——RSA密码
    密码学笔记(1)——数论准备知识
    第七章小结
  • 原文地址:https://www.cnblogs.com/saytome/p/7715152.html
Copyright © 2020-2023  润新知