java.net.ConnectException

failed to connect to SERVERIP (port 8080): connect failed: ENETUNREACH (Network is unreachable) Server code - public class ServerThread implements Runnable { public void run() { try { if (SERVERIP != null) { handler.post(new Runnable() { @Override public void run() { serverStatus .setText("Listening on IP: " + SERVERIP); } }); serverSocket = new ServerSocket(8080); while (true) { // LISTEN FOR INCOMING CLIENTS Socket client = serverSocket.accept(); handler.post(new Runnable() { @Override public void run() { serverStatus2.setText("Connected."); } }); try { BufferedReader in = new BufferedReader( new InputStreamReader( client.getInputStream())); String line = null; while ((line = in.readLine()) != null) { final String currLine = line; Log.i("Server Activity",currLine); handler.post(new Runnable() { @Override public void run() { String origString = serverStatus2.getText().toString(); String newString = origString + "\n" + currLine; serverStatus2.setText(newString); ToneGenerator tone = new ToneGenerator(AudioManager.STREAM_NOTIFICATION,90); tone.startTone(ToneGenerator.TONE_PROP_ACK,10); } }); } break; } catch (Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus .setText("Oops. Connection interrupted. Please reconnect your phones."); } }); e.printStackTrace(); } } } else { handler.post(new Runnable() { @Override public void run() { serverStatus .setText("Couldn't detect internet connection."); } }); } } catch (final Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Error" + e.getMessage()); } }); e.printStackTrace(); } } } // GETS THE IP ADDRESS OF YOUR PHONE'S NETWORK private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("ServerActivity", ex.toString()); } return null; } @Override protected void onStop() { super.onStop(); try { // MAKE SURE YOU CLOSE THE SOCKET UPON EXITING serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } Client - public class ClientThread implements Runnable { public void run() { try { if (socket==null){ initSocketConn(); } //connected = true; //while (connected) { try { Log.d("ClientActivity", "C: Sending command."); if (out==null){ out = new PrintWriter( new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())), true); } // WHERE YOU ISSUE THE COMMANDS out.println(edtMessage.getText().toString()); Log.d("ClientActivity", "C: Sent."); } catch (Exception e) { Log.e("ClientActivity", "S: Error", e); } //} //socket.close(); Log.d("ClientActivity", "C: Closed."); } catch (Exception e) { Log.e("ClientActivity", "C: Error", e); connected = false; } } } private void initSocketConn(){ try{ InetAddress serverAddr = InetAddress.getByName(serverIpAddress); Log.d("ClientActivity", "C: Connecting..."); socket = new Socket(serverAddr, 8080); //THIS IS WHERE THE ERROR IS THROWN } catch (Exception ex){ ex.printStackTrace(); } } I have checked online & tried different options like using different port number but it has not worked. I am confused why the code works perfectly when the Moto is server but gives the error when NExus is Server??? Please help! Logcat - 03-29 07:25:06.065 20592-20611/com.shriwas.samplesocketapp D/OpenGLRenderer: endAllActiveAnimators on 0xb7ca02d8 (RippleDrawable) with handle 0xb7d24240 03-29 07:25:08.957 20592-21470/com.shriwas.samplesocketapp D/ClientActivity: C: Connecting... 03-29 07:25:08.958 20592-21470/com.shriwas.samplesocketapp W/System.err: java.net.ConnectException: failed to connect to /fe80::8c3a:e3ff:fe5d:2e0d%p2p0%25 (port 8080): connect failed: ENETUNREACH (Network is unreachable) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.Socket.startupSocket(Socket.java:590) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.Socket.<init>(Socket.java:226)

Samebug tips0

We couldn't find tips for this exception.

Don't give up yet. Paste your full stack trace to get a solution.

Solutions on the web57

  • via Stack Overflow by user1122549
    ,
  • Stack trace

    • java.net.ConnectException: failed to connect to SERVERIP (port 8080): connect failed: ENETUNREACH (Network is unreachable) Server code - public class ServerThread implements Runnable { public void run() { try { if (SERVERIP != null) { handler.post(new Runnable() { @Override public void run() { serverStatus .setText("Listening on IP: " + SERVERIP); } }); serverSocket = new ServerSocket(8080); while (true) { // LISTEN FOR INCOMING CLIENTS Socket client = serverSocket.accept(); handler.post(new Runnable() { @Override public void run() { serverStatus2.setText("Connected."); } }); try { BufferedReader in = new BufferedReader( new InputStreamReader( client.getInputStream())); String line = null; while ((line = in.readLine()) != null) { final String currLine = line; Log.i("Server Activity",currLine); handler.post(new Runnable() { @Override public void run() { String origString = serverStatus2.getText().toString(); String newString = origString + "\n" + currLine; serverStatus2.setText(newString); ToneGenerator tone = new ToneGenerator(AudioManager.STREAM_NOTIFICATION,90); tone.startTone(ToneGenerator.TONE_PROP_ACK,10); } }); } break; } catch (Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus .setText("Oops. Connection interrupted. Please reconnect your phones."); } }); e.printStackTrace(); } } } else { handler.post(new Runnable() { @Override public void run() { serverStatus .setText("Couldn't detect internet connection."); } }); } } catch (final Exception e) { handler.post(new Runnable() { @Override public void run() { serverStatus.setText("Error" + e.getMessage()); } }); e.printStackTrace(); } } } // GETS THE IP ADDRESS OF YOUR PHONE'S NETWORK private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("ServerActivity", ex.toString()); } return null; } @Override protected void onStop() { super.onStop(); try { // MAKE SURE YOU CLOSE THE SOCKET UPON EXITING serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } Client - public class ClientThread implements Runnable { public void run() { try { if (socket==null){ initSocketConn(); } //connected = true; //while (connected) { try { Log.d("ClientActivity", "C: Sending command."); if (out==null){ out = new PrintWriter( new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())), true); } // WHERE YOU ISSUE THE COMMANDS out.println(edtMessage.getText().toString()); Log.d("ClientActivity", "C: Sent."); } catch (Exception e) { Log.e("ClientActivity", "S: Error", e); } //} //socket.close(); Log.d("ClientActivity", "C: Closed."); } catch (Exception e) { Log.e("ClientActivity", "C: Error", e); connected = false; } } } private void initSocketConn(){ try{ InetAddress serverAddr = InetAddress.getByName(serverIpAddress); Log.d("ClientActivity", "C: Connecting..."); socket = new Socket(serverAddr, 8080); //THIS IS WHERE THE ERROR IS THROWN } catch (Exception ex){ ex.printStackTrace(); } } I have checked online & tried different options like using different port number but it has not worked. I am confused why the code works perfectly when the Moto is server but gives the error when NExus is Server??? Please help! Logcat - 03-29 07:25:06.065 20592-20611/com.shriwas.samplesocketapp D/OpenGLRenderer: endAllActiveAnimators on 0xb7ca02d8 (RippleDrawable) with handle 0xb7d24240 03-29 07:25:08.957 20592-21470/com.shriwas.samplesocketapp D/ClientActivity: C: Connecting... 03-29 07:25:08.958 20592-21470/com.shriwas.samplesocketapp W/System.err: java.net.ConnectException: failed to connect to /fe80::8c3a:e3ff:fe5d:2e0d%p2p0%25 (port 8080): connect failed: ENETUNREACH (Network is unreachable) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.Socket.startupSocket(Socket.java:590) 03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err: at java.net.Socket.<init>(Socket.java:226) at com.shriwas.samplesocketapp.MyClientActivity.initSocketConn(MyClientActivity.java:108) at com.shriwas.samplesocketapp.MyClientActivity.access$300(MyClientActivity.java:20) at com.shriwas.samplesocketapp.MyClientActivity$ClientThread.run(MyClientActivity.java:77) at java.lang.Thread.run(Thread.java:818) Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable) at libcore.io.Posix.connect(Native Method) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111) at libcore.io.IoBridge.connectErrno(IoBridge.java:137) at libcore.io.IoBridge.connect(IoBridge.java:122)

    Write tip

    You have a different solution? A short tip here would help you and many other users who saw this issue last week.

    Users with the same issue

    dmitrydmitry
    65 times, last one
    Unknown visitor
    Unknown visitor2 times, last one
    Unknown visitor
    Unknown visitorOnce,
    Indri YunitaIndri Yunita
    26 times, last one
    Hiren AmaliyarHiren Amaliyar
    2 times, last one
    40 more bugmates