RazzleFnDazzle
Well-Known Member
I am running code on a real android device connected to wifi. I send out data on a multicast socket and wait for a response. I've tried it several different ways now and the phone can never receive a response. I tried opening a ServerSocket and manually sending data and that didn't work. I tried doing the same with a DatagramSocket and that didn't work. I tried to receive a response after sending data out on the MulticastSocket and that didn't work. It seems that I can't use my phone to receive data at all. I can send everything just fine, but can't receive. I can send multicasts and I can confirm the receiver is getting all the right ip addresses and stuff before responding. Here is a sample of the code:
phone snippet:
computer snippet:
I do have these permissions
phone snippet:
Code:
MulticastSocket socket = null;
WifiManager wifi = (WifiManager)gContext.getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = wifi.createMulticastLock("ptclock");
try {
lock.acquire();
byte[] data = "hello".getBytes();
InetAddress address = InetAddress.getByName(IP_ADDRESS);
socket = new MulticastSocket();
socket.joinGroup(address);
socket.setSoTimeout(5000);
while (true) {
try {
Thread.sleep(1000);
DatagramPacket packet = new DatagramPacket(data, 1, address, PORT);
socket.send(packet);
data = new byte[1024];
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
//Never reaches this point due to timeout
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (lock != null)
lock.release();
if (socket != null)
socket.close();
}
}
Code:
MulticastSocket socket = null;
try {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
InetAddress address = InetAddress.getByName(IP_ADDRESS);
socket = new MulticastSocket(PORT);
socket.joinGroup(address);
while (true) {
try {
socket.receive(packet);
//handles all the data correctly
InetAddress retAddress = packet.getAddress();
int retPort = packet.getPort();
data = "goodbye".getBytes();
packet = new DatagramPacket(data, data.length, retAddress, retPort);
socket.send(packet);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (socket != null)
socket.close();
}
Code:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>