【问题标题】:Android Client app cannot connect to Java PC Server - No Route to Host ErrorAndroid 客户端应用程序无法连接到 Java PC 服务器 - 没有路由到主机错误
【发布时间】:2016-11-09 02:09:01
【问题描述】:

我正在为 Android 构建一个应用程序,它将与 PC 通信并交换数据。我主要通过几个示例来学习如何制作应用程序以及如何使用 Java 套接字。我一直在努力工作的一个例子是:

http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html

我已经从上面的代码中修改了一些东西(变量名称和按键功能),当我使用 IP 地址:10.0.2.2,并在模拟器中运行应用程序和连接到桌面的服务器时专用网络,应用程序和服务器工作。我能够从模拟器上的应用程序向服务器发送消息,服务器接收它。

但是,当我在笔记本电脑上运行服务器并在 android 手机上运行客户端应用程序时,它们都以无线方式连接到另一个专用网络,我在 android studio 控制台中收到错误消息“No route to host强>。”我进入我的笔记本电脑命令终端并能够ping我的手机。我已经用正确的 IPv4 地址和套接字替换了代码区域。我还将我的笔记本电脑和手机连接到另一个无线网络,但仍然无法建立连接。

可能是什么问题?我必须在代码中指定什么东西才能启用连接吗?还是我必须对网络或硬件设备做一些事情来建立连接?

请注意: 活动、清单和布局的代码与链接中显示的基本相同,除了变量名称更改和我删除了函数:public boolean onCreateOptionsMenu(Menu menu)

感谢您的帮助!

编辑 2 下面是我的android客户端源代码:

    import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String messsage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textField = (EditText) findViewById(R.id.editText1); // reference to the text field
        button = (Button) findViewById(R.id.button1); // reference to the send button

        // Button press event listener
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                messsage = textField.getText().toString(); // get the text message on the text field
                textField.setText(""); // Reset the text field to blank
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
            }
        });
    }

    private class SendMessage extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                client = new Socket("192.168.1.37", 4444); // connect to the server
                printwriter = new PrintWriter(client.getOutputStream(), true);
                printwriter.write(messsage); // write the message to output stream

                printwriter.flush();
                printwriter.close();
                client.close(); // closing the connection

            } catch (UnknownHostException e) {
                Context context = getApplicationContext();
                /* If I try to send a toast notification my app crashes
                String text = "Could not connect";
                int duration = Toast.LENGTH_SHORT;
                Toast notify = Toast.makeText(context, text, duration);
                notify.show();*/
                e.printStackTrace();
            } catch (IOException e) {
                Context context = getApplicationContext();
                /*String text = "Could not connect";
                int duration = Toast.LENGTH_SHORT;
                Toast notify = Toast.makeText(context, text, duration);
                notify.show();*/
                e.printStackTrace();
            }
            return null;
        }

    }
}

服务器源代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author alexc
 */
public class JavaServer {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;

    public static void main(String[] args) throws IOException {
        try {
            serverSocket = new ServerSocket(4444); // Server socket

        } catch (IOException e) {
            System.out.println("Could not listen on port: 4444");
        }

        System.out.println("Server started. Listening to the port 4444");
                int count = 0;
        while (count < 1000) {
                    count++;
            try {

                clientSocket = serverSocket.accept(); // accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                message = bufferedReader.readLine();

                System.out.println(message);
                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex) {
                System.out.println("Problem in message reading");
            }
        }
                serverSocket.close();
    }

}

【问题讨论】:

  • 你只需要更改IP地址。那你用了什么?
  • I get an error in the android studio console。安慰?我认为 logcat 中有一个例外。
  • @greenapps 是的,对于不正确的术语感到抱歉,但是是的,logcat 显示了一个异常,上面写着“没有路由到主机”。我习惯于调用 IDE 输出区域、控制台。是的,我在 android 客户端上的这行代码(android 客户端示例代码的第 57 行)中将 IP 地址更改为网络上笔记本电脑的 IP 地址:'client = new Socket(myIP, 4444);' myIP 是一个 IPv4 地址,例如 192.xxx.x.x.
  • 调整您的代码,以便用户了解异常情况。然后切换防火墙。
  • 你可能会安装一些安卓控制台(终端)应用程序并尝试 ping 到服务器 ip.. 检查你是否得到同样的错误

标签: java android sockets networking


【解决方案1】:

大家好,所以我得到了我朋友的路由器,它是 Cisco 的 Linksys E1000 Wireless-N 路由器,并测试了我的 android 应用程序和 Java 服务器,它工作正常。不知道确切的配置是什么,但这是我所做的:

  1. 在路由器上启用 DHCP,否则您必须在 PC 和 Android 手机上设置静态 IP 地址

  2. 确保您的 PC 和 Android 手机已启用可在网络上被发现。我只记得当我连接到这个路由器时在我的电脑上,它询问我是否希望能够在网络上被发现

  3. 关闭防火墙(和防病毒),或保持端口打开,您希望您的 Android 应用程序的所有连接都能通过。

如果您仍有问题,这里有一些一般性的故障排除提示(因为您使用的任何设备都会有所不同):

  • 如果您没有为您的 PC 或安卓手机分配 IP 地址并且 DHCP 已启用,请尝试重置路由器(将其关闭然后重新打开;或完全系统重置)

  • 对于 Windows,打开命令提示符并键入 ipconfig 并找到您的 IPv4 地址。确保正确输入地址。然后在命令提示符中输入ping 'android phone's IP Address' 例如:

    ping 192.168.1.121

如果您收到响应,则表示您的手机已连接到网络。

然后尝试 ping 您为ipconfig 找到的 IPv4 地址,如果您的手机可以 ping 通它,那么您就没事了。如果无法ping通,请尝试关闭防火墙和防病毒软件。

无论如何,谢谢大家的帮助和建议。希望这些建议有所帮助。

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多