这几天在做wifi的智能灯,需要用到组播的知识就把这个记录一下,服务端使用java写的,代码如下:

 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.net.InetAddress;
 5 import java.net.MulticastSocket;
 6 
 7  class UDPServer{
 8     public static void main(String[] args)throws IOException{
 9         MulticastSocket  server = new MulticastSocket(5050);
10         InetAddress address = InetAddress.getByName("224.0.0.1");
11         server.joinGroup(address);
12         
13         
14         byte[] recvBuf = new byte[100];
15         DatagramPacket recvPacket 
16             = new DatagramPacket(recvBuf , recvBuf.length);
17         
18         while(true)
19         {
20         
21         server.receive(recvPacket);
22         String recvStr = new String(recvPacket.getData() , 0 , recvPacket.getLength());
23         System.out.println("Hello World!" + recvStr);
24         int port = recvPacket.getPort();
25         InetAddress addr = InetAddress.getByName("224.0.0.1");
26         String sendStr = "Hello ! I'm Server";
27         byte[] sendBuf;
28         sendBuf = sendStr.getBytes();
29         DatagramPacket sendPacket 
30             = new DatagramPacket(sendBuf , sendBuf.length , addr , port );
31         server.send(sendPacket);
32         }
33      
34         
35         
36     }
37     
38 }

ios端的主要代码:

 1 #import "UdpHelper.h"
 2 
 3 @implementation UdpHelper {
 4     AsyncUdpSocket* m_udpSocket;
 5 }
 6 
 7 + (UdpHelper*)getinstance
 8 {
 9 
10     static UdpHelper* udpHelper = nil;
11 
12     static dispatch_once_t onceToken;
13     dispatch_once(&onceToken, ^{
14             udpHelper = [[self alloc] init];
15         
16         [udpHelper openUDPServer];
17     });
18 
19     return udpHelper;
20 }
21 
22 - (void)openUDPServer
23 {
24     AsyncUdpSocket* tempSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
25     m_udpSocket = tempSocket;
26 
27     NSError* error = nil;
28     [m_udpSocket bindToPort:5051 error:&error];
29     [m_udpSocket joinMulticastGroup:@"224.0.0.1" error:&error];
30 
31     [m_udpSocket receiveWithTimeout:-1 tag:0];
32 }
33 
34 - (void)sendMessage:(NSString*)message
35 {
36     NSMutableString* sendString = [NSMutableString stringWithCapacity:100];
37     [sendString appendString:message];
38 
39     BOOL res = [m_udpSocket sendData:[sendString dataUsingEncoding:NSUTF8StringEncoding]
40                               toHost:@"224.0.0.1"
41                                 port:5050
42                          withTimeout:-1
43                                  tag:0];
44 
45     NSLog(@"%@", sendString);
46     if (res) {
47         NSLog(@"xxxxffff");
48     }
49     else {
50         NSLog(@"error");
51     }
52 }
53 
54 - (BOOL)onUdpSocket:(AsyncUdpSocket*)sock didReceiveData:(NSData*)data withTag:(long)tag fromHost:(NSString*)host port:(UInt16)port
55 {
56     NSLog(@"onUdpSocket successful");
57 
58     NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
59 
60     NSLog(@"%@", str);
61 
62     return YES;
63 }
64 
65 - (void)onUdpSocket:(AsyncUdpSocket*)sock didNotSendDataWithTag:(long)tag dueToError:(NSError*)error
66 {
67     NSLog(@"error1");
68 }
69 
70 - (void)onUdpSocket:(AsyncUdpSocket*)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError*)error
71 {
72     NSLog(@"error2");
73 }
74 
75 @end

以上的代码就可以实现两端互发消息通信,代码也主要是借鉴了一些别人的