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

Multi tool use
Multi tool use


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




1 Answer
1



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


readXXX()



However as, for example, an int is normally sent in one go, e.g. 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.


int


writeInt()


int



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().


readFully()


readFully()



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


read()



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.


read(byte)


BufferedInputStream





The reason I want to get the data when the timeout exception is thrown is because I'm doing some moving tests with Wi-Fi Direct technology. Therefore, 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. 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?
– Dani
Jul 1 at 9:29






1. If you want the amount of data transferred, use read(), as I already stated in my answer. 2. If you're talking about UDP, you're not talking about DataInputStream or any of the compound readXXX() methods, you are talking about DatagramSocket.receive(), which returns either a complete UDP datagram or nothing at all: nothing in between.
– EJP
Jul 1 at 10:09


read()


DataInputStream


readXXX()


DatagramSocket.receive()





But if I use read instead of redFully it does not receive all the bytes in the normal send, it seems as if it receives only a few. From 100MB I receive only 10 or 20MB . I send it: DOS.write(bytesMensaje); // bytesMensaje is a byte 100MB DOS.flush(); And with readFully it worked for me. Why does read () behave like that?
– Dani
Jul 2 at 1:46






Could you possible make up your mind. Either you want to know how many bytes were received before the timeout, in which case you must use read(), or you want to fill the buffer, in which case you can use readFully(). Nobody is stopping you from reading in a loop.
– EJP
Jul 2 at 2:55


read()


readFully()





Finally I have sent the 100 MB at once and I receive them with readFully all at once. Later in the cases where I can't receive everything because there are problems in the communication I catch the exception and count the number of 5 received byte by byte and it works properly. Anyway I don´t understand very well the operation of the function read (), the times that I have tried with this function (under ideal conditions) I never received the appropriate number of bytes, with readFully yes.
– Dani
yesterday







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Fh1Rwq pS65,edTqdjqVMIyE i7cSJ6CAKM5M01ogGSRDApCeRxJUSrO3g Qpjr8fH3
JQ70eShQT2Lj,h4kl,J 4Oj HkzaA2OWXRm05KYQvQu8XFp8r 0,yNA11XVHoKzPktgEWCG

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters