我的客户以高速率发送 udp 数据包。我确定我的 java 应用程序层没有收到客户端发送的所有 udp 数据包,因为wireshark 中收到的数据包数量与我的 java 应用程序不匹配。因为wireshark接收更多的udp数据包所以我确定udp数据包没有在网络中丢失。
代码在这里:
在一个线程中接收数据包并提供给一个LinkedBlockingQueue线程,在另一个线程上消费从一个 rx-java 主题中获取数据包LinkedBlockingQueue,然后调用onNext它。
socket = new DatagramSocket(this.port);
socket.setBroadcast(true);
socket.setReceiveBufferSize(2 * 1024 * 1024);
// thread-1
while (true) {
byte[] bytes = new byte[532];
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
try {
this.socket.receive(packet);
queue.offer(
new UdpPacket(
packet.getPort(), packet.getAddress().getHostAddress(), packet.getData()));
} catch (IOException e) {
e.printStackTrace();
}
}
// thread-2
UdpPacket packet;
while ((packet = queue.take()) != null) {
this.receiveMessageSubject.onNext(packet);
}
主机操作系统: Ubutnu 18.04
jeck猫
慕仙森
相关分类