Android的 - 套接字超时而连接 [英] android - socket timeout while connecting
问题描述
我想实现在Android TCP客户端应用程序。当我尝试插座倍连接到我的C ++服务器,出于尝试连接到服务器。
我的code:
新主题(新的ClientThread())开始();
尝试
{
为PrintWriter了=的新PrintWriter(新的BufferedWriter(
新OutputStreamWriter(socket.getOutputStream())),TRUE);
通过out.println(测试消息。);
}
赶上(例外五)
{
// ERROR1
e.printStackTrace();
}...类ClientThread实现Runnable
{
@覆盖
公共无效的run()
{
尝试
{
InetAddress类serverAddr = InetAddress.getByName(192.168.1.116);
插座=新的Socket(serverAddr,9000);
}
赶上(例外五)
{
//误差2
e.printStackTrace();
}
}
}
首先,ERROR1发生(插座是空),则误差2发生时(连接超时)。做工精致的服务器,我与不同的客户端进行了测试。我有使用许可权,因此它不应该是一个问题。
编辑:
在叠误差2:
2月5日至17日:26:50.789:W / System.err的(26625):java.net.ConnectException:无法连接到/192.168.1.116(9000端口)连接失败:ETIMEDOUT(连接超时)
二月5日至17日:26:50.789:W / System.err的(26625):在libcore.io.IoBridge.connect(IoBridge.java:114)
二月5日至17日:26:50.789:W / System.err的(26625):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
二月5日至17日:26:50.789:W / System.err的(26625):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
二月5日至17日:26:50.789:W / System.err的(26625):在java.net.Socket.startupSocket(Socket.java:566)
二月5日至17日:26:50.789:W / System.err的(26625):在java.net.Socket中的<&初始化GT;(Socket.java:225)
二月5日至17日:26:50.789:W / System.err的(26625):在cz.gclient.gardenclient.MainActivity $ ClientThread.run(MainActivity.java:153)
二月5日至17日:26:50.789:W / System.err的(26625):在java.lang.Thread.run(Thread.java:841)
二月5日至17日:26:50.789:W / System.err的(26625):libcore.io.ErrnoException:由造成连接失败:ETIMEDOUT(连接超时)
二月5日至17日:26:50.789:W / System.err的(26625):在libcore.io.Posix.connect(本机方法)
二月5日至17日:26:50.789:W / System.err的(26625):在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
二月5日至17日:26:50.789:W / System.err的(26625):在libcore.io.IoBridge.connectErrno(IoBridge.java:127)
二月5日至17日:26:50.789:W / System.err的(26625):在libcore.io.IoBridge.connect(IoBridge.java:112)
二月5日至17日:26:50.789:W / System.err的(26625):... 6个
连接超时是网络连接问题。有可能是在例如道路的防火墙。这不是一个规划问题,你不能在code解决这个问题。
I'm trying to implement a tcp client app on Android. When I try to connect to my C++ server, the socket times out while trying to connect to the server.
My code:
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();
}
}
}
First, the ERROR1 occurs (socket is null), then the ERROR2 occurs (connection time out). The server is working fine, I have tested it with different clients. I have "uses-permission" so it shouldn't be a problem.
EDIT: stack at 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
'Connection time out' is a network connectivity problem. There may be a firewall in the way for example. It isn't a programming problem and you can't solve it in code.
这篇关于Android的 - 套接字超时而连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!