C # UDP работает медленнее, чем TCP - PullRequest
1 голос
/ 09 марта 2012

Я пытаюсь передать потоковое видео с Xbox Kinect с клиента на сервер. Я получил это работает с TCP, но я мог получить только около 5 кадров в секунду, так что теперь я пробую это с UDP. UDP должен быть быстрее из-за того, как работает протокол, но кажется, что он медленнее. Вот мой пост про ПТС (/5280167/c-potokovoe-video-cherez-networkstream-tcpclient)

Я могу отправить все эти данные по локальной сети, но я начинаю терять много пакетов, если слишком быстро их выталкиваю. Вот почему я использую Thread.Sleep (20); Увеличение размера чака ускоряет его, но я максимально использую для отправки по локальной сети, и если я правильно понимаю, максимальный размер чанка для отправки через Интернет составляет около 1500 байт. Если бы я отправлял только 1500 байт за раз, это было бы очень медленно. Должно быть, я что-то делаю не так.

Вот код.

    private const int constChunkSize = 38400;
    protected UdpClient udpObject;

    private void HandleComm()
    {
        byte[] fullMessage = new byte[1228800];
        byte[] byteReceived;
        int currentIndex = 0;
        IPEndPoint remoteIPEndPoint = new IPEndPoint(ip, port);
        while (true)
        {                
            byteReceived = udpObject.Receive(ref remoteIPEndPoint);

            if (currentIndex + byteReceived.Length > 1228800)
            {
                int wtf = 0;
            }
            Array.Copy(byteReceived, 0, fullMessage, currentIndex, byteReceived.Length);
            currentIndex += byteReceived.Length;
            //Console.WriteLine("Recieved: " + currentIndex);
            if (currentIndex == 1228800)
            {
                if (OnDataReceived != null)
                {
                    FrameReceivedArgs args = new FrameReceivedArgs();
                    args.frame = new byte[fullMessage.Length];
                    fullMessage.CopyTo(args.frame, 0);
                    OnDataReceived(this, args);
                }
                currentIndex = 0;
                Console.WriteLine("Done receiving" + DateTime.Now.Ticks);
            }
        }            
    }

    public void sendData(byte[] data)
    {
        sending = true;
        sendThread = new Thread(sendDataThread);
        sendThread.Priority = ThreadPriority.Highest;
        sendThread.Start(data);            
    }

    private void sendDataThread(object tempData)
    {
        byte[] data = (byte[]) tempData;
        int totalBytes = data.Length;
        int currentBytes = 0;
        int bufferLength = constChunkSize;
        byte[] sendBytes = new byte[constChunkSize];

        while (currentBytes < totalBytes)
        {
            if (totalBytes - currentBytes < constChunkSize)
                bufferLength = totalBytes - currentBytes;

            Array.Copy(data, currentBytes, sendBytes, 0, bufferLength);

            currentBytes += bufferLength;
            udpObject.BeginSend(sendBytes, bufferLength, new AsyncCallback(sendingData), udpObject);
            Thread.Sleep(20);
            //Console.WriteLine("Sent: " + currentBytes);
        }
        Console.WriteLine("done sending" + DateTime.Now.Ticks);
        sending = false;
    }

    private void sendingData(IAsyncResult ar)
    {            
        udpObject.EndSend(ar);
    }
...