iOS UDP multicast

Source: Internet
Author: User

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

Related Article

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.