0

Here's the problem: I've successfully setup an app that will pull individual frames of video from a phone and move them to the server. But as you know, the inherent latency won't allow for a smooth transition on the server side. I'm guessing I need to package a number of images in a single byte array and unpack them on the server side. But is there a clean/simple way to do this? Could I use a multi-dimensional array? Something else?

Here's what I got client side (payload is an incoming image converted to a single byte array):

if (_socket != null)
        {
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();

            socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
            socketEventArg.UserToken = null;

            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
            {
                response = e.SocketError.ToString();

                // Unblock the UI thread
                _clientDone.Set();
            });


            socketEventArg.SetBuffer(payload, 0, payload.Length);
            _clientDone.Reset();

            _socket.SendAsync(socketEventArg);                
            _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
        }

And here's what I'm doing on the server side:

        const int portNo = portNumber;

        TcpListener listener = new TcpListener(portNo);
        listener.Start();
        Console.WriteLine("Listening...");

        TcpClient tcpClient = listener.AcceptTcpClient();
        NetworkStream NWStream = tcpClient.GetStream();
        byte[] bytesToRead = new byte[tcpClient.ReceiveBufferSize + 1];
        FrameCount += 1;


        int numBytesRead = NWStream.Read(bytesToRead, 0, Convert.ToInt32(tcpClient.ReceiveBufferSize));

        string FILE_NAME = "c:\\frames\\" + FrameCount.ToString() + ".gif";
        System.IO.FileStream fs = null;
        fs = new FileStream(FILE_NAME, FileMode.CreateNew, FileAccess.Write);
        fs.Write(bytesToRead, 0, numBytesRead);
        fs.Close();

        tcpClient.Close();
        listener.Stop();

        StartImage();

1 Answer 1

3

As long as your images are larger than the tcp package size (I think it's 1024 by default on Windows) you won't get performance from sending multiple images in one Send/Receive.

However, sending multiple images per socket connection is useful because you will avoid the overhead of multiple connection handshakes.

Before you optimize your packages, try to send multiple images one after another on the same socket connection.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 - it's just amazing how devs. complain about TCP latency while using an open/close protocol.
Thanks for the answer nvoigt. Martin, some of us devs aren't used to socket programming. This is my first foray into such an adventure so I'm really shooting in the dark, learning as I go. I'm used to web development where the only latency optimization we have to do is in the creative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.