[英]Server do not receive all messages
I have 2 computers: I would call them Comp A, Comp B; 我有两台计算机:我称它们为Comp A,Comp B;
I have to: 我必须:
To send the file I use the function 要发送文件,我使用功能
socket.SendTo(packet,0,count,SocketFlags.None,remoteEP);
from System.Net.Sockets
. 从
System.Net.Sockets
。
As a result I concluded that the file was being transmitted precisely. 结果,我得出的结论是文件正在精确传输。 I monitor it using Wireshark on Comp A and Comp B. However packets of bytes coming to the Comp B, don't coincide at all with the file being transmitted.
我在组件A和组件B上使用Wireshark对其进行监视。但是,到达组件B的字节数据包与正在传输的文件完全不符。
The program which sends data of the file, opens this file in a right way. 发送文件数据的程序以正确的方式打开该文件。 Then it passes right bytes of the source PCM file to the function
Socket.SendTo(...)
. 然后,它将源PCM文件的右字节传递给函数
Socket.SendTo(...)
。 But Wireshark of Comp A (output) displays absolutely incorrect bytes ie Comp A sends incorrect bytes. 但是Comp A(输出)的Wireshark显示绝对不正确的字节,即Comp A发送不正确的字节。
What could be the problem? 可能是什么问题呢?
I figured out that function socket.SendTo(packet,0,count,SocketFlags.None,remoteEP);
我想出了功能
socket.SendTo(packet,0,count,SocketFlags.None,remoteEP);
sends correct bytes if I send them with a delay. 如果延迟发送,则会发送正确的字节。 I mean I can send 400 bytes (without loops) and my program sends 400 absolutely precise, correct bytes.
我的意思是我可以发送400个字节(无循环),而我的程序可以发送400个绝对精确,正确的字节。
But I have a big PCM file. 但是我有一个很大的PCM文件。 Its size is about 50 Mb.
它的大小约为50 Mb。 Its duration is 1 minute.
持续时间为1分钟。 I have to send this file during a minute so that this file would be transmitted evenly, uniformly.
我必须在一分钟内发送此文件,以便该文件被均匀均匀地传输。 It means about 800 Kb needs to be transmitted per a second.
这意味着每秒需要传输约800 Kb。
So here is my program code. 这是我的程序代码。 I send every 800 Kb per second using timer function being called 2 times per second.
我使用称为每秒2次的计时器功能每秒发送800 Kb。
private void m_pTimer_Tick(object sender,EventArgs e)
{
uint sent_data = 0;
while ((sent_data <= (BUFFERSIZE / 120)) && ((num * RAW_PACKET) + sent_data < BUFFERSIZE))
{
uint bytes_count = ((BUFFERSIZE - (RAW_PACKET * num)) > RAW_PACKET) ? RAW_PACKET : (BUFFERSIZE - (RAW_PACKET * num));
byte[] buffer = new byte[bytes_count];
Array.Copy(ReadBuffer, num * RAW_PACKET, buffer, 0, bytes_count);
num++;
// Send and read next.
m_pUdpServer.SendPacket(buffer, 0, Convert.ToInt32(bytes_count), m_pTargetEP);
sent_data += bytes_count;
}
if ((num * RAW_PACKET) + sent_data == BUFFERSIZE)
{
m_pTimer.Enabled = false;
}
m_pPacketsReceived.Text = m_pUdpServer.PacketsReceived.ToString();
m_pBytesReceived.Text = m_pUdpServer.BytesReceived.ToString();
m_pPacketsSent.Text = m_pUdpServer.PacketsSent.ToString();
m_pBytesSent.Text = m_pUdpServer.BytesSent.ToString();
}
If I call a function m_pUdpServer.SendPacket(buffer, 0, Convert.ToInt32(bytes_count), m_pTargetEP);
如果我调用一个函数
m_pUdpServer.SendPacket(buffer, 0, Convert.ToInt32(bytes_count), m_pTargetEP);
without a timer or any loops(while, etc.) I see correct result on output. 没有计时器或任何循环(while等),我在输出中看到正确的结果。
Well here 120 - a number of file parts that are being transmitted for a period of every timer function call. 好吧,这里120-在每个计时器函数调用的一段时间内正在传输的许多文件部分。 The timer function is called 2 times per second.
定时器功能每秒被调用2次。
BUFFERSIZE
is a total file size. BUFFERSIZE
是文件的总大小。 ReadBuffer
is an array that contains all PCM file data. ReadBuffer
是一个包含所有PCM文件数据的数组。 RAW PACKET
= 400 bytes. RAW PACKET
= 400字节。 sent_data
is a total byte count being sent within every timer function call. sent_data
是每个计时器函数调用中发送的总字节数。 num
is a total count of sent packets. num
是已发送数据包的总数。 I suppose there is too many packets(bytes) to be sent within a timer function call. 我想在计时器函数调用中有太多数据包(字节)要发送。 Therefore I see incorrect values on output.
因此,我在输出中看到不正确的值。
So what is a solution of this problem? 那么该问题的解决方案是什么?
I think I can make up a RTP packet (to add a sequence number to every sent packet). 我想我可以组成一个RTP数据包(为每个发送的数据包添加一个序列号)。 It will help me to identify received packets and to make up a correct sequence of received packets.
这将有助于我识别接收到的数据包并正确组成接收到的数据包的顺序。 It can help me if received packets have a correct byte sequence.
如果收到的数据包具有正确的字节序列,它可以为我提供帮助。 Because if received packets have a mixed byte order(sequence) I don't understand how to restore a correct byte sequence in every received packet.
因为如果接收到的数据包具有混合的字节顺序(序列),我不明白如何在每个接收到的数据包中恢复正确的字节序列。
I was advised to refuse the timer call and to send packets evenly, uniformly using a synchronization by time. 建议我拒绝计时器调用,并均匀地使用时间同步发送数据包。 Actually I don't know how to do it.
其实我不知道该怎么做。 Maybe I should use threads, pool of threads or something like that.
也许我应该使用线程,线程池之类的东西。 What do you think?
你怎么看?
The only guarantee UDP gives you is that the entire message is delivered. UDP给您的唯一保证是整个消息都已传递。 However, if you send piece 1,2,3,4 in order they may be received in any order, for instance 4132. That is, UDP do not guarantee ordering.
但是,如果按顺序发送片段1、2、3、4,则它们可以以任何顺序接收,例如4132。也就是说,UDP不保证顺序。
You MUST include a sequence number to be able to store the PCM correctly. 您必须包括一个序列号才能正确存储PCM。
UDP do not guarantee delivery either. UDP也不能保证传递。 If the server receive piece #4 but not #5 within X seconds it should probably request that piece again.
如果服务器在X秒内收到#4而不是#5,则可能应该再次请求该件。
Or you'll just switch to TCP. 或者,您仅切换到TCP。 Much easier.
容易得多。 All you need is some way to tell the length of the file and then just transfer it.
您所需要的只是某种方式来确定文件的长度,然后直接传输它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.