A firewall for Android with VpnService. Responses are delivered, but a SocketTimeoutException is thrown
Try adding the missing SocketTimeoutException exception handler
byte[] responseBuffer = new byte[RESPONSE_SIZE]; try { mDatagramSocket.send(mDatagramPacket); final DatagramPacket response = new DatagramPacket(responseBuffer, responseBuffer.length); mDatagramSocket.receive(response); } catch (SocketTimeoutException e) { // ignore ; // continue; } catch (IOException e) { Log.e("NoRootFwService", "error: " + Arrays.toString(responseBuffer)); // I can see the correct response here. logException(e); }
From recvfrom() call docs:
[EAGAIN] or [EWOULDBLOCK] The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no out-of-band data is available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to await out-of-band data.
simplified explanation
If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and the external variable errno set to EAGAIN
The DatagramSocket is not in blocking mode and seems like you are trying to read from the socket and there is no data to read, are you sure you are really receiving the data? try cleaning the buffer for each received packet.
The point was that I used a wrong IPv4 pseudo header to compute the checksum. It contained neither the packet UDP header nor the transmitted data. Since I included the UDP header and the data, I haven't seen the exception from the original question.