Home  >  Q&A  >  body text

Java 判断socket的连接状态

1.android 设备在未有设备连接的时候,怎么判断socket是未连接,如果使用API提供的isConnected()的方法会报一个空指针

2.当socket连接的设备突然断开怎么实时获取socket的状态

3在进行socket通信的时候会出现可以发送数据,却收不到数据,如果处理

伊谢尔伦伊谢尔伦2765 days ago608

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:39:56

    1. Is the server enabled on your device? , if yes, then ServerSocket.accept()方法是阻塞的,你的判断代码不要写在这个方法成功前就创建好的其他线程内,要与ServerSocket.accept()在同一线程或者在 ServerSocket.accept() 执行后新开的线程内,以保证判断方法也会被阻塞影响。 ——另外判断连接状态用Socket.isClosed().

    2. There is no way to achieve complete real-time. You can look at the information on the heartbeat mechanism. Generally, this method of continuously sending simple data is used to determine whether the remote end is online.

    3. Same as the heartbeat mechanism above, you can ask the receiving end to return a result mark to you after the data is sent successfully.

    reply
    0
  • Cancelreply