kbang
kbang

Reputation: 704

java.net.BindException: bind failed: EADDRINUSE

I am new to android and i am trying to create a server socket . the code follows.

i am getting the warning continuously . can it be fixed ? can i ignore it?

03-28 15:47:34.460: W/System.err(3185): java.net.BindException: bind failed: EADDRINUSE (Address already in use)

03-28 15:47:34.460: W/System.err(3185):     at libcore.io.IoBridge.bind(IoBridge.java:89)

03-28 15:47:34.460: W/System.err(3185):     at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:150)

03-28 15:47:34.460: W/System.err(3185):     at java.net.ServerSocket.<init>(ServerSocket.java:100)

03-28 15:47:34.470: W/System.err(3185):     at java.net.ServerSocket.<init>(ServerSocket.java:69)

03-28 15:47:34.470: W/System.err(3185):     at <path>$server.run(<filename>.java:302)

03-28 15:47:34.470: W/System.err(3185):     at java.lang.Thread.run(Thread.java:856)
03-28 15:47:34.470: W/System.err(3185): Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)

03-28 15:47:34.470: W/System.err(3185):     at libcore.io.Posix.bind(Native Method)
03-28 15:47:34.470: W/System.err(3185):     at ibcore.io.ForwardingOs.bind(ForwardingOs.java:39)
03-28 15:47:34.470: W/System.err(3185):     at libcore.io.IoBridge.bind(IoBridge.java:87)
03-28 15:47:34.470: W/System.err(3185):     ... 5 more

03-28 15:47:34.470: W/System.err(3185): java.lang.NullPointerException
03-28 15:47:34.490: W/System.err(3185):     at <path>Provider$server.run(<filename>.java:315)

03-28 15:47:34.490: W/System.err(3185):     at java.lang.Thread.run(Thread.java:856)

Code:

class server implements Runnable {

        public void run() {


        ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(10000);
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            //  Log.d("Recieving ", "Server Socket Created");


            try {
                while(true) {
                    // Blocks until a connection occurs:
                    try {

                Socket client = serverSocket.accept();
                        //Log.d("Recieving ", "Client request accepted");
                        str_proc tk = new str_proc(client);
                        tk.start();

                    } catch (IOException e1) {

                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                        Log.d("Recieving ", "Problem creating socket for listening");

                    }
                }//while true loop ends

            }catch (Exception e){
                e.printStackTrace();
            }
        }
}

Upvotes: 4

Views: 10331

Answers (2)

user207421
user207421

Reputation: 310957

Your exception handling is up the pole.

The catch after the creation of the ServerSocket shouldn't be there at all: it should be after the code that uses the ServerSocket, in which case it could be combined with the existing second catch. Code that relies on the success of code in a prior try block should be inside that try block.

However, the only way you can be getting that error continuously is if you are starting the thread continuously, which doesn't make sense anyway as you can only have one listener at any given TCP port.

So investigate why you are starting the thread over and over again, and stop it,

Upvotes: 3

You can call close() from another thread, and the accept() call will throw a SocketException. In this way in the next call of ServerSocket constructor you won't get "java.net.BindException: bind failed: EADDRINUSE".

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"`enter code here`
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="project.vasile.emanuel.gresanu.test_server_socket.MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click"/>
</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

    private static final String TAG=MainActivity.class.getSimpleName();
    private static boolean isClick;
    private My_Server My_Server;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button) findViewById(R.id.btn);
        isClick=false;
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(!isClick)
                 {
                     Log.i(TAG,"Initilize and Running My_Server");
                     My_Server =new My_Server();
                     if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB)
                     {
                         My_Server.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,(Void[])null);
                     }
                     else
                     {
                         My_Server.execute((Void[])null);
                     }
                     isClick=true;
                 }
                else
                 {
                    if(null!= My_Server)
                    {
                        My_Server.cancelAccept();
                        isClick=false;
                    }
                 }
            }
        });
    }
}

My_Server

public class My_Server extends AsyncTask<Void, Void, Void> {

    private static final String TAG = My_Server.class.getSimpleName();
    private static final int PORT = 8091;
    private static boolean isExiting;
    private ServerSocket serverSocket;

    @Override
    protected Void doInBackground(Void... params) {
        isExiting = false;
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Socket clientSocket=null;
        if(null!=serverSocket)
        while (!isExiting) {
            try {
                Log.i(TAG, "Waiting for a new connection");
                clientSocket=serverSocket.accept();
                Log.i(TAG, "Connction accepted");
                //Do something with the new client
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
                if(null!=this.serverSocket&& !serverSocket.isClosed()) {
                    try {
                        Log.i(TAG,"Closing Server connection");
                        this.serverSocket.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }
        try {
            Log.i(TAG,"Closing the connection");
            if(null!=serverSocket && !serverSocket.isClosed()) {
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG,e.getMessage());
        }
        return null;
    }

    public void cancelAccept() {
        Log.i(TAG, "cancelAccept()");
        isExiting = true;
        if(null!=this.serverSocket&& !serverSocket.isClosed())
        {
            try {
                Log.i(TAG,"Closing Server connection in onCancelled");
                this.serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.gc();
    }
}

Upvotes: 0

Related Questions