0

I used code from this website about socket programming in Android. One phone is the client while the other acts as server. I copy/pasted the code exactly and placed the server code on one phone and that of client on the other. Both phones are connected to the same wifi router.

However, I am unable to send even a simple message from the client to server phone. I get timeouts and error of java.net.ConnectException: failed to connect to /10.0.2.15 (port 8080): connect failed: EHOSTUNREACH (No route to host)

I read that some others have succeeded using this code. That's why I suppose it has something to do with the wifi connection. I tried two other routers and faced the same problem. I also tried setting up Port forwarding (it is a setting on some routers that you tell the router that you are using some port like for example 8080 and you need to do this in order for the sockets programming to work). However, both routers don't have English language setting. So I couldn't find where the port forwarding setting is. I can get a new router with English language settings if it is required to get this thing working.

My question is: Do you think port forewarding setting may very likely be the problem or is there something else I have overlooked? Any ideas to point me in the right direction would be appreciated.

Here is the full Logcat output:

01-22 18:37:40.100: E/ClientActivity(19568): C: Error
01-22 18:37:40.100: E/ClientActivity(19568): java.net.ConnectException: failed to connect to /10.0.2.15 (port 8080): connect failed: EHOSTUNREACH (No route to host)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.IoBridge.connect(IoBridge.java:114)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.Socket.startupSocket(Socket.java:566)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.net.Socket.<init>(Socket.java:225)
01-22 18:37:40.100: E/ClientActivity(19568):    at com.example.client.ClientActivity$ClientThread.run(ClientActivity.java:60)
01-22 18:37:40.100: E/ClientActivity(19568):    at java.lang.Thread.run(Thread.java:856)
01-22 18:37:40.100: E/ClientActivity(19568): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.Posix.connect(Native Method)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-22 18:37:40.100: E/ClientActivity(19568):    at libcore.io.IoBridge.connect(IoBridge.java:112)
4

1 に答える 1

0

そこにあるコードは、それ自体では機能しません。ニーズに合わせて、サーバーとクライアントの両方で IP アドレスを変更する必要があります。現在のサーバーの IP アドレスをローカルの IP アドレス「192.168.1.xxx」に変更してみてください。クライアントでは、サーバーとして入力したものと同じ IP アドレスに接続する必要があります。ポートフォワーディングのためにルーターの設定を変更する必要はありません。

于 2013-01-22T10:20:59.737 に答える