开发者

Android DatagramSocket not working correctly

开发者 https://www.devze.com 2023-01-09 18:27 出处:网络
public void Connect() throws Exception { InetAddress dest; dest = InetAddress.getByName(\"192.168.1.100\");
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? If I surround it in a try/catch block, nothing is caught so it's not an 开发者_StackOverflow中文版exception. 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.

Add the following to the manifest:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号