精确到字节后发现少了4000的字节,怎么会少的?丢包?还是其他什么原因? 代码如下 private void Listen() { int port = 8889; UdpClient listener = new UdpClient(port); int m_intSerial = 0; //数据包顺序号 int m_intBlocks = 0; //数据分割块数 int m_intLength = 0; //数据长度 int m_intMessageLength = 0; int diushi=0; int place = 0; int m_intGetCount = 0; int m_intBlockLength = 1000; IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.110"), 8889); try {
while (true) { byte[] bytesF = listener.Receive(ref iep); place = 0; m_intSerial = BitConverter.ToInt32(bytesF, place);//获得本次包顺序号 place += 4; m_intBlocks = BitConverter.ToInt32(bytesF, place);//获得数据总包数 place += 4; m_intLength = BitConverter.ToInt32(bytesF, place);//获得本次包数据长度 place += 4; m_intMessageLength = BitConverter.ToInt32(bytesF, place);//获得总数据长度 place += 4; byte[] data = new byte[m_intMessageLength]; //申明数据的总长 Array.Copy(bytesF, 16, data, m_intBlockLength * m_intSerial, m_intLength); //复制本包数据 //m_intGetCount = m_intGetCount + 1; Application.DoEvents(); if (m_intBlocks > 1) { while (true) { if (m_intGetCount >= m_intBlocks) { break; } byte[] bytes = listener.Receive(ref iep); place = 0; m_intSerial = BitConverter.ToInt32(bytes, place); place += 4; m_intBlocks = BitConverter.ToInt32(bytes, place); place += 4; m_intLength = BitConverter.ToInt32(bytes, place); place += 4; m_intMessageLength = BitConverter.ToInt32(bytes, place); place += 4; Array.Copy(bytes, 16, data, m_intBlockLength * m_intSerial, m_intLength); //复制数据 m_intGetCount = m_intGetCount + 1;
代码如下
private void Listen()
{
int port = 8889; UdpClient listener = new UdpClient(port);
int m_intSerial = 0; //数据包顺序号
int m_intBlocks = 0; //数据分割块数
int m_intLength = 0; //数据长度
int m_intMessageLength = 0;
int diushi=0;
int place = 0;
int m_intGetCount = 0;
int m_intBlockLength = 1000;
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.110"), 8889); try
{
while (true)
{
byte[] bytesF = listener.Receive(ref iep);
place = 0;
m_intSerial = BitConverter.ToInt32(bytesF, place);//获得本次包顺序号
place += 4;
m_intBlocks = BitConverter.ToInt32(bytesF, place);//获得数据总包数
place += 4;
m_intLength = BitConverter.ToInt32(bytesF, place);//获得本次包数据长度
place += 4;
m_intMessageLength = BitConverter.ToInt32(bytesF, place);//获得总数据长度
place += 4; byte[] data = new byte[m_intMessageLength]; //申明数据的总长 Array.Copy(bytesF, 16, data, m_intBlockLength * m_intSerial, m_intLength); //复制本包数据 //m_intGetCount = m_intGetCount + 1;
Application.DoEvents();
if (m_intBlocks > 1)
{
while (true)
{
if (m_intGetCount >= m_intBlocks)
{
break;
}
byte[] bytes = listener.Receive(ref iep);
place = 0;
m_intSerial = BitConverter.ToInt32(bytes, place);
place += 4;
m_intBlocks = BitConverter.ToInt32(bytes, place);
place += 4;
m_intLength = BitConverter.ToInt32(bytes, place);
place += 4;
m_intMessageLength = BitConverter.ToInt32(bytes, place);
place += 4;
Array.Copy(bytes, 16, data, m_intBlockLength * m_intSerial, m_intLength); //复制数据
m_intGetCount = m_intGetCount + 1;
progressBar1.Value = (int)(m_intGetCount / m_intBlocks * 100);
Application.DoEvents();
// MessageBox.Show("共有数据包" + m_intBlocks + "个,得到" + m_intGetCount + "个", "收d到数据检测"); }
}
File.WriteAllBytes("E:\\1.mp3", data);
MessageBox.Show("共有数据包" + m_intBlocks + "个,得到" + m_intGetCount + "个", "收到数据检测");
if (m_intGetCount >= m_intBlocks)
{
break;
}
}
}
catch (System.Exception pe)
{
Console.WriteLine(pe.ToString());
}
}
BinaryWriter bw=new BinaryWiter(fs);
bw.Write(data);
这样写吗?试了不行,另外我在读文件的时候 如果不传输,而直接再在本机上写出来一个文件就行,只要传输出去再接收就打不开了