Tenday
Tenday

Reputation: 121

How to start the client and connect to the server in AsyncTask?

I run on the computer through the command line server with port 6666. Tell me how can I connect to it by clicking on the button in the emulator? I appear in the broad gulls "Connecting ..." and then an error: W / System.err: java.net.ConnectException: Connection refused. At the same time when I run the server and the client just separate files, everything works, it does not matter the command line or in android studio. What am I doing wrong? Here is MainActivity:

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button but;
    MyTask mt;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but = (Button) findViewById(R.id.btnOk);
        but.setOnClickListener(this);
    }

    public void onClick(View v) {
        mt = new MyTask();
        mt.execute();
    }

    class MyTask extends AsyncTask<Void, Void, Void> {
        int port = 6666;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            int serverPort = 6666; 
            String address = "127.0.0.1"; 

            try {
                InetAddress ipAddress = InetAddress.getByName(address);
                System.out.println("Connecting...");
                Socket socket = new Socket(ipAddress, serverPort);
                System.out.println("Connected");

            } catch (Exception x) {
                x.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }
    }
}

I get the following error:

java.net.ConnectException: Connection refused
     at java.net.PlainSocketImpl.socketConnect(Native Method)
     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
     at java.net.Socket.connect(Socket.java:605)
     at java.net.Socket.connect(Socket.java:554)
     at java.net.Socket.<init>(Socket.java:431)
     at java.net.Socket.<init>(Socket.java:241)
     at com.tenday.myapplication.MainActivity$MyTask.doInBackground(MainActivity.java:56)
     at com.tenday.myapplication.MainActivity$MyTask.doInBackground(MainActivity.java:41)
     at android.os.AsyncTask$2.call(AsyncTask.java:305)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
     at java.lang.Thread.run(Thread.java:761)

Upvotes: 0

Views: 1026

Answers (2)

Galalen
Galalen

Reputation: 64

If you are using the emulator probably the IP address you are using is wrong you should use 10.0.0.2. see the android docs

Upvotes: 1

Yazan
Yazan

Reputation: 6082

change the IP address you are connecting to

String address = "127.0.0.1"; 

use the IP address of the machine that runs the server, as 127.0.0.1 will point to the mobile not the computer that hosts the emulator.

you should use something like

String address = "192.168.100.10";

or whatever IP address the computer have

Upvotes: 1

Related Questions