我写了这个代码,它在发送者和接收者之间发送图像。
首先,你必须运行接收器然后运行发送器。
当我在1KB到1.5KB之间的图像上测试此代码时,它工作正常,但是当我尝试发送更大的图像时,会显示此消息
无法评估表达式,因为a 本地..........
我的代码如下,有人可以帮忙吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
namespace UPDTester
{
public class UDPClass
{
public Image Merge(Queue<byte[]> myList)
{
int ImgHeight = BitConverter.ToInt32(myList.Dequeue(), 0);
int ImgWidth = BitConverter.ToInt32(myList.Dequeue(), 0);
Bitmap bmp = new Bitmap(ImgWidth, ImgHeight);
Graphics g = Graphics.FromImage(bmp);
int x, y = 0;
while (myList.Count > 0)
{
x = BitConverter.ToInt32(myList.Dequeue(), 0);
y = BitConverter.ToInt32(myList.Dequeue(), 0);
g.DrawImage(ByteToBitmapConverter(myList.Dequeue()), x, y);
}
return bmp;
}
/// <summary>
/// Image Segmentatoin.
/// img: the image that we like to divided.
/// </summary>
public Queue<byte[]> Segmentation(Bitmap img)
{
Queue<byte[]> ByteArray = new Queue<byte[]>();
ByteArray.Enqueue(BitConverter.GetBytes(img.Width));
ByteArray.Enqueue(BitConverter.GetBytes(img.Height));
Image temimg;
for (ushort x = 0; x < img.Width - 5; x += 5)
{
for (ushort y = 0; y < img.Height - 5; y += 5)
{
//temimg = null;
temimg = img.Clone(new Rectangle(x, y, 5, 5), PixelFormat.Format32bppArgb);
ByteArray.Enqueue(BitConverter.GetBytes(x));
ByteArray.Enqueue(BitConverter.GetBytes(y));
ByteArray.Enqueue(ImageToByteConverter(temimg));
}
}
return ByteArray;
}
//Sender
public void SenderUDP(Bitmap img)
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);
MessageBox.Show("Message received from");
MessageBox.Show(Encoding.ASCII.GetString(data, 0, recv));
Queue<byte[]> temlist = Segmentation(img);
data = new byte[1024];
data =temlist.Dequeue();
//Send Width of image.
server.SendTo(data, data.Length, SocketFlags.None, ipep);
data = new byte[1024];
data = temlist.Dequeue();
//Send Height of image.
server.SendTo(data, data.Length, SocketFlags.None, ipep);
data = BitConverter.GetBytes(temlist.Count);
//Send Count of all list.
server.SendTo(data, data.Length, SocketFlags.None, ipep);
MessageBox.Show(temlist.Count.ToString() + " Iam Sender");
while (temlist.Count > 0)
{
server.SendTo(temlist.Dequeue(), Remote);
//MessageBox.Show(temlist.Count.ToString() + "S");
}
//server.Close();
}
//Receiver..(IP, PortNum)
public Image ReceiverUDP()
//public void ReceiverUDP(ref PictureBox pic)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
MessageBox.Show("Waiting for a client....");
//Console.WriteLine("Waiting for a client....");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
MessageBox.Show("Message received from ", Remote.ToString());
MessageBox.Show(Encoding.ASCII.GetString(data, 0, recv));
//Console.WriteLine("Message received from {0}:", Remote.ToString());
//Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
Queue<byte[]> TempList = new Queue<byte[]>();
//Receive Width of image.
newsock.ReceiveFrom(data, ref Remote);
TempList.Enqueue(data);
//Receive Height of image.
newsock.ReceiveFrom(data, ref Remote);
TempList.Enqueue(data);
//reccive Count of the list.
newsock.ReceiveFrom(data, ref Remote);
int count = BitConverter.ToInt32(data, 0);
MessageBox.Show(count.ToString() + " Iam Receiver");
data = new byte[1024];
while (count > 0)
{
data = new byte[1024];
newsock.ReceiveFrom(data, ref Remote);
TempList.Enqueue(data);
data = new byte[1024];
newsock.ReceiveFrom(data, ref Remote);
TempList.Enqueue(data);
data = new byte[1024];
newsock.ReceiveFrom(data, ref Remote);
TempList.Enqueue(data);
MessageBox.Show(count.ToString());
count -= 3;
}
return Merge(TempList);
}
private byte[] ImageToByteConverter(Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
private Image ByteToBitmapConverter(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Image img = Image.FromStream(ms);
return img;
}
}
}
答案 0 :(得分:0)
最有可能的原因是它锁定是因为UDP不能保证交付。数据包可以丢弃。如果发生这种情况,你的接收循环就会坐在那里永远等待一个永远不会来的数据包。