There is a server and an android device on the same WI-FI network. Android creates a connection (socket) with the server and sends data. How to determine on an android device if the TCP connection is interrupted? After all, the android will continue to send info.

UPADTE: An interesting observation is that the methods of the class Socket isClosed and isConnected do not work as one might think. isConnected - if the connection has been created, the method will always return true isClosed - will return false only when the close() method is called from the client, not the server. We continue the search ..

UPDATE 2: Found an interesting checkError() method in PrinterWriter , which PrinterWriter True if it is not possible to send a message over a TCP connection. But here are its pitfalls: If the connection is broken, the android sends a message, for the first time checkError() returns false , and true only the next times (provided that the connection is still broken).

  • By timeout There is still keepalive, but it is not obligatory to use. So the surest way is timeout. - Sergey
  • @Sergey timeout should be checked in service or can I set a message when sending a message to the server? How to properly implement a gap check? - ivanovd422
  • It is necessary to check on the side where it is especially important. On the server itself, it is not necessary to keep the "hung" connection. On the client, if it is some kind of autonomous is the same. If the application is interactive (implies communication with the user), then there probably is not necessary. Even the most stupid will guess to restart if that. - Sergey
  • I have a slightly different task: there is a server and a client, they are connected using a socket. The client sends messages to the server, but suddenly the server crashes. How can the client know that the server has crashed? After all, if the client continues to send messages, he will not know anything .. - ivanovd422
  • I don’t even know about the program. It is necessary to make a confirmation of receipt. TCP itself has confirmation of packets, but I don’t remember whether a violation of this protocol results in an error on the socket in the program. In theory, it should, he can not send forever without confirmation of the packages on the other side. Need to smoke manuals. - Sergey

0