简体   繁体   中英

How can I get the amount of payload received when an timeout exception throws (Android)?

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.

Also for the case of UDP when packet loss occurs I would like to know the amount of information that I receive.

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.

TCP RX:

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 < tamMensaje; i++) {
   if (payload[i] != 5) {
      failures = failures + 1;
   }
}
int nPayLoad = payload.length- failures;
client.close(); 
serverSocket.close();

UDP RX:

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 < 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 < tamMensaje; i++) {
   if (payload[i] != 5) {
      failures = failures + 1;
                        }
                    }
 int nPayload = payload.length- failures;
 datagramSocket.close();

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?

Thanks

You can't if you use any of the compound readXXX() methods, as their APIs don't provide any means of retrieving it.

However as, for example, an int is normally sent in one go, eg by writeInt() , there is really little reason why half of it would arrive long before the other half, even if you got really unlucky and segmentation or packetization split it. It could happen, but you would have to be really be extraordinarily unlucky, and in any case half an int is no more use than none of it.

Similar considerations apply to readFully() : presumably you are reading something, all of which is supposed to be there, so half of it wouldn't be of much use; and conversely, if it would, don't use readFully() .

If you use the basic read() method, the answer of course is zero: nothing arrived before the timeout.

I could not receive the bytes one by one because it would be really slow.

Of course, but that's not the only alternative. Use read(byte[]) , with a buffer size of your choice, say 8192. Or a BufferedInputStream .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM