cpp SOCK_RAW continues to receive the same message ad infinitum

0

I create a socket of the form:

fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

then proceed:

bind(fd, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in))

where saddr supports INADDR_ANY and is from the AF_INET family

The problem I have is that after listening with a loop of the type:

while(true) {
  char mess[BUFFSIZE+1];
  int content_size = recv(fd, mess, BUFFSIZE, 0);
  mess[content_size] = '
fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
'; std::cout<<"recibí el mensaje: "<<mess<<std::endl; }

Once I receive a continuous message, I receive it during all the subsequent tours of the loop

EDIT: I program in DEBIAN.

    
asked by badabum 05.12.2017 в 14:30
source

1 answer

0

You are configuring the connection with UDP, that is, with datagrams:

fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
//                             ^^^^^^^^^^^

For this type of connection you must use recvfrom . Something like this:

struct sockaddr_storage addr;
int content_size = recvfrom(fd, mess, BUFFSIZE, 0, (struct sockaddr*)&addr, sizeof(struct sockaddr_storage));
    
answered by 05.12.2017 в 14:41