小编0x0*_*Wan的帖子

android - 连接时的套接字超时

我正在尝试在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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

首先,发生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 …
Run Code Online (Sandbox Code Playgroud)

sockets android

6
推荐指数
1
解决办法
8590
查看次数

标签 统计

android ×1

sockets ×1