绑定失败:EADDRINUSE(地址已在使用中)

时间:2015-04-22 12:52:15

标签: android sockets android-activity port powerpoint

您好我正在制作powerpoint远程Android项目。

但是,当我开始项目它显示问题"绑定失败:EADDRINUSE(地址已在使用中)" 我在谷歌搜索,搜索,并说这个问题来自PORT号码。但我把它改成了另一个数字,如11000,8000或21000 ....它仍然无效 我的代码在这里:

public class BroadcastReceiver extends Thread {
/** Our port is 34012, this can later be changed in the Settings */
private final int PORT = 21000;
/**
 * Reference to MainActivity to avoid unnecessary Listener interfaces (for
 * this small project).
 */
private MainActivity runningOn;

/**
 * The Constructor
 * @param c
 *      Reference to the MainActivity.
 */
public BroadcastReceiver(MainActivity c) {
    this.runningOn = c;
}

/**
 * This method starts a cycle, that is broken, when we receive a
 * UDP-Broadcastmessage from a PowerPoint Remote server.
 */
public void run() {
    String msg = null;
    DatagramPacket packet = null;
    try {

        DatagramSocket dsocket = new DatagramSocket(PORT);
        byte[] buffer = new byte[2048];

        packet = new DatagramPacket(buffer, buffer.length);
        dsocket.setBroadcast(true);         
        while (true) {
            Log.i("BroadcastReceiver", "Starting BroadcastReceiving.");
            dsocket.receive(packet);
            Log.i("BroadcastReceiver", "Ended receving packets");
            msg = new String(buffer, 0, packet.getLength());
            System.out.println(packet.getAddress().getHostName() + ": "
                    + msg);
            System.out.println("Broadcast received from:"
                    + packet.getAddress().getHostName() + ": " + msg);
            if (msg.contains("PowerPoint Remote")) {
                break;
            }
            packet.setLength(buffer.length);
        }
    } catch (IOException e) {
        Log.e("PPTREMOTE", e.getMessage() + "\n" + e.getStackTrace() + "\n"
                + e.getCause());
    }
    final String[] result = { msg, packet.getAddress().getHostName() };
    runningOn.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            runningOn.onServerFound(result);
        }
    });

    return;
}

logcat的:

04-22 19:50:56.964: E/PPTREMOTE(7791): bind failed: EADDRINUSE (Address already in use)
04-22 19:50:56.964: E/PPTREMOTE(7791): [Ljava.lang.StackTraceElement;@426a83d8
04-22 19:50:56.964: E/PPTREMOTE(7791): libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)
04-22 19:50:56.964: W/dalvikvm(7791): threadid=12: thread exiting with uncaught exception (group=0x41827e90)
04-22 19:50:56.964: D/xxdalvikvm(7791): XX::Lcom/android/internal/util/FastPrintWriter$1; was loaded
04-22 19:50:56.964: E/ViewRootImpl(7791): mIsPenSupport: :true
04-22 19:50:56.964: E/AndroidRuntime(7791): FATAL EXCEPTION: Thread-6949
04-22 19:50:56.964: E/AndroidRuntime(7791): Process: de.predefined.powerpointremote, PID: 7791
04-22 19:50:56.964: E/AndroidRuntime(7791): java.lang.NullPointerException
04-22 19:50:56.964: E/AndroidRuntime(7791):     at de.predefined.powerpointremote.BroadcastReceiver.run(BroadcastReceiver.java:63)

0 个答案:

没有答案