When we using a raw socket, we need to handle everything including handling which packet we need to process.
Suppose I receive 2 packets with 10 byte size. The packets contain the message "helloworld" and "helooworld".
Suppose "helloworld" packet is the first packet I receive from the network, and we know that the "helloworld" packet will be placed into the buffer.
But before I'm processing the "helloworld" packet, I receive the "helooworld" packet.
Is there any overwritten data in the buffer?
Example code :
struct sockaddr _addr;
socklen_t size = sizeof(_addr);
unsigned char buffer[20];
int fd = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
recvfrom(fd,buffer,sizeof(buffer),0,&_addr,&size);
My question is :
When I receive the "helloworld" packet, the "helloworld" packet will be placed at the index 0-9, but before I'm processing the "helloworld" packet, I also receive the "helooworld" packet.
Will the "helooworld" packet be placed in index 0-9? And that means it will overwrite the previous data or be placed in index 10-19?
In the context of raw sockets and the code example provided, each call to recvfrom
will overwrite the contents of the buffer from the start (index 0), assuming the buffer size is sufficient to hold the received data.
To avoid losing data:
recvfrom
again or you need to pass different buffers to recvfrom