我的问题在于通过udp发送图像字节,我严格地模仿了tcp而制作了一个api,但它也像udp一样工作,我想要的就是将图像拆分成块并将每个图像发送到服务器,最后一个长度是“剩下的”
客户端(发送方)
using (var stream = new MemoryStream())
{
Frame.Save(stream, ImageFormat.Png);
byte[] ImageBuffer = stream.ToArray();
byte[] SendBuffer = new byte[400];
Client.QuickMessage("SI", ImageBuffer.Length); // Start Image, buffer length
int lastPacketPos = 0;
for (int lastCounter = 0; lastCounter <= ImageBuffer.Length - 400; lastCounter += 400)
{
Buffer.BlockCopy(ImageBuffer, lastCounter, SendBuffer, 0, 400);
Client.SecureMessage("IP", SendBuffer, true); // Image Packet, buffer
lastPacketPos = lastCounter;
Thread.Sleep(1);
}
SendBuffer = new byte[ImageBuffer.Length - lastPacketPos];
Buffer.BlockCopy(ImageBuffer, lastPacketPos, SendBuffer, 0, ImageBuffer.Length - (lastPacketPos));
Client.SecureMessage("LP", SendBuffer, true); // Last Packet, buffer
}
服务器端(接收方)
byte[] ImageMemory;
int bitCounter = 0;
ShareScreenWindow ClientShareWindow = new ShareScreenWindow();
private void Server_onReceivedData(string ClientName, string Message, object Obj)
{
if (Message == "SI")
{
ImageMemory = new byte[(int)Obj];
Console.WriteLine("R " + (int)Obj);
bitCounter = 0;
}
else if (Message == "IP")
{
Buffer.BlockCopy((byte[])Obj, 0, ImageMemory, bitCounter, ((byte[])Obj).Length);
bitCounter += ((byte[])Obj).Length;
Console.WriteLine(bitCounter);
}
else if (Message == "LP")
{
Buffer.BlockCopy((byte[])Obj, 0, ImageMemory, bitCounter, ((byte[])Obj).Length);
using (MemoryStream ImageStream = new MemoryStream(ImageMemory))
{
ClientShareWindow.ShareImage.Image = Image.FromStream(ImageStream);
ImageStream.Dispose();
}
}
}
到目前为止没有任何效果,由于计数大于服务器端的缓冲区大小,或者发生drawing.dll错误,我遇到了各种错误,请帮忙!
消息按顺序发送,这不是问题