1
votes

I have a, to my mind odd, problem whereby I can send and receive UDP packets if I send them with a broadcast address (192.168.1.255). If I send with a unicast address (192.168.1.83) the packet does not get transmitted (I've checked with Wireshark).

The send function is:

    private void sendPacket(String data) {
        try {
            if (mSocket.isClosed()) {
                Log.e(TAG, "Socket is closed: " + data);
                return;
            }
            Log.d(TAG, "Send: " + data + " - " + myLocalIP.toString()+ " - " + myBcastIP.toString());
            DatagramPacket packet = new DatagramPacket(data.getBytes(),
                    data.length(), myLocalIP, BCAST_PORT);

            mSocket.send(packet);
        } catch (Exception e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

My manefest file has the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Given that broadcast works I can't think what I am doing wrong. Any ideas please?

1

1 Answers

2
votes

Have you tried on another ports? Maybe loop this method several times, UDP doesn't guarantee 100% delivery of packets

Are you using socket.setBroadcast(true) somwhere in your code, to send broadcast messages?