Solution to iOS 14.5 UDP broadcast Sendto returns -1

Solution to iOS 14.5 UDP broadcast Sendto returns -1

[[400268]]

This article is reprinted from the WeChat public account "Wangluo Development", written by Zhan Fei. Please contact the WeChat public account "Wangluo Development" to reprint this article.

1. Problem Background

  1. After the mobile phone system is upgraded to iOS 14.5, UDP broadcast sending fails
  2. The old version of the project uses socket
  3. The new version of the project uses CocoaAsyncSocket
  4. Both UDP packet sending methods will report an error No route to host

The specific contents of the error are as follows:

  1. sendto: -1
  2. client: sendto fail, but just ignore it
  3. : No route to host

2. Problem Analysis

2.1 Troubleshooting sendto returns -1

We know that sending a broadcast sendto returns -1, and normally the sendto return value is greater than 0.

First determine whether the socket connection is established

  1. self._sck_fd4 = socket(AF_INET,SOCK_DGRAM,0);
  2. if (DEBUG_ON) {
  3. NSLog(@ "client init() _sck_fd4=%d" ,self._sck_fd4);
  4. }

self._sck_fd4 prints:

  1. server init(): _sck_fd4=12

The socket connection is normal, and then determine the data packet

  1. sendto(self._sck_fd4, bytes, dataLen, 0, (struct sockaddr*)&target_addr, addr_len) = -1

Data sending failed

2.2 Add NSLocalNetworkUsageDescription permission

  1. Info.plist Add NSLocalNetworkUsageDescription
  2. Send a UDP broadcast to trigger a permission pop-up window, allowing the user to click OK to allow access to the local network.

Found that the problem still exists

2.3 Troubleshooting unicast messages

Since the hostName set for sending broadcast in the project is 255.255.255.255, in order to troubleshoot, we decided to send unicast first to see if it can succeed.

After changing the unicast address to 192.168.0.101, it was found that it could be sent successfully. Then, sending unicast in the new version of CocoaAsyncSocket library was also successful.

It is recommended to use 192.168.0.255 for UDP broadcast. After changing the broadcast address, the problem is solved and the device can receive UDP broadcast data.

3. Problem Solving

Since the 192.168.0.255 broadcast address is only the current local address, the first three segments of the 192.168.0 local address need to be changed dynamically in the App. The solution is as follows:

  1. NSString *localInetAddr4 = [ESP_NetUtil getLocalIPv4];
  2. NSArray *arr = [localInetAddr4 componentsSeparatedByString:@ "." ];
  3. NSString *deviceAddress4 = [NSString stringWithFormat:@ "%@.%@.%@.255" ,arr[0], arr[1], arr[2]];

For packet filtering, you only need to filter whether the last segment of the address is 255.

  1. bool isBroadcast = [targetHostName hasSuffix:@ "255" ];

<<:  Hongmeng OS, Android, IOS, which one is the future?

>>:  Android 12 is officially released: the biggest design change in Android history, smoother!

Recommend

Tools and promotion channels used in a complete event planning

"What tools are needed for a complete event?...

What is the difference between WeChat Phonebook and VoLTE?

WeChat Phonebook is now online! How big of an imp...

Windows licensing fees may drop to $42 in 2020

Microsoft recently released a free version of its ...

Top ten keywords for new media in 2018!

The year 2018 has passed by in a flash. The prosp...

How much does it cost to make a small program in Guangzhou?

It is estimated that many people do not know much...

Earthworms are good at splitting in two? My sea anemone can split in a hundred

As summer comes to an end, the best time of the y...