I have an issue with broadcasting Datagram packets from one Android phone to the next.
I have set up my application to be able to use 2 different Datagram Sockets. This all works well, and I am able to switch between the Sockets just fine.
I use 2 phones to test the application, as well as a PC based application that is able to communicate with my application. When I try to broadcast Datagram packets from 1 of the 2 phones, the PC application reacts just fine, but the other phone doesn’t respond at all. The same happens when I try it with the other phone.
But here’s the catch: whenever I try to broadcast from the PC based application, both phones respond. (???)
Both devices and the PC application are set to use the same broadcast address for sending. Yet it doesn’t seem that the phone’s accept the broadcast of the other. I have confirmed that they do get a broadcast response from themselves when they are broadcasting, which is correct obviously.
The methods I use to initialise and update the broadcast socket is stated below.
private void initBroadcastSocket(Inet4Address address, int port){
try {
mBroadcastSocket = new DatagramSocket(port, address);
mBroadcastSocket.setBroadcast(true);
mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
} catch (IOException ioe) {
Log.e(TAG, "Exception occurred while initializing BroadcastSocket: " + ioe.toString());
}
if(mBroadcastSocket != null){
Log.d(TAG, "BroadcastSocket initially set to " + mBroadcastSocket.getLocalAddress() +
":" + mBroadcastSocket.getLocalPort());
}
}
public synchronized void updateBroadcastSocket(Inet4Address address, int port){
// Temporarily suspend the listening Thread.
...
// If the socket is open, close it.
if(mBroadcastSocket != null){
mBroadcastSocket.close();
mBroadcastSocket = null;
}
// Create new socket with the passed values.
try {
mBroadcastSocket = new DatagramSocket(port, address);
mBroadcastSocket.setBroadcast(true);
mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
} catch (SocketException se) {
Log.e(TAG, "Exception occured while updating BroadcastSocket: " + se.toString());
}
// Log new address and port.
...
// Continue the listening Thread.
...
}
If anyone finds a flaw in my code, please elaborate.
Issue is both Android devices have the same IP-Address (somehow). I am trying to fix this now.