Jareddlc
Jareddlc

Reputation: 349

Android Emulator UDP cannot receive; works fine on phone

Hello I am trying to connect to a box that is on the network. It has a working UDP server on it. With the code below I am able to communicate with the box and send/receive UDP packets from my phone. However, I cannot figure out how to setup using the android emulator. I've read a lot on StackOverflow as well as other forums with not luck. I am 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 to the entire network; that is why Im using 255.255.255.255 but for android I read that it doesn't work and I opened settings in the Emulator and found my IP to the one 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????????????????????

Thanks in advance

UPDATE:

I currently got it so that my emulator can send a packet outside the host computer using a UDP forwarder NetworkActiv AUTAPF. It sends out my UDP but it doesn't not forward the response. Does anyone know where I should send the destination response to?

Upvotes: 4

Views: 6211

Answers (2)

danidis
danidis

Reputation: 81

I had the same problem, and I discovered that the problem was because of the android emulator redirections and a bug.

First of all, if you use an android emulator device with a level of API 25+ you will need to start the device without the "AndroidWifi" feature or you will not to able to communicate with your device because of a bug https://issuetracker.google.com/issues/37095198

For doing this:

cd "C:\Program Files (x86)\Android\android-sdk\emulator"
emulator.exe -avd <name+of+your+device> -feature -Wifi

After that you will need to redirige the desired ports of your localhost to your emulator device

telnet localhost 5554
auth <your+code+here>
redir add udp:<origin+port>:<destination+port>

Finally, you need to redirige the UDP packets from your local IP address (192.168.1.X) to yout localhost (127.0.0.1) or the emulated device will not receive the packet. For doing this you can create a simple UDP rediriger or use the program i created to do the same thing https://github.com/danidis91/Port-forward-UDP

Upvotes: 3

Mr.Me
Mr.Me

Reputation: 9276

There is No Way you can connect your android emulator to any thing physical on your network directly.

Android Emulator creates its own LAN and each emulator instance a new LAN instance with the same IPs (This is OK because they can't access each other)

If you insist on using the Emulator to communicate with the Box , you should create a UDP proxy socket on your PC (Using JAVA , C# , C++ , VB ... or any desktop programming language you want)

They way your Desktop App Will Work

  • get your LAN subnet to determain the packet source later.
  • Start a UDP Listener for the ports your server use.
  • Find the IP address of the sender (there are APIs for that) if the sender is the emulator, resend that packet to the box using the box IP

There is a good demonistration on the subject on Android's developers website.

Please take a look Here

Good Luck

Upvotes: 1

Related Questions