简体   繁体   中英

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? If I surround it in a try/catch block, nothing is caught so it's not an 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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