When we using a raw socket, we need to handle everything including handling which packet we need to process.
Suppose i receive 2 packet with 10 byte size. The packet contains message helloworld and helooworld.
Suppose helloworld packet is the first packet I'm receive from the network, and we know that the helloworld packet will be placed into the buffer.
But before I'm proccessing the helloworld packet, I'm receive 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'm receive the helloworld packet, the helloworld packet will be placed at the index 0-9, but before I'm proccessing the helloworld packet then i'm also receive the helooworld packet
Will 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