BindException地址已在使用中,但使用重用

时间:2013-01-22 11:38:46

标签: java sockets networking

我有一个问题,我的网络......

        protected Void doInBackground(Void... arg0) {
        int nreq = 1;
        System.out.println("Hall000");
        try {
            ServerSocket sock = new ServerSocket(6677);
            sock.setReuseAddress(true);
            sock.setSoTimeout(1000);
                Socket newsock = sock.accept();
                System.out.print("\n");
                System.out.println("Creating command...");
                System.out.print("\n");
                new answercall();

                nreq++;
                System.out.println(nreq);
                final DataInputStream din = new DataInputStream(
                        ss.getInputStream());
                // Einlesen
                diny1 = (din).read();
                diny2 = (din).read();
                diny3 = (din).read();
                diny4 = (din).read();
                diny5 = (din).read();
                diny6 = (din).read();
                diny7 = (din).read();
                System.out.println("diny" + diny1 + " " + diny2 + " "
                        + diny3 + " " + diny4 + " " + diny5 + " " + diny6
                        + " " + diny7);
                setDiny1(diny1);
                setDiny2(diny2);
                setDiny3(diny3);
                setDiny4(diny4);
                setDiny5(diny5);
                setDiny6(diny6);
                setDiny7(diny7);
                System.out.println(diny1 + " " + diny2 + " " + diny3 + " "
                        + diny4 + " " + diny5 + " " + diny6 + " " + diny7
                        + " " + diny8);
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY1, diny1).commit();
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY2, diny2).commit();
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY3, diny3).commit();
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY4, diny4).commit();
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY5, diny5).commit();
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY6, diny6).commit();
                getSharedPreferences(DINY, MODE_PRIVATE).edit()
                        .putInt(DINY7, diny7).commit();


        } catch (Exception e) {
            System.out.println("IO error" + e);
        }
        return null;

    }

LogCat告诉我:

IO errorjava.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)

我知道这意味着什么,但我不知道,为什么会这样......我使用重用,所以它不应该来,是吗?我做错了什么?

1 个答案:

答案 0 :(得分:3)

来自ServerSocket的文档:

  

绑定套接字后启用或禁用SO_REUSEADDR时的行为(请参阅isBound())未定义。

您必须创建没有绑定的套接字,设置SO_REUSEADDR并在之后绑定:

ServerSocket sock = new ServerSocket();
sock.setReuseAddress(true);
sock.bind(new InetSocketAddress(6677));

PS:当你试图阻止他们改善你的问题时,你最大限度地减少愿意帮助你的人数。