简体   繁体   English

Android DatagramSocket无法正常工作

[英]Android DatagramSocket not working correctly

public void Connect() throws Exception
{
 InetAddress dest;
 dest = InetAddress.getByName("192.168.1.100");
 DatagramSocket socket = new DatagramSocket();
 socket.connect(new InetSocketAddress(15900));
 byte[] message = "Oh Hai!".getBytes();
 DatagramPacket packet = new DatagramPacket(message, message.length,dest,15900);
 socket.send(packet);
}

Using the above code on Android SDK 1.5, when attaching the debugger to the android emulation, I step through the above sample (obtained from a tutorial), and the debugger returns control to the user when it reaches the DatagramSocket line... as soon as I hit F8 (Eclipse Galileo) to continue, I immediately have control again.. basically it never reaches socket.connect.... What is going wrong here? 在Android SDK 1.5上使用上述代码,将调试器附加到android仿真时,我逐步完成了上述示例(从教程中获得),并且调试器在到达DatagramSocket行时将控制权返回给用户...当我按F8键(Eclipse Galileo)继续时,我立即又有了控制权。基本上,它再也达不到socket.connect了。 If I surround it in a try/catch block, nothing is caught so it's not an exception. 如果我将它放在try / catch块中,则不会捕获任何东西,因此也不例外。 Why is it dying like this? 为什么会这样死呢?

Thanks! 谢谢!

Ah, Socket Permission Error... nevermind! 啊,套接字权限错误...没关系!

For the people wondering how I solved it: The manifest needs to have a uses-permission added, permission being internet. 对于那些想知道我如何解决它的人们:清单需要添加使用许可,许可是Internet。

Add the following to the manifest: 将以下内容添加到清单中:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM