2

I am getting java.net.ConnectException: /ip_address:port - Connection refused exception.

Here is the code:

public boolean sendMessage(String message, String ip, int port) 
    {
        try {


            Log.i("Log", "Socket Operator IP before split : "+ip);
            String[] str = ip.split("\\.");
            for(int i =0;i<str.length;i++)
            {
                Log.i("Log", "Socket Operator after split : "+str[i]);
            }
            byte[] IP = new byte[str.length];

            for (int i = 0; i < str.length; i++) {

                IP[i] = (byte) Integer.parseInt(str[i]);                
            }               
            Socket socket = getSocket(InetAddress.getByAddress(IP), port);
            if (socket == null) {
                Log.i("Log","Socket Operator SOCKET got null");
                return false;
            }

            PrintWriter out = null;
            out = new PrintWriter(socket.getOutputStream(), true);

            out.println(message);
        } catch (UnknownHostException e) {      
            Log.i("Log","Socket Operator SOCKET got UnknownHostException");
            return false;
            //e.printStackTrace();
        } catch (IOException e) {
            Log.i("Log","Socket Operator SOCKET got IOException");
            e.printStackTrace();
            return false;           
            //e.printStackTrace();
        }
        Log.i("Log","Socket Operator SOCKET got executed");
        return true;        
    }

And Here is my getSocket method:

private Socket getSocket(InetAddress addr, int portNo) 
    {
        Log.i("Log","Socket Operator InetAddress : "+addr.toString()+" Port : "+portNo);            
        Socket socket = null;
        if (sockets.containsKey(addr) == true) 
        {
            Log.i("Log","Socket Operator socket is true");
            socket = sockets.get(addr);
            // check the status of the socket
            if  ( socket.isConnected() == false ||
                  socket.isInputShutdown() == true ||
                  socket.isOutputShutdown() == true ||
                  socket.getPort() != portNo 
                 )  
            {           
                // if socket is not suitable,  then create a new socket
                sockets.remove(addr);               
                try {
                    socket.shutdownInput();
                    socket.shutdownOutput();
                    socket.close();
                    socket = new Socket(addr, portNo);
                    sockets.put(addr, socket);
                } 
                catch (IOException e) {                 
                    Log.e("getSocket: when closing and removing", "");
                }               
            }
        }
        else  
        {
            Log.i("Log","Socket Operator socket is false");
            try {
                Log.i("Log","Socket Operator socket is false in try start");
                socket = new Socket(addr, portNo);
                sockets.put(addr, socket);
            } catch (IOException e) {
                Log.e("getSocket: when creating", "");              
            }                   
        }
        return socket;      
    }

I have followed these links but no luck:

1 https://stackoverflow.com/a/6876306/1395259

2 java.net.ConnectException - Connection Refused Android Emulator

Please help me out to solve this.

Thanks in advance.

Community
  • 1
  • 1
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85

1 Answers1

-1

I encountered similar issue, if you see the message

This will occur when the Port is wrong or blocked or no service:

java.net.ConnectException: failed to connect to /xx.xx.xx.xx (port xx) after xxms: isConnected failed: ECONNREFUSED (Connection refused)

When the IP or host name cannot find in network or blocked and timeout specified you see message like this:

java.net.SocketTimeoutException: failed to connect to /xx.xx.xxx.xx (port xx) after 10000ms

When there is no Network connected on the Device you see the message like this:

java.net.ConnectException: failed to connect to /xx.xx.xx.xxx (port xx) after 10000ms: connect failed: ENETUNREACH (Network is unreachable)


(for the record), internally my API used this method:

com.android.okhttp.internal.http.HttpURLConnectionImpl.execute()

So check the Port number for your case, try with different Port number.

skstar
  • 415
  • 4
  • 12
  • Second and third aren't right. Connect timeouts cause a `ConnectException.` The `SocketTimeoutException` is for reads and accepts only: see the Javadoc. Unreachable networks cause the third message, not the second. The third one causes a `ConnectException: connect timed out`. – user207421 Nov 28 '14 at 03:26
  • @EJP , these are the results i provided by testing on the Android devices, and the Emulator (Android API Level 21) . I directly verified these by checking the LogCat logs. Anyone who execute these tests can challenge. – skstar Nov 28 '14 at 05:13
  • And you've now edited your answer to agree with me. Make up your mind. If Android really throws `SocketTimeoutException` on `connect(),` which is now far from clear after your edit, it is in contravention of the contract expressed in the Javadoc, – user207421 Dec 01 '14 at 04:34