模拟器未从本地主机收到UDP数据包 [英] UDP packets not received in emulator from localhost

查看:168
本文介绍了模拟器未从本地主机收到UDP数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模拟器中运行时,我的应用程序无法接收UDP数据包. UDP数据包通过以下Java程序在端口49999上的"localhost"上发送.

My app is unable to receive the UDP packets when running in the emulator. UDP packets are sent by below java program on "localhost" over the port 49999.

    DatagramSocket clientsocket;
        DatagramPacket dp;
        BufferedReader br;
        InetAddress ia;
        byte buf[] = new byte[1024];
        int cport = 50000, sport = 49999;

        clientsocket = new DatagramSocket(cport);
        dp = new DatagramPacket(buf, buf.length);
        br = new BufferedReader(new InputStreamReader(System.in));
        ia = InetAddress.getLocalHost();

        while(true)
        {
            Random rand = new Random();
            String str1 = rand.nextInt(100) + "";
            buf = str1.getBytes();

            System.out.println("Sending " + str1);
            clientsocket.send(new DatagramPacket(buf,str1.length(), ia, sport));

            try{
                Thread.sleep(100);
            } catch(Exception e){}
        }

在同一本地主机上运行的另一个Java UDP服务器程序可以很好地接收数据包.这意味着数据包已正确发送到localhost:49999.

Another java UDP server program running on the same localhost receives the packets fine. This means that the packets are sent to localhost:49999 correctly.

要将数据包从localhost转发到仿真器,我进行了telnet重定向,如下所示:

To forward the packets from localhost to the emulator, I did telnet redirect as below:

telnet localhost 49999
redir add udp:49999:49999

应用程序中的UDP接收器如下所示:

The UDP receiver in the app looks like this:

byte[] data = new byte[1400];
DatagramPacket packet = new DatagramPacket(data, 1400);
DatagramSocket socket = new DatagramSocket(49999);
socket.setSoTimeout(200);
try{
socket.receive(packet);  ---->> This throws a SocketTimeoutException
} catch(SocketTimeoutException e){}

我的理解是,telnet重定向应该负责将数据包从开发机器的localhost:49999转发到仿真器的localhost:49999,以便数据在DatagramSocket(49999)上可用.但是,它一直一直抛出SocketTimeoutException.

My understanding was that the telnet redirect should take care of forwarding the packets from my development machine's localhost:49999 to emulator's localhost:49999 so that the data is available on the DatagramSocket(49999). However it keeps throwing the SocketTimeoutException all the time.

知道这里谜题缺少的内容将对您有很大的帮助.

It would be a great help to know what is the missing piece of the puzzle here.

推荐答案

连接到本地主机后,您可能需要检查cmd中的命令netstat -na是否确实按预期分配了端口.也可以尝试使用IP 127.0.0.1.

After connecting to the localhost you may want to check that the port was actually asigned as intended with the command netstat -na in the cmd. It also might be worth a try to use the IP 127.0.0.1 instead.

这篇关于模拟器未从本地主机收到UDP数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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