Android never receives UDP packet(Android 从不接收 UDP 数据包)
问题描述
以下代码导致超时.
它在非 Android Java 上运行良好.怎么了?
It works fine on non-Android Java. What's the matter?
//@Override
public static void run()
{
//System.out.println ( "Local Machine IP : "+addrStr.toString ( ) ) ;
HelloWorldActivity.tv.setText("Trace 1");
try
{
// Retrieve the ServerName
InetAddress serverAddr; //= InetAddress.getByName(Server.SERVERIP);
InetAddress ias[] = InetAddress.getAllByName(Server.SERVERNAME);
serverAddr = ias[0];
Log.d("UDP", "C: Connecting...");
/* Create new UDP-Socket */
DatagramSocket socket = new DatagramSocket();
/* Prepare some data to be sent. */
String strQuery="ÿÿÿÿgetservers"+" "+Server.iProtocol+" "+"'all'";
Log.d("UDP", strQuery);
//byte[] buf = ("ÿÿÿÿgetservers 68 'all'").getBytes();
byte[] buf = strQuery.getBytes();
/* Create UDP-packet with
* data & destination(url+port) */
DatagramPacket packet = new DatagramPacket(buf, buf.length,
serverAddr, Server.SERVERPORT);
Log.d("UDP", "C: Sending: '" + new String(buf) + "'");
/* Send out the packet */
socket.setSoTimeout(5000);
socket.send(packet);
Log.d("UDP", "C: Sent.");
Log.d("UDP", "C: Done.");
// http://code.google.com/p/android/issues/detail?id=2917
byte[] buffer= new byte[1024*100];
DatagramPacket receivePacket
= new DatagramPacket(buffer,
buffer.length); //, serverAddr, Server.SERVERPORT);
socket.receive(receivePacket);
HelloWorldActivity.tv.setText("TTT");
String x = new String(receivePacket.getData());
Log.d("UDP", "C: Received: '" + x + "'");
HelloWorldActivity.tv.setText(x);
} catch (Exception e) {
HelloWorldActivity.tv.setText(e.getMessage());
Log.e("UDP", "C: Error", e);
}
}
public class Server
{
/*
//public static java.lang.string SERVERIP;
public static String SERVERNAME = "monster.idsoftware.com";
public static String SERVERIP = "192.246.40.56";
public static int SERVERPORT = 27950;
public static int PROTOCOL = 68;
*/
//public static String SERVERNAME="monster.idsoftware.com";
public static String SERVERNAME="dpmaster.deathmask.net";
public static String SERVERIP="192.246.40.56";
public static int SERVERPORT=27950;
//public static int iProtocol= 68; // Quake3
public static int iProtocol=71; // OpenArena
}
Android 清单:
Android manifest:
<?xml version="1.0" encoding="utf-8"?>
<use-permission id="android.permission.READ_CONTACTS" />
<use-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_CELL_ID" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="@drawable/icon"
android:label="AAA New Application"
>
<activity android:name="HelloWorldActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
推荐答案
你是在模拟器上测试还是在手机上测试?如果您使用的是模拟器,则需要了解 模拟器上的网络是如何工作的.最具体的:
Are you testing this on the emulator or on an actual phone? If you're using an emulator you need to be aware of how networking on the emulator works. Most specifically:
模拟器的每个实例都在虚拟路由器/防火墙服务后面运行,该服务将其与您的开发机器的网络接口和设置以及互联网隔离开来.仿真设备看不到您的开发机器或网络上的其他仿真器实例.相反,它只看到它通过以太网连接到路由器/防火墙.
Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.
您可能需要设置端口转发,使用模拟器控制台或使用adb
命令.
You'll probably need to set up port forwarding, either using the Emulator console or using the adb
command.
这篇关于Android 从不接收 UDP 数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 从不接收 UDP 数据包
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01