0
votes

I am Sending 68bytes of data using UDP Protocol.

68bytes of data consist of 4byte of int and random 64byte of byte array. uint seq starts with zero and it will increase if client send datagram to server once at a time. I used BitConverter.GetBytes for data's seq to make byte array.

public class NewData
{
    public uint seq;
    public byte[] data = new byte[64];

    public NewData()
    {
        seq = 0;
        data = null;
    }

    public NewData(uint seq, byte[] data)
    {
        this.seq = seq;
        this.data = data;
    }
}

Server has 2 Threads. 1 Thread will Enqueue and the other thread will Dequeue. (Producer and Consumer Thread)

I tried to check the data is coming well.

private void ReceiveThread()
{
    int recv;
    uint seq = 0;
    byte[] datagram = new byte[1024];
    List<byte> list = new List<byte>(); // for enqueue seq test

    while (true)
    {       
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        socket.Bind(endpoint);
        socket.Blocking = false;
     
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, port);
        EndPoint tmpRemote = (EndPoint)sender;

        while (true)
        {
            try
            {
                recv = socket.ReceiveFrom(datagram, ref tmpRemote);
            }
            catch (SocketException e)
            {
                Thread.Sleep(threadSleep); 
                continue;
            }

            for (int i = 0; i < 4; i++) // 4 bytes of int. (datagram = seq + data)
            {
                list.Add(datagram[i]);
            }
            Debug.Write(Convert.ToString(BitConverter.ToUInt32(list.ToArray(), 0)) + " ");
            list.Clear();
        }
    }
}

The datagram's seq missing after 973.

Debug.Write () says ... 970 971 972 974 977 981 984 987 991 994 998 1001 1004 1007 1010 1014 1017 1021 1023 1027 1030 1034 1037 ...

Why does interval changed since the datagram sequence increased 1 at a time?

Or Should I think about other way to change byte array to Int?

Edit) I am sending data per 10ms. It works when i send data per 100ms.

Maybe a packet loss, because this is udp.shingo
It could be packet loss... I am sending data per 10ms. It works when I send data per 100ms.KooEunBam
Packet loss can also be caused by receive buffer overflow. How do you send the data, how large is the data and the how long is the sleep time? An interval of 10ms worked but 100ms didn't seems unreasonable.shingo
How can i control receive buffer overflow?KooEunBam
Try a bigger ReceiveBufferSize to see if the missing number becomes larger.shingo