TCP \ IP客户端 - EHOSTUNREACH(无主机路由)

时间:2012-09-20 23:30:34

标签: java android tcpclient

我正在尝试开发客户端服务器TCP / IP应用程序。我的PC上运行了服务器,客户端应用程序正在移动设备上运行。它们都在同一个Wi-Fi网络中,但我无法在它们之间建立连接。调试客户端Android应用程序时显示以下错误:

09-21 01:08:40.422: W/System.err(8536): java.net.ConnectException: failed to connect to /192.168.15.115 (port 4449): connect failed: EHOSTUNREACH (No route to host)
09-21 01:08:40.453: W/System.err(8536):     at libcore.io.IoBridge.connect(IoBridge.java:114)
09-21 01:08:40.453: W/System.err(8536):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-21 01:08:40.453: W/System.err(8536):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.startupSocket(Socket.java:566)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.tryAllAddresses(Socket.java:127)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.<init>(Socket.java:177)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.<init>(Socket.java:149)
09-21 01:08:40.453: W/System.err(8536):     at sabarish.example.client_mobile.MainActivity$1.onClick(MainActivity.java:61)
09-21 01:08:40.453: W/System.err(8536):     at android.view.View.performClick(View.java:3511)
09-21 01:08:40.453: W/System.err(8536):     at android.view.View$PerformClick.run(View.java:14105)
09-21 01:08:40.453: W/System.err(8536):     at android.os.Handler.handleCallback(Handler.java:605)
09-21 01:08:40.453: W/System.err(8536):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 01:08:40.453: W/System.err(8536):     at android.os.Looper.loop(Looper.java:137)
09-21 01:08:40.453: W/System.err(8536):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-21 01:08:40.453: W/System.err(8536):     at java.lang.reflect.Method.invokeNative(Native Method)
09-21 01:08:40.453: W/System.err(8536):     at java.lang.reflect.Method.invoke(Method.java:511)
09-21 01:08:40.453: W/System.err(8536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-21 01:08:40.453: W/System.err(8536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-21 01:08:40.453: W/System.err(8536):     at dalvik.system.NativeStart.main(Native Method)
09-21 01:08:40.457: W/System.err(8536): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.Posix.connect(Native Method)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.IoBridge.connect(IoBridge.java:112)
09-21 01:08:40.457: W/System.err(8536):     ... 18 more

我正在使用的代码:

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

    @Override
    public 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.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

                try {
                    client = new Socket("192.168.15.115", 4449);  //connect to 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) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }); 
    }
}

我做错了什么?

6 个答案:

答案 0 :(得分:6)

这看起来像网络问题而不是Java问题。之一:

  • 客户端上的网络要么不知道如何将数据包路由到服务器,
  • 客户端在端口4449上的连接尝试被防火墙或数据包过滤器阻止,或
  • 您使用的是错误的IP地址。

(“无主机路由”消息表明这是第一个问题,但防火墙有时会配置为对不需要的流量做出误导性响应。)

无论如何,您最好在寻找有关如何配置和/或网络和路由的网站。

答案 1 :(得分:2)

您好我的mac在Android设备中运行应用程序时遇到了同样的问题。为了让它起作用,我必须做两件事:

  1. 在mac
  2. 中关闭防火墙
  3. 启用红外接收器(System Pref&gt; Security&gt; Firewall&gt; Advanced)
  4. 它有效!

答案 2 :(得分:1)

当我开发一个与Java桌面服务器通信的android应用程序时,我面临同样的问题,解决问题只需断开连接Android手机与PC的USB线缆。

答案 3 :(得分:0)

在手机上下载ping应用程序,如果可以,请尝试ping服务器ip ... 如果使用apache将本地机器IP放在httpd.conf配置文件中。

# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 127.0.0.1:80
Listen 192.168.1.30:80  (your IP)

答案 4 :(得分:0)

关闭移动 WiFi 然后再打开

WifiManager mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

mWifiManager.setWifiEnabled(false);
mWifiManager.setWifiEnabled(true);

并再次尝试创建套接字连接

答案 5 :(得分:-1)

没错..

主机无法访问,就像那样。但是,如果一切都是oK(配置良好的节点/网络,良好的wifi信号,..)您可以使用ping serverIP(来自电话)或ping smartphone(来自服务器)强制执行此过程。

ping(ICMP数据包)将强制路由器加速新路由(到达主机的方式)...发送大量ping(-n选项)例如:ping google.com -n 1000(连续1000次ping)< / p>