TCP\IP client - EHOSTUNREACH (No route to host)
我正在尝试开发客户端服务器 TCP/IP 应用程序。我的电脑上运行着一台服务器,而客户端应用正在移动设备上运行。它们都在同一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();
}
}
});
}
}
我做错了什么?