These days in doing WiFi smart lights, need to use the knowledge of multicast to this record, the server is written in Java, the code is as follows:
1 Importjava.io.IOException;2 ImportJava.net.DatagramPacket;3 ImportJava.net.DatagramSocket;4 Importjava.net.InetAddress;5 ImportJava.net.MulticastSocket;6 7 classudpserver{8 Public Static voidMain (string[] args)throwsioexception{9MulticastSocket Server =NewMulticastSocket (5050);TenInetAddress address = Inetaddress.getbyname ("224.0.0.1"); One Server.joingroup (address); A - - byte[] Recvbuf =New byte[100]; the Datagrampacket Recvpacket -=NewDatagrampacket (Recvbuf, recvbuf.length); - - while(true) + { - + server.receive (recvpacket); AString Recvstr =NewString (Recvpacket.getdata (), 0, Recvpacket.getlength ()); atSystem.out.println ("Hello world!" +recvstr); - intPort =Recvpacket.getport (); -InetAddress addr = Inetaddress.getbyname ("224.0.0.1"); -String sendstr = "Hello!" I ' m Server "; - byte[] sendbuf; -SendBuf =sendstr.getbytes (); in Datagrampacket Sendpacket -=NewDatagrampacket (SendBuf, sendbuf.length, addr, port); to server.send (sendpacket); + } - the * $ }Panax Notoginseng -}
The main code on the iOS side:
1 #import "UdpHelper.h"2 3 @implementationUdphelper {4asyncudpsocket*M_udpsocket;5 }6 7+ (udphelper*) getinstance8 {9 Ten Staticudphelper* Udphelper =Nil; One A Staticdispatch_once_t Oncetoken; -Dispatch_once (&oncetoken, ^{ -Udphelper =[[Self alloc] init]; the - [Udphelper openudpserver]; - }); - + returnUdphelper; - } + A- (void) Openudpserver at { -asyncudpsocket* Tempsocket =[[Asyncudpsocket alloc] initwithdelegate:self]; -M_udpsocket =Tempsocket; - -nserror* error =Nil; -[M_udpsocket Bindtoport:5051error:&ERROR]; in[M_udpsocket Joinmulticastgroup:@"224.0.0.1"error:&ERROR]; - to[M_udpsocket receivewithtimeout:-1Tag0]; + } - the- (void) SendMessage: (nsstring*) Message * { $nsmutablestring* sendstring = [nsmutablestring stringwithcapacity: -];Panax Notoginseng [sendstring appendstring:message]; - theBOOL res =[M_udpsocket senddata:[sendstring datausingencoding:nsutf8stringencoding] +Tohost:@"224.0.0.1" APort5050 thewithtimeout:-1 +Tag0]; - $NSLog (@"%@", sendstring); $ if(res) { -NSLog (@"xxxxffff"); - } the Else { -NSLog (@"Error");Wuyi } the } - Wu-(BOOL) Onudpsocket: (asyncudpsocket*) sock didreceivedata: (nsdata*) Data withtag: (Long) tag fromhost: (nsstring*) host port: (UInt16) Port - { AboutNSLog (@"Onudpsocket Successful"); $ -nsstring* str =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; - -NSLog (@"%@", str); A + returnYES; the } - $- (void) Onudpsocket: (asyncudpsocket*) sock Didnotsenddatawithtag: (Long) tag Duetoerror: (nserror*) Error the { theNSLog (@"Error1"); the } the -- (void) Onudpsocket: (asyncudpsocket*) sock Didnotreceivedatawithtag: (Long) tag Duetoerror: (nserror*) Error in { theNSLog (@"Error2"); the } About the @end
The above code can be implemented on both ends of the message communication, the code is mainly reference to some other people's
iOS UDP multicast