Here is my code:
Code Block Objective-C #import <arpa/inet.h> #import <fcntl.h> #import <ifaddrs.h> #import <netdb.h> #import <net/if.h> #import <sys/socket.h> #import <sys/types.h> - (void)sendUDPBroadcast { int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { NSLog(@"socket create error"); return; } if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { NSLog(@"socket set Nonblock error"); goto CLEAN; } int reuseaddr = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) < 0) { NSLog(@"socket set SO_REUSEADDR error"); goto CLEAN; } int nosigpipe = 1; if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe)) < 0) { NSLog(@"socket set SO_NOSIGPIPE error"); goto CLEAN; } int bBroadcast = 1; if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bBroadcast, sizeof(bBroadcast)) < 0) { NSLog(@"socket set broadcast error"); goto CLEAN; } char buffer[] = "Hello world"; struct sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr("255.255.255.255"); sa.sin_port = htons(50000); int res = sendto(sock, buffer, strlen(buffer), 0, &sa, sizeof(sa)); if (res < 0) { NSLog(@"send error:%d msg:%s", errno, strerror(errno)); goto CLEAN; } CLEAN: close(sock); }
When I run on an iPhone with iOS 14.5 Beta installed, I get the following error:
Any help is appreciated. Thanks in advance.Line55: send error:65 msg:No route to host