c++csocketstcpvideo-streaming

Receive packet by packet data from TCP socket?


I have a TCP socket on which I receive video stream. I want to receive data as packet by packet from socket so that I could remove the packet header and keep the only stream data. How can I do this??

Any help will be appreciated.


Solution

  • You can't. TCP doesn't work with packets / messages etc. TCP works with bytes. You get a stream of bytes. The problem is that there's no guarantee reagarding the number of bytes you'll get each time you read from a socket. The usual way to handle this:

    Your message could be:

    |Message Length:4bytes|Additional header Information:whatever1|Message Data:whatever2|
    

    What you'll then have to do is read 4 bytes and then read as much as those 4 bytes tell you. Then you'll be able to strip the header and get the data.