A network UDP broadcast, such as a mobile phone, to reply to a UDP message after receiving a broadcast

Source: Internet
Author: User

B Mobile: Send a message to a mobile phone, waiting for a reply


-(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];//allow broadcast must otherwise not be sent after multicast and broadcast
NSString *message = @ "{\" type\ ": 49}";
[Udpsocket senddata:[message datausingencoding:nsutf8stringencoding] withtimeout:-1 tag:10];//The function can only be connected to a socket by the user
[Udpsocket senddata:[message datausingencoding:nsutf8stringencoding] tohost:@ "192.168.1.102" port:8400 withTimeout: -1 tag:10];//Client socket send multicast or broadcast according to host's IP address to order
[Udpsocket beginreceiving:nil];//must start preparing to receive data
}


-(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];//The IP and port from which the service-side response can be obtained for subsequent TCP connections
NSLog (@ "Adress =%@%i", host,port);

}

A mobile phone set up UDP monitoring, received the message after the reply:

-(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%@", msg);
        
        nsstring *host = nil;
        uint16_t port = 0;
        [Gcdasyncudpsocket gethost:&host port:&port fromaddress: Address];
       //can obtain the IP and port of the client socket, but the direct print address is empty
         NSLog (@ "Adress =%@%i", host,port);


Server-side Response client
NSString *jsonstring = @ "Reply message";
[Sock senddata:[jsonstring datausingencoding:nsutf8stringencoding] toaddress:address withTimeout:-1 tag:10];


}


}

A network UDP broadcast, such as a mobile phone, to reply to a UDP message after receiving a broadcast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.