IOException java.net.ConnectException: failed to connect to /127.0.0.1(port 5000):connect failed:ECONNREFUSED(Connection Refused)

CAPTN_BLCKA

Trying to do android socket programming based on this tutorial http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/

I have my firewall turned off and anti virus disabled. If I make my server address to 127.0.0.1 I get the error in the title. If I make it my local IP address,it just sits at socket going to be created.I have tried it without and with port forwarding and setting it to the same port.

Client

package com.bennatpjose.androidsocketclient;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Socket socket;
    private TextView text;
    private static final int SERVERPORT = 5000;
    private static final String SERVER_IP = "10.52.7.179";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView) findViewById(R.id.textView1);
    new Thread(new ClientThread()).start();
    text.setText(text.getText().toString()+"Client Thread should be fired");

}

public void onClick(View view) {
    try {

        EditText et = (EditText) findViewById(R.id.EditText01);
        String str = et.getText().toString();
        text.setText(text.getText().toString()+"\n"+"Click Event "+str);
        PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())),
                true);
        text.setText(text.getText().toString()+"\n"+"Next is out.println("+str+")");
        out.println(str);
        text.setText(text.getText().toString()+"\n"+"out.println("+str+")");
    } catch (UnknownHostException e) {
        text.setText(text.getText().toString()+"\nPrint Writer UnknownHostException "+e.toString());
    } catch (IOException e) {
        text.setText(text.getText().toString()+"\nPrint Writer IOException "+e.toString());
    } catch (Exception e) {
        text.setText(text.getText().toString()+"\nPrint Writer Exception "+e.toString());
    }
}

class ClientThread implements Runnable {

    @Override
    public void run() {
        text.setText(text.getText().toString()+"\nInside client thread run method ");
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
//If I set address to my local ip it just sits here and doesn't show socket created.


text.setText(text.getText().toString()+"\n"+serverAddr +" Socket going to be Created");
                socket = new Socket(serverAddr, SERVERPORT);
                text.setText(text.getText().toString()+"\n"+socket.toString() +"Socket Created");
            } catch (UnknownHostException e1) {
                text.setText(text.getText().toString()+"\nClient Thread UnknownHostException "+e1.toString());
            } catch (IOException e1) {
                text.setText(text.getText().toString()+"\nClient Thread IOException "+e1.toString());
            }catch (Exception e1) {
                text.setText(text.getText().toString()+"\nClient Thread Exception "+e1.toString());
            }
    }

}

}

Server

package com.bennatpjose.androidsocketserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ServerSocket serverSocket;

    Handler updateConversationHandler;

    Thread serverThread = null;

    private TextView text;

    public static final int SERVERPORT = 6000;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (TextView) findViewById(R.id.text2);
        text.setText(text.getText().toString()+"onCreate method started");
        updateConversationHandler = new Handler();

        this.serverThread = new Thread(new ServerThread());
        this.serverThread.start();
        text.setText(text.getText().toString()+"\n"+"serverThread started");
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {

        serverSocket.close();
        text.setText("!!Socket Stopped!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class ServerThread implements Runnable {

        public void run() {
            Socket socket = null;
            try {
                serverSocket = new ServerSocket(SERVERPORT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (!Thread.currentThread().isInterrupted()) {

                try {

                    socket = serverSocket.accept();
                    text.setText(text.getText().toString()+socket+"\n");

                    CommunicationThread commThread = new CommunicationThread(socket);
                    new Thread(commThread).start();

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

    class CommunicationThread implements Runnable {

        private Socket clientSocket;

        private BufferedReader input;

        public CommunicationThread(Socket clientSocket) {

            this.clientSocket = clientSocket;

            try {

                this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

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

        public void run() {

            while (!Thread.currentThread().isInterrupted()) {

                try {

                    String read = input.readLine();

                    updateConversationHandler.post(new updateUIThread(read));

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

    }

    class updateUIThread implements Runnable {
        private String msg;

        public updateUIThread(String str) {
            this.msg = str;
        }

        @Override
        public void run() {

            text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
        }
    }
}
CAPTN_BLCKA

So I figured it out. One of the mistakes I did was replacing the SERVER_IP with my local ip where as I should have left it at 10.0.2.2 since its a special loopback address android emulators. 10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

You also have to run the server first,set the port redirection and then run the client.

Look up Emulator networking section at http://developer.android.com/tools/devices/emulator.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)

From Dev

java.net.ConnectException: fail to connect to localhost/127.0.0.1(port 8080): connect failed:ECONNREFUSED….(Codename One App)

From Dev

Failed to connect to the java service wrapper (java.net.ConnectException)

From Dev

java.net.ConnectException: Connection refused: connect

From Dev

Failed to connect: 0 push notification

From Dev

Hadoop "failed on connection exception: java.net.ConnectException: Connection refused"

From Dev

java.net.ConnectException: Connection refused: connect for HTTPS connections

From Dev

Tomcat exception - java.net.ConnectException: Connection refused: connect

From Dev

java.net.ConnectException: Connection refused: connect: localhost

From Dev

Jetty 9.0.6 - java.net.ConnectException: Connection refused: connect

From Dev

java.net.ConnectException: Connection refused: connect,Launch canceled

From Dev

WebDriverException: java.net.ConnectException: Failed to connect to localhost error with Selenium 3 and chromedriver on MacOS

From Dev

java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80

From Dev

"Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: /tmp/mongodb-27017.sock:0: Connection refused''"

From Dev

Error Handle in Rxjava : ConnectException: Failed to connect

From Dev

IOException: failed to connect to /192.168.70.1 (port 9900): connect failed: ETIMEDOUT (Connection timed out)

From Dev

IOException: failed to connect to /192.168.70.1 (port 9900): connect failed: ETIMEDOUT (Connection timed out)

From Java

Elasticsearch: Failed to connect to localhost port 9200 - Connection refused

From Dev

curl (7): Failed to connect to localhost port 8000: Connection refused

From Dev

curl: (7) Failed to connect to 127.0.0.1 port 51786: Connection refused

From Dev

Bluetooth connect failed. java.io.IOException: read failed, socket might closed or timeout, read ret: -1

From Dev

Bluetooth connect failed. java.io.IOException: read failed, socket might closed or timeout, read ret: -1

From Dev

Failed to connect to: localhost:27017: Connection refused

From Dev

Failed to connect to server: Connection refused (111)

From Dev

System error 111 (connection refused) , Failed to connect

From Dev

rsync: failed to connect to 1.2.3.4\#015: Connection refused

From Dev

How to resolve java net ConnectException Connection refused connect even server is up

From Dev

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

From Dev

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect in Spring Boot

Related Related

  1. 1

    java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)

  2. 2

    java.net.ConnectException: fail to connect to localhost/127.0.0.1(port 8080): connect failed:ECONNREFUSED….(Codename One App)

  3. 3

    Failed to connect to the java service wrapper (java.net.ConnectException)

  4. 4

    java.net.ConnectException: Connection refused: connect

  5. 5

    Failed to connect: 0 push notification

  6. 6

    Hadoop "failed on connection exception: java.net.ConnectException: Connection refused"

  7. 7

    java.net.ConnectException: Connection refused: connect for HTTPS connections

  8. 8

    Tomcat exception - java.net.ConnectException: Connection refused: connect

  9. 9

    java.net.ConnectException: Connection refused: connect: localhost

  10. 10

    Jetty 9.0.6 - java.net.ConnectException: Connection refused: connect

  11. 11

    java.net.ConnectException: Connection refused: connect,Launch canceled

  12. 12

    WebDriverException: java.net.ConnectException: Failed to connect to localhost error with Selenium 3 and chromedriver on MacOS

  13. 13

    java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80

  14. 14

    "Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: /tmp/mongodb-27017.sock:0: Connection refused''"

  15. 15

    Error Handle in Rxjava : ConnectException: Failed to connect

  16. 16

    IOException: failed to connect to /192.168.70.1 (port 9900): connect failed: ETIMEDOUT (Connection timed out)

  17. 17

    IOException: failed to connect to /192.168.70.1 (port 9900): connect failed: ETIMEDOUT (Connection timed out)

  18. 18

    Elasticsearch: Failed to connect to localhost port 9200 - Connection refused

  19. 19

    curl (7): Failed to connect to localhost port 8000: Connection refused

  20. 20

    curl: (7) Failed to connect to 127.0.0.1 port 51786: Connection refused

  21. 21

    Bluetooth connect failed. java.io.IOException: read failed, socket might closed or timeout, read ret: -1

  22. 22

    Bluetooth connect failed. java.io.IOException: read failed, socket might closed or timeout, read ret: -1

  23. 23

    Failed to connect to: localhost:27017: Connection refused

  24. 24

    Failed to connect to server: Connection refused (111)

  25. 25

    System error 111 (connection refused) , Failed to connect

  26. 26

    rsync: failed to connect to 1.2.3.4\#015: Connection refused

  27. 27

    How to resolve java net ConnectException Connection refused connect even server is up

  28. 28

    com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

  29. 29

    com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect in Spring Boot

HotTag

Archive