简体   繁体   English

发生超时异常时,如何获取收到的有效负载量(Android)?

[英]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. 在我正在Android开发的应用程序中,我使用套接字tcp和udp发送数字5的字节。 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. 我想知道是否有可能获得抛出SocketTimeoutException异常之前接收到的有效负载的数量。我正在使用Wi-Fi Direct技术进行一些移动测试,因此,在发送时,由于对等体之前已断开连接。

Also for the case of UDP when packet loss occurs I would like to know the amount of information that I receive. 同样对于UDP,当发生丢包时,我想知道我收到的信息量。

To read what I get, I use readFully and recieve. 要阅读我得到的内容,我使用readFully并接收。 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: TCP接收:

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: UDP接收:

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? 如果在发送TCP和UDP时发生通信中断以及UDP不能收到应有的一切,我怎么知道接收到的信息量?

Thanks 谢谢

You can't if you use any of the compound readXXX() methods, as their APIs don't provide any means of retrieving it. 如果使用任何复合的readXXX()方法,则无法进行操作,因为它们的API不提供任何检索方法。

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. 但是,例如,一个int通常一次性发送,例如通过writeInt()发送,即使您真的很不幸并且分段或打包拆分了它,也几乎没有理由为什么其中一半会比另一半早得多。 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用途总比没有任何用途的多。

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; 类似的考虑也适用于readFully() :大概您正在阅读某些东西,所有这些东西都应该在那儿,所以其中一半没有多大用处。 and conversely, if it would, don't use 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[]) ,选择缓冲区大小,例如8192。或BufferedInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android流式套接字连接上获得超时异常? - How can I get a timeout exception on an android streaming socket connection? 在后台收到通知时,如何让我的android应用执行代码? - How can I get my android app to execute code when a notification is received when it's in background? 如何获取引发自定义异常的类的名称 - How can I get the name of the class that throws a custom made Exception 如何重用引发异常的过程? - How can I reuse a procedure that throws an exception? 使用RequestFuture.get()时Android排名超时异常 - Android volley Timeout Exception when using RequestFuture.get() 当我尝试在Android中创建文件时程序抛出异常 - Program throws an exception when I try to create a file in Android 当一个任务引发异常时,如何获取ScheduledExecutorService来继续执行其他后续任务 - How do i get ScheduledExecutorService to keep executing other subsequent tasks when one task throws an exception jpa异常时如何保存信息 - How I can save information when I get exception in jpa Android - 当收到推送通知 FCM 时,如何更新我的活动中的数据或 UI? - Android - How can I update data or UI in my Activity when Push Notification FCM is received? 当Selenium抛出pageLoadTimeout异常时,如何删除该块? - How do I remove the block when Selenium throws a pageLoadTimeout exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM