I am working on an Android project that communicates on TCP / IP.
- Android sends messages to the server via the device socket
- The server sends a reply message on the Android device
this problem is not But I have very attractive questions.
I have no idea how to connect connection interrupts (WiFi, edge, WiFi to edge on open socket, ..) and connection timeout? If Android device sends 1 message and there is a connection problem at the moment - then Android device sends different messages (other requests) - is it a guarantee that the answer will be delivered in the correct sequence?
I tried to set the timeout for the socket object but it did not work. I do not know why, but if I set the timeout for 5 seconds and I had closed the server before sending the message - more than 5 seconds before that the accusation was received.
I did not find any articles on the Internet
The way you get timeout for TCP sockets You can use the Socket Channel () () class to deal with non-blocking sockets in both Android and Android devices using the selection () or the poll (), both of them for a specific period (for example 10 or 20 seconds) Ask for a socket and you can tell if it is worth writing (send you ()) Readable (there is data read). Apart from this, the selection () command will tell you that there is an error in the socket, possibly a broken connection. When you get such error (except for the interrupted signal, it should be ignored and the selection should be reissued), the socket you can do is off and there is no way to open a new account with the server As far as I know, to recover a broken connection, though, if you have implemented in the protocol, then you can resume where the socket was broken. I do not know how you have implemented the protocol, but more of them will need a positive ACK (accept) from the receiver before going with the second message. In addition, when setting up a connection, the customer should specify that this is a new connection or broken and works accordingly.
The idea is:
The sender specifies a specified order to process and the length of the data that is sent after this header, once the receiver is able to process it After the header and the data are received, it sends the sender a positive value to the ACK packet to accept the message, alternatively it is necessary in case of some data if you do not receive ACK packets after the appropriate period. If you are, you can send the same data again until you receive positive ACK.
There may be a situation where the client sends the message, the server receives it and processes it, but when the server sends positive ACK packets, the connection brakes and clients do not get the packet , So after the connection is restored, send the same message again. To avoid this situation, it is necessary to send a message id (an int that is incremental for each message) to identify in the header.
I know that it looks difficult and it really is. If the connection is on the same network (intranet), then it works well, but when the communication is on the internet, you may have to face many problems and situations, which you can not control, so it There is a well defined protocol where you can recover from a broken connection and the transaction / message will not be duplicated.
No comments:
Post a Comment