Import First: AsyncUdpSocket.h, ASYNCUDPSOCKET.M
Add <AsyncUdpSocketDelegate> Agreements
Created: @property (nonatomic, strong) Asyncudpsocket *myudpsocket;
-(void) Viewdidload {
[Super Viewdidload];
Self.myudpsocket = [[Asyncudpsocket alloc]initwithdelegate:self];
Binding port
[Self.myudpsocket bindtoport:9000 Error:nil];
Turn on the radio
[Self.myudpsocket Enablebroadcast:yes Error:nil];
Start receiving data
[Self.myudpsocket receivewithtimeout:-1 tag:0];
}
-(BOOL) Onudpsocket: (Asyncudpsocket *) sock didreceivedata: (NSData *) data withtag: (long) tag fromhost: (NSString *) host Port: (UInt16) port{
if (![ Host hasprefix:@ ":"]) {
NSString *string = [[NSString alloc]initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "%@:%@", host,string);
}
[Self.myudpsocket receivewithtimeout:-1 tag:0];
return YES;
}
-(Ibaction) clicked: (ID) Sender {
[Self.myudpsocket senddata:[@ "hahaha" datausingencoding:nsutf8stringencoding] tohost:@ "255.255.255.255" port:9000 Withtimeout:-1 tag:0];
}
Lan Yi Education UDP Socket