Reputation: 43
I have already googled and searched stackoverflow but I can find nothing related to my problem.
First of all, let me describe briefly the scene.
Imagine a camera sending all the time pieces (from now on I will call them 'chunks') of JPEG through the network by UDP. Using UDP is a constraint, I cannot change that, so please do not answer 'why don't you use TCP?', because you know my point: 'Because I can't'.
On the other side a client receives the chunks sent by the cam. To build a kind of flow control I have set three fields (three bytes to store JPEG number, chunk index, amount of chunks) at the beginning of my datagram, and after that the chunk itself.
By the moment I am testing both sides in my own laptop sending and receiving through the loopback (127.0.0.1) but the problem is that while the camera (sender) says it has sent all the chunks propperly (I am testing with a single JPEG picture that gets splitted in 161 chunks) the client receives a random number of pieces (sometimes near 70, some others 100, and a few times all of them). I have tried sniffing my loopback with rawcap (http://www.netresec.com/?page=RawCap) and it detects another amount of UDP 'datagrams', different from 161 (which is supposed to be sent), and from the amount the client claims to have received.
So, is it possible that the send sentence is not working as expected? Any other suggestion to continue investigation?
Here is my sending method.
private void startSendingData(IPEndPoint target)
{
fatherForm.writeMessage("START command received. Sending data to " + target.Address.ToString() + " port " + target.Port.ToString() + "...");
//Get chunks prepared
List<byte[]> listOfChunks = getChunksInAList(collectFiles());
List<uint> chunksSucces = new List<uint>();
List<uint> chunksFailed = new List<uint>();
byte[] stopDatagram = getStopDatagram();
//Initialise the socket
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Stopwatch sw = Stopwatch.StartNew();
foreach (byte[] chunk in listOfChunks)
{
if (s.SendTo(chunk, target) == chunk.Length)
chunksSucces.Add(BitConverter.ToUInt32(chunk,sizeof(uint)));
else
chunksFailed.Add(BitConverter.ToUInt32(chunk, sizeof(uint)));
}
Debug.WriteLine(chunksSucces.Count + " sent successfully");
//Tell the receiver not to continue receiving
s.SendTo(stopDatagram, target);
long ellapsedMs = sw.ElapsedMilliseconds;
sw.Stop();
writeTransmissionRate(listOfChunks.Count, ellapsedMs);
Debug.WriteLine(sw.ElapsedMilliseconds + "ms ellapsed");
sw.Stop();
s.Close();
}
And the output is:
161 chunks to be sent
161 sent successfully
6ms ellapsed
Transmission rate: 37000.65KB/s 36.13MB/s
But at the other side I only recieve 22 datagrams in this test.
Thank you in advance.
Upvotes: 3
Views: 1646
Reputation: 21251
UDP is not guaranteed. The network stack may be dropping packets if your receive function is not processing them fast enough.
Try sending them slower, or increasing your UDP receive buffer:
Upvotes: 3