Android UDP emulator cannot receive; works great on phone
Hello, I am trying to connect to a box that is online. It has a working UDP server on it. With the below code, I can communicate with the field and send / receive UDP packets from my phone. However, I cannot figure out how to set up using the Android emulator. I've read a lot about StackOverflow as well as other forums but no luck. I'm on windows 8
Android code:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.os.Handler;
import android.util.Log;
public class udp implements Runnable {
// Private variable
private String IPServer = "10.0.2.2"; // For Emulator
//private String IPServer = "255.255.255.255"; // For Phone
private int portServer = 6286;
private int portDestin = 4381;
private InetAddress serverAddr;
private InetAddress localAddr;
private DatagramSocket socketSend;
private DatagramSocket socketList;
private DatagramPacket packetSend;
private DatagramPacket packetList;
private Handler uiHandler;
public udp(){
};
public void send() {
// Retrieve the server name
try {
Log.d("UDP", "Creating InetAddress");
serverAddr = InetAddress.getByName(IPServer);
} catch (Exception e) {
Log.e("UDP", "InetAddress Error:", e);
}
// Create UDP sockets
try {
Log.d("UDP", "Creating Sockets");
socketSend = new DatagramSocket(portServer);
socketList = new DatagramSocket(portDestin);
}
catch(Exception e) {
Log.e("UDP", "DatagramSocket Error:", e);
}
// Sets socket to broadcast
try {
Log.d("UDP", "SetBroadcast");
socketSend.setBroadcast(true);
}
catch(Exception e) {
Log.e("UDP", "SetBroadcast Error:", e);
}
// Create UDP packets
try {
Log.d("UDP", "Creating packets");
byte[] dataSend = new byte[32];
byte[] dataRead = new byte[32];
String msg = "Packet Broadcast";
dataSend = msg.getBytes();
packetSend = new DatagramPacket(dataSend, dataSend.length, serverAddr, portDestin);
packetList = new DatagramPacket(dataRead, dataRead.length, serverAddr, portDestin);
}
catch(Exception e) {
Log.e("UDP", "DatagramPacket Error:", e);
}
// Send packet
try {
Log.d("UDP", "Sending packet");
socketSend.send(packetSend);
}
catch(Exception e) {
Log.e("UDP", "Send Error:", e);
}
// Receive packet
Log.d("UDP", "Receiving packet");
for(int i=0; i<2; i++)
{
try {
socketList.receive(packetList);
}
catch(Exception e) {
Log.e("UDP", "Receive Error:", e);
}
String packetRec = new String(packetList.getData());
Log.d("UDP", "Received: "+packetRec);
}
socketSend.close();
socketList.close();
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
I need to broadcast the entire network; so i use 255.255.255.255 but for android i read that it doesn't work and i opened settings in emulator and found my ip address above.
I also port redir
telnet localhost 5554
redir add udp:4381:4381
log from phone:
02-11 12:01:09.743: D/UDP(17253): Creating InetAddress 02-11 12:01:09.743: D/UDP(17253): Creating Sockets 02-11 12:01:09.753: D/UDP(17253): SetBroadcast 02-11 12:01:09.753: D/UDP(17253): Creating packets 02-11 12:01:09.753: D/UDP(17253): Sending packet 02-11 12:01:09.753: D/UDP(17253): Receiving packet 02-11 12:01:09.753: D/UDP(17253): Received: Packet Broadcast???????????? 02-11 12:01:09.763: D/UDP(17253): Received: ??????PACKETSTUFF???????????fP*
log from android emulator:
02-11 20:00:22.742: D/UDP(1201): Creating InetAddress 02-11 20:00:22.742: D/UDP(1201): Creating Sockets 02-11 20:00:22.752: D/UDP(1201): SetBroadcast 02-11 20:00:22.772: D/UDP(1201): Creating packets 02-11 20:00:22.772: D/UDP(1201): Sending packet 02-11 20:00:22.772: D/UDP(1201): Receiving packet 02-11 20:00:22.772: D/UDP(1201): Received: Packet Broadcast????????????????????
Thank you in advance
UPDATE:
I currently got it so that my emulator can send the packet outside of the host machine using the UDP NetworkActiv AUTAPF forwarder. It is sending my UDP, but it is not sending a response. Does anyone know where I should send the destination response?
source to share
There is no way you can connect your Android emulator to any physical thing on your network.
Android Emulator creates its own LAN and each emulator instance is a new LAN instance with the same IP addresses (this is ok because they cannot access each other)
If you insist on using an emulator to communicate with the box, you must create a UDP proxy proxy on your computer (using JAVA, C #, C ++, VB ... or whatever programming language on the computer you want)
They will work on your desktop
- so that your local network subnet can later determine the source of the packet.
- Start a UDP listener for the ports used by the server.
- Find the IP address of the sender (there is an API for that), if the sender is an emulator, send this packet in the field using the IP field
There is a good demo on the Android Developers website.
Please take a look here
Luck
source to share