Tutorial :Java Sockets and Dropped Connections



Question:

What's the most appropriate way to detect if a socket has been dropped or not? Or whether a packet did actually get sent?

I have a library for sending Apple Push Notifications to iPhones through the Apple gatways (available on GitHub). Clients need to open a socket and send a binary representation of each message; but unfortunately Apple doesn't return any acknowledgement whatsoever. The connection can be reused to send multiple messages as well. I'm using the simple Java Socket connections. The relevant code is:

Socket socket = socket();   // returns an reused open socket, or a new one  socket.getOutputStream().write(m.marshall());  socket.getOutputStream().flush();  logger.debug("Message \"{}\" sent", m);  

In some cases, if a connection is dropped while a message is sent or right before; Socket.getOutputStream().write() finishes successfully though. I expect it's due to the TCP window isn't exhausted yet.

Is there a way that I can tell for sure whether a packet actually got in the network or not? I experimented with the following two solutions:

  1. Insert an additional socket.getInputStream().read() operation with a 250ms timeout. This forces a read operation that fails when the connection was dropped, but hangs otherwise for 250ms.

  2. set the TCP sending buffer size (e.g. Socket.setSendBufferSize()) to the message binary size.

Both of the methods work, but they significantly degrade the quality of the service; throughput goes from a 100 messages/second to about 10 messages/second at most.

Any suggestions?

UPDATE:

Challenged by multiple answers questioning the possibility of the described. I constructed "unit" tests of the behavior I'm describing. Check out the unit cases at Gist 273786.

Both unit tests have two threads, a server and a client. The server closes while the client is sending data without an IOException thrown anyway. Here is the main method:

public static void main(String[] args) throws Throwable {      final int PORT = 8005;      final int FIRST_BUF_SIZE = 5;        final Throwable[] errors = new Throwable[1];      final Semaphore serverClosing = new Semaphore(0);      final Semaphore messageFlushed = new Semaphore(0);        class ServerThread extends Thread {          public void run() {              try {                  ServerSocket ssocket = new ServerSocket(PORT);                  Socket socket = ssocket.accept();                  InputStream s = socket.getInputStream();                  s.read(new byte[FIRST_BUF_SIZE]);                    messageFlushed.acquire();                    socket.close();                  ssocket.close();                  System.out.println("Closed socket");                    serverClosing.release();              } catch (Throwable e) {                  errors[0] = e;              }          }      }        class ClientThread extends Thread {          public void run() {              try {                  Socket socket = new Socket("localhost", PORT);                  OutputStream st = socket.getOutputStream();                  st.write(new byte[FIRST_BUF_SIZE]);                  st.flush();                    messageFlushed.release();                  serverClosing.acquire(1);                    System.out.println("writing new packets");                    // sending more packets while server already                  // closed connection                  st.write(32);                  st.flush();                  st.close();                    System.out.println("Sent");              } catch (Throwable e) {                  errors[0] = e;              }          }      }        Thread thread1 = new ServerThread();      Thread thread2 = new ClientThread();        thread1.start();      thread2.start();        thread1.join();      thread2.join();        if (errors[0] != null)          throw errors[0];      System.out.println("Run without any errors");  }  

[Incidentally, I also have a concurrency testing library, that makes the setup a bit better and clearer. Checkout the sample at gist as well].

When run I get the following output:

Closed socket  writing new packets  Finished writing  Run without any errors  


Solution:1

This not be of much help to you, but technically both of your proposed solutions are incorrect. OutputStream.flush() and whatever else API calls you can think of are not going to do what you need.

The only portable and reliable way to determine if a packet has been received by the peer is to wait for a confirmation from the peer. This confirmation can either be an actual response, or a graceful socket shutdown. End of story - there really is no other way, and this not Java specific - it is fundamental network programming.

If this is not a persistent connection - that is, if you just send something and then close the connection - the way you do it is you catch all IOExceptions (any of them indicate an error) and you perform a graceful socket shutdown:

1. socket.shutdownOutput();  2. wait for inputStream.read() to return -1, indicating the peer has also shutdown its socket  


Solution:2

After much trouble with dropped connections, I moved my code to use the enhanced format, which pretty much means you change your package to look like this:

enter image description here

This way Apple will not drop a connection if an error happens, but will write a feedback code to the socket.


Solution:3

If you're sending information using the TCP/IP protocol to apple you have to be receiving acknowledgements. However you stated:

Apple doesn't return any acknowledgement whatsoever

What do you mean by this? TCP/IP guarantees delivery therefore receiver MUST acknowledge receipt. It does not guarantee when the delivery will take place, however.

If you send notification to Apple and you break your connection before receiving the ACK there is no way to tell whether you were successful or not so you simply must send it again. If pushing the same information twice is a problem or not handled properly by the device then there is a problem. The solution is to fix the device handling of the duplicate push notification: there's nothing you can do on the pushing side.

@Comment Clarification/Question

Ok. The first part of what you understand is your answer to the second part. Only the packets that have received ACKS have been sent and received properly. I'm sure we could think of some very complicated scheme of keeping track of each individual packet ourselves, but TCP is suppose to abstract this layer away and handle it for you. On your end you simply have to deal with the multitude of failures that could occur (in Java if any of these occur an exception is raised). If there is no exception the data you just tried to send is sent guaranteed by the TCP/IP protocol.

Is there a situation where data is seemingly "sent" but not guaranteed to be received where no exception is raised? The answer should be no.

@Examples

Nice examples, this clarifies things quite a bit. I would have thought an error would be thrown. In the example posted an error is thrown on the second write, but not the first. This is interesting behavior... and I wasn't able to find much information explaining why it behaves like this. It does however explain why we must develop our own application level protocols to verify delivery.

Looks like you are correct that without a protocol for confirmation their is no guarantee the Apple device will receive the notification. Apple also only queue's the last message. Looking a little bit at the service I was able to determine this service is more for convenience for the customer, but cannot be used to guarantee service and must be combined with other methods. I read this from the following source.

http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/

Seems like the answer is no on whether or not you can tell for sure. You may be able to use a packet sniffer like Wireshark to tell if it was sent, but this still won't guarantee it was received and sent to the device due to the nature of the service.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »