iOS oc 组播
时间: 2023-08-15 16:08:36 浏览: 143
iOS OC 加载动图(gif)
在 iOS 中,可以使用 MulticastSocket 实现组播功能。下面是一个基本的示例代码:
```objective-c
#import <UIKit/UIKit.h>
#import <arpa/inet.h>
#import <sys/socket.h>
@interface MulticastSocket : NSObject
@property (nonatomic, assign) int socketDescriptor;
@property (nonatomic, assign) struct sockaddr_in multicastAddr;
- (BOOL)joinMulticastGroup:(NSString *)multicastGroup onInterface:(NSString *)networkInterface;
- (BOOL)leaveMulticastGroup:(NSString *)multicastGroup onInterface:(NSString *)networkInterface;
- (BOOL)sendData:(NSData *)data toMulticastGroup:(NSString *)multicastGroup onPort:(int)port;
@end
@implementation MulticastSocket
- (BOOL)joinMulticastGroup:(NSString *)multicastGroup onInterface:(NSString *)networkInterface {
// 创建 socket
self.socketDescriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (self.socketDescriptor < 0) {
NSLog(@"Failed to create socket");
return NO;
}
// 设置 socket 选项
int reuse = 1;
if (setsockopt(self.socketDescriptor, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
NSLog(@"Failed to set socket options");
return NO;
}
// 绑定 socket
struct sockaddr_in localAddr;
memset((char *)&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(0);
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(self.socketDescriptor, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) {
NSLog(@"Failed to bind socket");
return NO;
}
// 设置组播地址
memset((char *)&self.multicastAddr, 0, sizeof(self.multicastAddr));
self.multicastAddr.sin_family = AF_INET;
self.multicastAddr.sin_port = htons(12345);
if (inet_aton([multicastGroup UTF8String], &self.multicastAddr.sin_addr) == 0) {
NSLog(@"Invalid multicast address");
return NO;
}
// 加入组播组
struct ip_mreq mreq;
memset((char *)&mreq, 0, sizeof(mreq));
mreq.imr_multiaddr.s_addr = self.multicastAddr.sin_addr.s_addr;
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(self.socketDescriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) {
NSLog(@"Failed to join multicast group");
return NO;
}
return YES;
}
- (BOOL)leaveMulticastGroup:(NSString *)multicastGroup onInterface:(NSString *)networkInterface {
// 离开组播组
struct ip_mreq mreq;
memset((char *)&mreq, 0, sizeof(mreq));
mreq.imr_multiaddr.s_addr = self.multicastAddr.sin_addr.s_addr;
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(self.socketDescriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) {
NSLog(@"Failed to leave multicast group");
return NO;
}
// 关闭 socket
if (close(self.socketDescriptor) < 0) {
NSLog(@"Failed to close socket");
return NO;
}
return YES;
}
- (BOOL)sendData:(NSData *)data toMulticastGroup:(NSString *)multicastGroup onPort:(int)port {
// 发送数据到组播组
struct sockaddr_in destAddr;
memset((char *)&destAddr, 0, sizeof(destAddr));
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(port);
if (inet_aton([multicastGroup UTF8String], &destAddr.sin_addr) == 0) {
NSLog(@"Invalid multicast address");
return NO;
}
if (sendto(self.socketDescriptor, [data bytes], [data length], 0, (struct sockaddr *)&destAddr, sizeof(destAddr)) < 0) {
NSLog(@"Failed to send data");
return NO;
}
return YES;
}
@end
```
使用示例:
```objective-c
// 创建组播 socket
MulticastSocket *socket = [[MulticastSocket alloc] init];
if (![socket joinMulticastGroup:@"224.0.0.1" onInterface:nil]) {
NSLog(@"Failed to join multicast group");
return;
}
// 发送数据到组播组
NSString *message = @"Hello, multicast!";
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
if (![socket sendData:data toMulticastGroup:@"224.0.0.1" onPort:12345]) {
NSLog(@"Failed to send data");
}
// 离开组播组
if (![socket leaveMulticastGroup:@"224.0.0.1" onInterface:nil]) {
NSLog(@"Failed to leave multicast group");
}
```
阅读全文