简体   繁体   中英

BindException Address already in use, but using reuse

i have a problem, with my 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 tells me that:

IO errorjava.net.BindException: bind failed: EADDRINUSE (Address already in use)

I know what it means, but i dont know, why it comes... im using reuse, so it shouldnt come, should it? what have i done wrong?

From the documentation of ServerSocket :

The behaviour when SO_REUSEADDR is enabled or disabled after a socket is bound (See isBound()) is not defined.

You have to create the socket without binding, set SO_REUSEADDR and bind afterwards:

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

PS: You minimize the amount of people willing to help you, when trying to prevent them from improving your question.

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