Android Emulator UDP cannot receive; works fine on

2019-02-25 01:19发布

问题:

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?

回答1:

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



标签: android udp