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

2023-09-14 00:00:19 作者:拜你所赐

我想开发一个客户端服务器的TCP / IP的应用程序。我有我的电脑上运行的服务器和客户端应用程序是在移动运行。他们都是在同一个Wi-Fi网络,但我不能在它们之间建立连接。在调试客户端Android应用程序表现出以下错误:

I am trying to develop a client server TCP/IP application. I have a server running on my PC and the client app is running on the mobile. They both are in the same Wi-Fi network, but I cannot establish a connection between them. While debugging the client Android app showed the following error:

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

我使用的code:

The code I am using:

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

  }

我是什么做错了吗?

What am I doing wrong?

推荐答案

在没有路由的错误表示当TCP连接尝试,连接失败,因为底层协议软件无法找到一个网络节点到网络节点路线指定的目标节点。

The no route error indicates that when the TCP connection was attempted, the connection failed because the underlying protocol software could not find a network node to network node route to the designated target node.

该修补程序是有点依赖于操作系统,但它主要是要求你设置你的路由表,以便从中你正在试图连接的设备能够弄清楚如何你想连接到该设备。

The fix is somewhat operating system dependent but it mainly requires you to set up your routing tables so that the device from which you are trying to connect is able to figure out how to reach the device to which you want to connect.

通常你指定一个网关,当你试图连接,连接请求超出网络网关的其他信息技术设备,如路由器解决,等等。

Normally you specify a gateway to which when you attempt to connect, the connection request goes out the network gateway to be resolved by other information technology devices such as routers, etc.

这看起来是一个Android设备,所以第一件事情就是要确保你有是否连接WiFi或细胞。另一个办法是确保该WiFi或细胞被接通和操作。

This looks to be an Android device, so the first thing is to make sure that you have connectivity whether WiFi or cell. Another would be to make sure that the WiFi or cell is turned on and operational.

您指定的IP地址通常是私人子网内。因此,要检查的是是,你试图连接到该设备是否在同一子网内,你的设备上。

The IP address you specify is normally within a private sub-net. So thing to check is is whether the device that you are trying to connect to is on the same sub-net that your device is on.

下面是子网和路由一个文件。

Here is a document on sub-nets and routing.