iOS UDP通讯

作者: dadahua | 来源:发表于2016-09-29 18:10 被阅读1257次

前言:

最近用GCDAsyncSocket写个小东西,UDP通讯现在大多也使用GCD,很少用Runloop。然后粗略的了解了下UDP通讯。
它是比HTTP更加底层的通讯协议,特点是:传输快无连接,系统开销也少。
更详细的基础知识:Socket理论知识
GCDAsyncUdpSocket这个框架很强大,你只需绑定端口通过代理接受消息,和对象方法发送消息。

Demo:

TCP.gif

简介

  1. 第一个界面作为服务器端,绑定端口号,监听端口号里面的消息。
  2. 第二个界面作为客户端发送消息,给服务器的端口号和局域网的IP地址(下图有获取本机IP地址方法),同时也绑定端口号,监听端口号里面消息。
  3. 消息发送后,服务器通过代理接受到消息,消息里面包括客户端的IP地址·端口·发送的内容。
  4. 服务器再在代理里面回复消息:“我收到了”,给客户端的IP地址·端口

备注:获取本机IP地址方法。

获取本机ip地址

代码

  • 服务端
// 初始化socket
-(void)initSocket {
    
    self.title = @"服务器";
    dispatch_queue_t dQueue = dispatch_queue_create("Server queue", NULL);
    receiveSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self
                                                  delegateQueue:dQueue];
    NSError *error;

    // 绑定一个端口(可选),如果不绑定端口, 那么就会随机产生一个随机的电脑唯一的端口
    // 端口数字范围(1024,2^16-1)
    [receiveSocket bindToPort:SERVERPORT error:&error];
    if (error) {
        NSLog(@"服务器绑定失败");
    }
    // 开始接收对方发来的消息
    [receiveSocket beginReceiving:nil];
}
// 接收消息代理
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
    
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    /**
     *  更新UI一定要到主线程去操作啊
     */
    dispatch_sync(dispatch_get_main_queue(), ^{
        self.textView.text = msg;
    });
    NSLog(@"客户端ip地址-->%@,port--->%u,内容-->%@",
          [GCDAsyncUdpSocket hostFromAddress:address],
          [GCDAsyncUdpSocket portFromAddress:address],
          msg);
    
    NSString *sendStr = @"我收到了";
    NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
    // 该函数只是启动一次发送 它本身不进行数据的发送, 而是让后台的线程慢慢的发送 也就是说这个函数调用完成后,数据并没有立刻发送,异步发送
    [receiveSocket sendData:sendData toHost:[GCDAsyncUdpSocket hostFromAddress:address]
                       port:[GCDAsyncUdpSocket portFromAddress:address]
                withTimeout:60
                        tag:500];
}
  • 客户端
-(void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"客户端";
    dispatch_queue_t qQueue = dispatch_queue_create("Client queue", NULL);
    sendSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self
                                               delegateQueue:qQueue];
    NSError *error;
    [sendSocket bindToPort:CLIENTPORT error:&error];
    if (error) {
        NSLog(@"客户端绑定失败");
    }
    [sendSocket beginReceiving:nil];
}
// 发送消息
-(IBAction)sendMsgClick:(UIButton *)sender {
    NSData *sendData = [msgTF.text dataUsingEncoding:NSUTF8StringEncoding];
    [sendSocket sendData:sendData
                  toHost:ipTF.text
                    port:SERVERPORT
             withTimeout:60
                     tag:200];
}
// 发送消息失败回调
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error {
    
    if (tag == 200) {
        NSLog(@"client发送失败-->%@",error);
    }
}
// 收到消息回调
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
    
    NSString *receiveStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"服务器ip地址--->%@,host---%u,内容--->%@",
          [GCDAsyncUdpSocket hostFromAddress:address],
          [GCDAsyncUdpSocket portFromAddress:address],
          receiveStr);
    
    dispatch_sync(dispatch_get_main_queue(), ^{
        receiveLab.text = receiveStr;
    });
}
// 关闭套接字,并销毁
-(void)dealloc {
    
    [sendSocket close];
    sendSocket = nil;
}

要点

  • 收到消息的回调方法里面,如果要更新UI的话,一定要切换到主线程里去实现操作。
  • 向对方发消息,方法参数的端口号要是 对方绑定的端口号。
    demo地址https://github.com/dadahua/Demo

相关文章

  • iOS UDP通讯

    前言: 最近用GCDAsyncSocket写个小东西,UDP通讯现在大多也使用GCD,很少用Runloop。然后粗...

  • IOS UDP通讯

    网址:http://my.oschina.net/u/2285956/blog/370353 1.我们利用开源框架...

  • LWIP UDP偶发丢包问题

    RTTHREAD LWIP实现UDP通讯,使用得socket通讯偶发UDP丢包,用Wireshark监控通讯数据,...

  • 2019-03-21 【c++&c#】进程间通讯__共享内存

    进程间通讯方式:共享内存,管道(linux),udp通讯(若是在同一台电脑上通过udp通讯,那么它并没有经过网络,...

  • 局域网内 UDP 获取 MAC 地址

    名词及协议 1.UDP UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,...

  • UDP通讯测试

    因为NB-IoT以UDP/CoAP为主,所以用Python socket和Twisted框架测试了一阵子。刚开始测...

  • UDP通讯协议

    UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open Sys...

  • tcp和udp的区别

    传输层主要有:tcp、udp等等 UDP: (user datagram protocol)提供无连接通讯,不能够...

  • 第九篇 异常处理和并发编程1

    一、异常处理 二、基于udp协议的套接字通讯 三、udp协议不会粘包 Tcp和udp的区别Tcp协议是可靠的协议。...

  • python面试题

    一、简述TCP和UDP的区别以及优缺点。 1、UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口...

网友评论

    本文标题:iOS UDP通讯

    本文链接:https://www.haomeiwen.com/subject/eazkyttx.html