Android socket connection timeout

后端 未结 2 1865
無奈伤痛
無奈伤痛 2021-01-05 09:43

My android app is connected to the server through socket, which is coded in node.js. When the is left in the foreground for 15 minutes it losses connection to the server. Th

2条回答
  •  庸人自扰
    2021-01-05 10:00

    This is a simple example that shows how to set the timeout on a java socket :

    sockAdr = new InetSocketAddress(SERVER_HOSTNAME, SERVER_PORT);
    socket = new Socket();
    timeout = 5000;
    socket.connect(sockAdr, timeout);
    reader = new BufferedReader(new InputStreamReader(socket.getInputStream());
    while ((data = reader.readLine())!=null) 
          log.e(TAG, "received -> " + data);
    log.e(TAG, "Socket closed !");
    

提交回复
热议问题