我试图在Android上实施tcp客户端应用。当我尝试连接到我的C ++服务器时,套接字在尝试连接到服务器时超时。
我的代码:
new Thread(new ClientThread()).start();
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
out.println("Test message.");
}
catch (Exception e)
{
// ERROR1
e.printStackTrace();
}
...
class ClientThread implements Runnable
{
@Override
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName("192.168.1.116");
socket = new Socket(serverAddr, 9000);
}
catch (Exception e)
{
// ERROR2
e.printStackTrace();
}
}
}
首先,发生ERROR1(套接字为空),然后发生ERROR2(连接超时)。服务器运行正常,我已经与不同的客户端进行了测试。我有"使用 - 许可"所以它不应该是一个问题。
编辑: 堆叠在ERROR2:
05-17 02:26:50.789: W/System.err(26625): java.net.ConnectException: failed to connect to /192.168.1.116 (port 9000): connect failed: ETIMEDOUT (Connection timed out)
05-17 02:26:50.789: W/System.err(26625): at libcore.io.IoBridge.connect(IoBridge.java:114)
05-17 02:26:50.789: W/System.err(26625): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-17 02:26:50.789: W/System.err(26625): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-17 02:26:50.789: W/System.err(26625): at java.net.Socket.startupSocket(Socket.java:566)
05-17 02:26:50.789: W/System.err(26625): at java.net.Socket.<init>(Socket.java:225)
05-17 02:26:50.789: W/System.err(26625): at cz.gclient.gardenclient.MainActivity$ClientThread.run(MainActivity.java:153)
05-17 02:26:50.789: W/System.err(26625): at java.lang.Thread.run(Thread.java:841)
05-17 02:26:50.789: W/System.err(26625): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
05-17 02:26:50.789: W/System.err(26625): at libcore.io.Posix.connect(Native Method)
05-17 02:26:50.789: W/System.err(26625): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-17 02:26:50.789: W/System.err(26625): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-17 02:26:50.789: W/System.err(26625): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-17 02:26:50.789: W/System.err(26625): ... 6 more
答案 0 :(得分:12)
'连接超时'是网络连接问题。例如,可能有防火墙。这不是一个编程问题,你无法在代码中解决它。