Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive packet by packet data from TCP socket?

Tags:

c++

c

tcp

sockets

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.

like image 248
Ali Ahmed Avatar asked Jun 07 '11 10:06

Ali Ahmed


People also ask

Does TCP receive packets?

Transmission Control Protocol (TCP) is helpful to send a file or message over a connected network. Each file gets divided into packets at the sender network. The packets get merged on arrival at the receiving network. The maximum size of a TCP packet is 64K (65535 bytes).

How does TCP receive data?

When the sending TCP wants to establish connections, it sends a segment called a SYN to the peer TCP protocol running on the receiving host. The receiving TCP returns a segment called an ACK to acknowledge the successful receipt of the segment. The sending TCP sends another ACK segment, then proceeds to send the data.

Which methods are used for sending and receiving data in TCP socket?

recv(bufsize) − As name implies, this method receives the TCP message from socket. The argument bufsize stands for buffer size and defines the maximum data this method can receive at any one time. socket. send(bytes) − This method is used to send data to the socket which is connected to the remote machine.

Can TCP receive packets out of order?

TCP "guarantees" that a receiver will receive the reconstituted stream of bytes as it was originally sent by the sender. However, between the TCP send/receive endpoints (i.e., the physical network), the data can be received out of order, it can be fragmented, it can be corrupted, and it can even be lost.


2 Answers

As others have mentioned, TCP is a streaming protocol. This means from an API point of view, there is no concept of "packet". As a user, all you can expect is a stream of data.

Internally, TCP will break the stream into segments that can be placed into IP packets. These packets will be sent along with control data, over IP, to the remote end. The remote end will receive these IP packets. It may discard certain IP packets (in the case of duplicates), reorder the packets or withhold data until earlier packets have arrived. All this is internal to TCP meaning the concept of a "TCP packet" is meaningless.

You might be able to use raw sockets to receive the raw IP packets but this will mean you will have to reimplement much of the TCP stack (like sending ACKs and adjusting window size) to get the remote end to perform correctly. You do not want to do this.

UDP, on the other hand, is a datagram protocol. This means that the user is made aware of how the data is sent over the network. If the concept of packets or datagrams are important to you, you will need to build your own protocol on top of UDP.

like image 85
doron Avatar answered Oct 11 '22 02:10

doron


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:

  • When you want to send a "packet" include as the first thing a length
  • When you read stuff from a socket make sure you read at least that length

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.

like image 30
cnicutar Avatar answered Oct 11 '22 02:10

cnicutar