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