diff --git a/android/src/main/java/com/tradle/react/UdpReceiverTask.java b/android/src/main/java/com/tradle/react/UdpReceiverTask.java index 16254a0b..22a7e5b7 100644 --- a/android/src/main/java/com/tradle/react/UdpReceiverTask.java +++ b/android/src/main/java/com/tradle/react/UdpReceiverTask.java @@ -48,6 +48,8 @@ public void run() { final String base64Data = Base64.encodeToString(packet.getData(), packet.getOffset(), packet.getLength(), Base64.NO_WRAP); receiverListener.didReceiveData(base64Data, address.getHostAddress(), packet.getPort()); + } catch(java.net.SocketTimeoutException timeout) { // receive timed-out, needed to get out of the loop when socket gets closed + continue; } catch (IOException ioe) { if (receiverListener != null) { receiverListener.didReceiveError(ioe.getMessage()); diff --git a/android/src/main/java/com/tradle/react/UdpSocketClient.java b/android/src/main/java/com/tradle/react/UdpSocketClient.java index 79946577..214689ea 100644 --- a/android/src/main/java/com/tradle/react/UdpSocketClient.java +++ b/android/src/main/java/com/tradle/react/UdpSocketClient.java @@ -75,6 +75,7 @@ public void bind(Integer port, @Nullable String address) throws IOException { mSocket = new MulticastSocket(socketAddress); mSocket.setReuseAddress(true); + mSocket.setSoTimeout(500); // begin listening for data in the background mReceiverTask = new UdpReceiverTask(mSocket, this);