IOException java.net.ConnectException: 连接失败,无法连接到 /127.0.0.1(端口5000): 连接失败:ECONNREFUSED(连接被拒绝)。

3
尝试根据此教程进行基于Android套接字编程 http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/ 我关闭了防火墙并禁用了反病毒软件。 如果我将服务器地址设置为127.0.0.1,则会出现标题中的错误。 如果我将其设置为本地IP地址,则只会停留在即将创建的套接字上。 我尝试过不使用端口转发,也尝试过设置为相同的端口。
客户端
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());
            }
    }

}

}

服务器

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");
        }
    }
}

我应该补充说明一下,我正在两个 AVD 上运行它。 - CAPTN_BLCKA
1个回答

1
我找到了问题所在。我曾经犯过的一个错误是,我用我的本地IP地址替换了SERVER_IP,而实际上我应该把它保留为10.0.2.2,因为这是一个特殊的环回地址,适用于Android模拟器。
10.0.2.2是对主机环回接口(即开发计算机上的127.0.0.1)的特殊别名。
此外,您还需要首先运行服务器,设置端口重定向,然后再运行客户端。
请参考http://developer.android.com/tools/devices/emulator.html中的“模拟器网络”部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接