我是刚来的,没有c币,求好心人帮忙解答,谢谢谢谢!!!!
package com.example.practice;
import java.net.DatagramSocket;
import java.net.SocketException;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new kaishi().start();
}
class kaishi extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("01");
try {
System.out.println("03");
DatagramSocket socket =new DatagramSocket(50000); //异常,原因 套接字不能被打开
System.out.println("05");
} catch (SocketException e) {
System.out.println("02");
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("04");
}
}
}
··················································································
以上是一个android软件一个代码,目的是用UDP协议进行手机之间的通信,代码内容仅仅就是创建一个DatagramSocket对象。问题是:每次程序进行到创建DatagramSocket对象的时候都会抛出异常,用真机测试输出结果是:01 03 02 04。这是在Android的工程中写的,在java工程中就可以成功创建,语法是按照Java的API写的,但每次都抛出异常,网上说是因为端口号被占用才抛出异常,换了好几个端口号还是会抛出异常。求可以成功创建DatagramSocket对象的方法.