Java UDP 发送 - 一一接收数据包 [英] Java UDP send - receive packet one by one
问题描述
我正在尝试通过 UDP 发送和接收一些音频编码数据,但很困惑.
I'm trying to send and receive some Audio encoded data via UDP, and confused.
首先,我之前在C#和node.js上做过UDP的send-receive,一个接一个的接收数据包非常简单直接,但是在Java中就不一样了,让我很困惑.
First of all, I have done UDP send-receive before on C# and node.js, and receiving the packet one by one is very straight forward and easy, but it's different in Java, and confusing me.
每个编码的数据大小约为 360-380 字节
,
The every encoded data size is approximately 360-380 bytes
for each,
并且我需要通过 UDP 将此编码的数据作为数据包发送到远程(或本地主机到目前为止).
and I need to send this encoded data as a packet to remote(or localhost so far) via UDP.
UDP 接收器应接收作为数据包的编码数据并进行解码处理.
The UDP-receiver should receive the encoded data as a packet and process to decode.
这是一个简单的例子来说明我的工作.
Here is a brief example to illustrate what I do.
编码器和 UDP 发送方
ds = new DatagramSocket(localPort);
//a Thread with While loop----------
Log.d("AudioEncoder", outData.length + " bytes encoded");
//The encoded data size is about 370 bytes for each
packet = new DatagramPacket(outData, outData.length,
InetAddress.getByName("127.0.0.1"), localPort);
ds.send(packet);
UDP 接收器和解码器
ds = new DatagramSocket(localPort);
byte[] buffer2 = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer2, buffer2.length);
while (isPlaying)
{
ds.receive(packet);
Log.d("UDP-receiver", buffer2.length + " bytes received");
//do Decoding on buffer2
//..........
}
基本上,每个要发送和接收的数据包(编码字节数据)大小约为 360-380 字节.
Basically, a packet (encoded byte data) size to send and receive is approximately 360-380 bytes for each.
然后,我理解对于 Java Datagram
,为特定的字节大小制作一个缓冲区,例如 512 或 1024
(我不知道什么大小在在这种情况下,以及在这些天).
Then, I understand for Java Datagram
, make a buffer for a specific byte size such as 512 or 1024
(I don't know what size is most optimized in this case, and in these days).
如果我设置DatagramPacket
大小1024 bytes
,360-380 bytes
的数据包会出现两次被缓冲到池中,那么原来的 2 个数据包被连接在一起,然后作为一个单字节数组处理?
If I set the DatagramPacket
size 1024 bytes
, the packet which size is 360-380 bytes
comes twice to be pooled in the buffer, then the original 2 packets are joind together then processed as a single byte array?
如果我设置DatagramPacket
大小512 bytes
,360-380 bytes
的数据包会两次进入缓冲区,那么既然溢出了缓冲区大小,那么到达的单个数据包会被刷屏处理吗?
If I set the DatagramPacket
size 512 bytes
, the packet which size is 360-380 bytes
comes twice to be pooled in the buffer, then since it's overflowed the buffer size, the single packet arrived is swiped to be processed?
再说一遍,至少足够了,接收者可以一个一个地接收一个数据包,然后对每个数据包进行处理,但这里的事情看起来很复杂.
Again, it's sufficient at least, the receiver can receive a packet one by one, then process for each, but things looks complicated here.
请指教.谢谢.
推荐答案
如果我将DatagramPacket的大小设置为1024字节,那么大小为360-380字节的数据包会两次进入缓冲池,然后将原来的2个数据包连接在一起作为一个单字节数组进行处理?
If I set the DatagramPacket size 1024 bytes, the packet which size is 360-380 bytes comes twice to be pooled in the buffer, then the original 2 packets are joind together then processed as a single byte array?
没有.您会收到两个单独的数据包.
No. You receive two separate packets.
如果我将DatagramPacket 的大小设置为512 字节,那么大小为360-380 字节的数据包会两次进入缓冲池
If I set the DatagramPacket size 512 bytes, the packet which size is 360-380 bytes comes twice to be pooled in the buffer
没有
那么既然已经溢出了缓冲区大小,那么到达的单个数据包会被刷屏处理吗?
then since it's overflowed the buffer size, the single packet arrived is swiped to be processed?
我不知道刷卡"是什么意思,但这一切都没有发生.您会收到两个单独的数据包.
I don't know what 'swiped' means, but none of this happens. You receive two separate packets.
这篇关于Java UDP 发送 - 一一接收数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!