title: How can I get the amount of payload received when an timeout exception throws (Android)?
body: <p>In the application that I am developing in Android, I send bytes of number 5 using sockets tcp and udp. I would like to know if it is possible to get the amount of payload that was received until a SocketTimeoutException exception was thrown.I'm doing some moving tests with Wi-Fi Direct technology so, when sending, all may not be received because the peers are disconnected before.</p>
  
  <p>Also for the case of UDP when packet loss occurs I would like to know the amount of information that I receive. </p>
  
  <p>To read what I get, I use readFully and recieve. This reception is done in a single step or in a loop in which I receive large amounts of information. I could not receive the bytes one by one because it would be really slow.</p>
  
  <p>TCP RX:</p>
  
  <pre><code>ServerSocket serverSocket = new ServerSocket(SERVERPORT);
  Socket client = serverSocket.accept();
  DataInputStream DIS = new DataInputStream(client.getInputStream());
  int tamMensaje = (100 * 1000 * 1000);
  byte[] payload = new byte[tamMensaje];
  DIS.readFully(payload);
  int failures = 0;
  for (int i = 0; i &lt; tamMensaje; i++) {
     if (payload[i] != 5) {
        failures = failures + 1;
     }
  }
  int nPayLoad = payload.length- failures;
  client.close(); 
  serverSocket.close();
  </code></pre>
  
  <p>UDP RX:</p>
  
  <pre><code>DatagramSocket datagramSocket = new DatagramSocket(SERVERPORT);
  byte[] buffer = new byte[1400];
  DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  int tamMensaje = 1400 *71428;
  int iteration = 71428;
  for (int i = 0; i &lt; iteration; i++) {
     datagramSocket.receive(packet);
      msg_received = msg_received + new String(buffer, 0, packet.getLength());
  }
  byte[] payload = msg_received.getBytes();
  int failures = 0;
  for (int i = 0; i &lt; tamMensaje; i++) {
     if (payload[i] != 5) {
        failures = failures + 1;
                          }
                      }
   int nPayload = payload.length- failures;
   datagramSocket.close();
  </code></pre>
  
  <p>How can I know the amount of information received if the communication is cut off when sending for TCP and UDP occurs as well as in case UDP does not receive everything it should?</p>
  
  <p>Thanks</p>