Android Emulator UDP 无法接收;在手机上工作正常 [英] Android Emulator UDP cannot receive; works fine on phone

查看:23
本文介绍了Android Emulator UDP 无法接收;在手机上工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试连接到网络上的盒子.它上面有一个工作的 UDP 服务器.使用下面的代码,我可以与盒子通信并从我的手机发送/接收 UDP 数据包.但是,我无法弄清楚如何使用 android 模拟器进行设置.我在 StackOverflow 以及其他不走运的论坛上阅读了很多内容.我在 Windows 8 上

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

安卓代码:

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
}   
}

我需要向全网广播;这就是为什么我使用 255.255.255.255 但对于 android 我读到它不起作用,我在模拟器中打开设置并找到我的 IP 到上面的那个.

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.

我也移植了redir

telnet localhost 5554
redir add udp:4381:4381

手机登录:

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????????????????????

提前致谢

更新:

我目前得到了它,以便我的模拟器可以使用 UDP 转发器 NetworkActiv AUTAPF 在主机外部发送数据包.它发出我的 UDP,但它不转发响应.有谁知道我应该将目标响应发送到哪里?

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?

推荐答案

你无法将你的安卓模拟器直接连接到网络上的任何物理设备.

Android Emulator 创建自己的LAN,并且每个模拟器实例都有一个具有相同 IP 的新 LAN 实例(这没关系,因为它们不能互相访问)

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)

如果您坚持使用 Emulator 与 Box 通信,您应该在您的 PC 上创建一个 UDP 代理套接字(使用 JAVA 、 C# 、 C++ 、 VB ... 或任何您想要的桌面编程语言)

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

  • 稍后获取您的 LAN 子网以确定数据包来源.
  • 为您的服务器使用的端口启动一个 UDP 侦听器.
  • 如果发件人是模拟器,则查找发件人的 IP 地址(有相应的 API),使用盒子 IP 将该数据包重新发送到盒子

在 Android 的开发者网站上有一个很好的演示.

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

请看这里

祝你好运

这篇关于Android Emulator UDP 无法接收;在手机上工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆