【发布时间】:2015-06-25 04:21:11
【问题描述】:
我的应用程序使用 UdpClient 从其他机器接收图像。
每个图像大小为 951000 字节,MTU 限制为 1500 字节。
所以发送方应用程序必须使用分片...并且每个发送包都包含包含 2 个 int 的标头
- 总数
- current_number
接收字节的代码..这是非常密集的比特率,因为视频每 30 毫秒就有一个新帧要发送到我的应用程序..
我发现自己丢失了包裹,我不知道该怎么做才能不丢失包裹。
有人知道如何解决这个问题吗? 有没有更好的方法?
这是代码
public class PackagePartial
{
public int total_count;
public int current_count; // first package is 1
public byte[] buffer;
public byte[] Serializable()
{
// make the Serialize
}
public static void DeSerializable(byte[] v)
{
total_count = ... ;
current_count = ...
buffer = ...
}
}
// the network layer
int lastPackIndex = 0;
List<byte> collection = new List<byte>();
while(true)
{
byte[] package = _clientListener.Receive(ref ep);
PackagePartial p = PackagePartial.DeSerializable(package);
// indication that i lost package
if(p.current_count - lastPackIndex != 1 )
{
collection.Clear();
lastPackIndex = 0
continue;
}
if(p.current_count == p.total_count)
{
// image Serialize and send it to the GUI layer as bitmap
Image img = ConvertBytesToImage(collection);
SendToGui(img);
collection.Clear();
lastPackIndex = 0
}
else
{
lastPackIndex = p.current_count
collection.AddRange(p.Buffer)
}
【问题讨论】:
-
通常的方法是采用压缩(例如 H.264)来减小发送数据的大小。
-
没有办法压缩 - 这是我无法控制和处理的东西:(
-
尝试显着增加 listener.Client.ReceiveBufferSize
-
@Yanshof:我不相信你。
-
Graffito@ 谢谢...我尝试了,但这无济于事