0

I'm currently working on an android chat application. First, I have created the server and then, the android client. But for some reason, the client cannot connect to the server. Nevertheless, I have specified the same port for both client and server.

Could you help me, please ?

Here is the code of my server :

public class ChatServer {

    private static int port = 8080;

    public static void main (String[] args) throws IOException {


        ServerSocket server = new ServerSocket(port); /* start listening on the port */
        System.out.println( "Listening on "+ server );

        Socket client = null;

        while(true) {
            try {
                client = server.accept();
                System.out.println( "Connection from " + client );
                /* start a new thread to handle this client */
                Thread t = new Thread(new ClientConnect(client));
                t.start();
            } catch (IOException e) {

                System.err.println("Accept failed.");
                System.err.println(e);
                System.exit(1);
                server.close();
            }
        }
    }
}

and here is the client :

public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    EditText nickname = (EditText)findViewById(R.id.nicknameField);
    EditText password = (EditText)findViewById(R.id.passwordField);
    Button signin = (Button) findViewById(R.id.signin);
    Button signup = (Button) findViewById(R.id.signup);



    new Downloader(this).execute(8080);


}

and the Asynctask class

public class Downloader extends AsyncTask<Integer, Void, Void> {

    private WeakReference<MainActivity> activity;
    private int port;

    public Downloader(MainActivity act){
        super();
        activity = new WeakReference<MainActivity>(act);
    }

    protected Void doInBackground(Integer... params) {
        port = params[0];

        try {
            Socket clientSocket = new Socket("localhost", port);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

When I used the debugger on eclipse, it goes through the line

Socket clientSocket = new Socket("localhost", port);

but after that it goes to the IOException.

Thanks for your help !

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
tonystrawberry
  • 102
  • 1
  • 2
  • 12
  • 1
    You cannot use localhost. Where does your server run? Where does your app run? – greenapps May 24 '14 at 17:36
  • I just launch my server as a Java application on eclipse. and I also run my application with an android emulator. – tonystrawberry May 24 '14 at 18:01
  • Yes. Why can I not use localhost as the IP address ? – tonystrawberry May 24 '14 at 18:27
  • Because your emulator is a different system then your computer. Your app does not run on your computer. Your server does. You cannot use 127.0.01. I think you have to use something like 10.0.0.1. But I don'tknow exactly. I just have seen your problem so often described on this site. So do a little searching and you will find. – greenapps May 24 '14 at 18:37
  • Thank you a lot ! I found my solution there :http://stackoverflow.com/questions/18341652/connect-failed-econnrefused – tonystrawberry May 24 '14 at 18:47

1 Answers1

0

You can go to the properties of Android on the emulator equal like you do on a smartphone Android and see the ip you go atributed.

But if you still using the Eclipse i advise you to change to Android Studio but i think you gonna have a problema with SDK you have to unnistall that but beside that no problems.

Tiago Sousa
  • 138
  • 2
  • 13