A手机等的网络udp广播,收到广播以后回复udp消息

B手机:向A手机发送一条消息,等待A回复


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self broadCast];
    
    return YES;
}

- (IBAction)broadCast {
    GCDAsyncUdpSocket *udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    
    NSError *error = nil;
    [udpSocket enableBroadcast:YES error:&error];//允许广播 必须 否则后面无法发送组播和广播
    NSString *message = @"{\"type\":49}";
    //[udpSocket sendData:[message dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:10];//该函数只能用户已经连接的socket
    [udpSocket sendData:[message dataUsingEncoding:NSUTF8StringEncoding] toHost:@"192.168.1.102"   port:8400 withTimeout:-1 tag:10];//客户端socket发送组播或是广播 根据host的ip地址来订
    [udpSocket beginReceiving:nil];//必须要  开始准备接收数据
}


- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
      fromAddress:(NSData *)address
withFilterContext:(id)filterContext {
    NSLog(@"ReceiveData = %@, fromAddress = %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding],[[NSString alloc] initWithData:address encoding:NSUTF8StringEncoding]);
    NSString *host = nil;
    uint16_t port = 0;
    [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];//从此可以获取服务端回应的ip和端口 用于后面的tcp连接
     NSLog(@"Adress = %@ %i",host,port);
    
}

 

A手机建立udp监听,收到消息后回复:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self startReciveUdpBroadcast:udpSocket8400 port:8400];
    
    return YES;
}

- (void)startReciveUdpBroadcast:(GCDAsyncUdpSocket *)aUdpSocket port:(int)port
{
     if (aUdpSocket == nil)
    {
        aUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
        NSError *error = nil;
        
        if (![aUdpSocket bindToPort:port error:&error])
        {
            NSLog(@"udpSocket Error binding: %@", error);
            return;
        }
        if (![aUdpSocket beginReceiving:&error])
        {
            NSLog(@"udpSocket Error receiving: %@", error);
            return;
        }
        
        NSLog(@"start Receive Broadcast:%@============== ,%d",aUdpSocket,port);
        
        [aUdpSocket enableBroadcast:YES error:&error];

    }
   
    if(port == 8400)
    {
        udpSocket8400 = aUdpSocket;
    }
   }


#pragma mark- GCDAsyncUdpSocketDelegate
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
{      
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSLog(@"udp receive data 49 %@", msg);
        
        NSString *host = nil;
        uint16_t port = 0;
        [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];
        //可获取客户端socket的ip和端口,不过直接打印address是空的
        NSLog(@"Adress = %@ %i",host,port);


        //服务端回应客户端                   
        NSString *jsonString = @"回复的消息" ;   
        [sock sendData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] toAddress:address withTimeout:-1 tag:10];
        

    }
   
   
}

 

你可能感兴趣的:(UDP)