如何在AsyncTask中启动客户端并连接到服务器?

时间:2017-08-06 19:46:41

标签: java android sockets android-asynctask server

我通过命令行服务器使用端口6666在计算机上运行。告诉我如何通过单击模拟器中的按钮连接到它?我出现在广泛的海鸥“连接......”然后出现错误:W / System.err: java.net.ConnectException: Connection refused。在我运行服务器和客户端只是单独的文件的同时,一切正常,它与命令行或android studio无关。我究竟做错了什么? 这是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);
        }
    }
}

我收到以下错误:

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)

2 个答案:

答案 0 :(得分:1)

更改您要连接的IP地址

String address = "127.0.0.1"; 

使用运行服务器的计算机的IP地址,因为127.0.0.1将指向移动设备,而不是指向托管模拟器的计算机。

你应该使用像

这样的东西
String address = "192.168.100.10";

或计算机拥有的任何IP地址

答案 1 :(得分:1)

如果您使用的是模拟器,可能是您使用的IP地址错误,则应使用10.0.0.2。 see the android docs