Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

697
Visualizações
UDP sendto() error: invalid argument

We are trying to apply a UDP based protocol and having some problem with sendto() function.

when we try to response to the write-request with ack we get "invalid argument" from the sendto() function

this is our code:

int                sock;                  // Socket 
sockaddr_in_t      echoServAddr;          // Local address 
sockaddr_in_t      echoClntAddr;          // Client address 
unsigned int       cliAddrLen;            // Length of incoming message
data_packet_t      echoBuffer;
wrq_packet_t       wrqBuffer;
unsigned short     echoServPort;          // Server port 
int                recvMsgSize;           // Size of received message 
ack_packet_t      Ack;
struct timeval     timeout;
fd_set             fds;



/* Create socket for sending/receiving datagrams */
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) perror("TTFTPERROR: socket() failed"); 

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
echoServAddr.sin_port = htons(echoServPort);

/*Bind to the local address */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) perror("TTFTPERROR: bind() failed");

FD_ZERO(&fds);
FD_SET(sock, &fds);
timeout.tv_sec = WAIT_FOR_PACKET_TIMEOUT;
timeout.tv_usec = 0;

while (1) {
    recvMsgSize = recvfrom(sock, &wrqBuffer, FULL_PACKET_SIZE, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen);
    if (recvMsgSize > 0) break; // we got something!
}

Ack = CreateAckPacket(0); // send ack 0
if (sendto(sock, &Ack, sizeof(Ack), 0, (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) == -1){
    perror("TTFTPERROR: sendto() failed to send ack 0");
    exit(-1);
}

Could you help us understand what is wrong?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The likely error is a combination of the sendto line and recvfrom a few lines above.

What you are doing is, you reuse the sockaddr that recvfrom gave you to send a reply (ACK in this case). That is absolutely legitimate and correct, how else could you send a reply to someone on a connectionless protocol (you have no other way of knowing whom to send the answer to, nor with what protocol version!).

But: While you correctly provide &cliAddrLen to recvfrom so it can return the actual address length (which may vary, think of IPv4 vs IPv6 addresses) that was written into the buffer echoClntAddr, you later call sendto with sizeof(echoClntAddr) as the size of the address field.
Since echoClntAddr is most likely a sockaddr_storage (it should be, anyway), its size will be larger than any valid protocol's address size. Thus, it's an invalid argument. Or rather, the address is.

You should instead call sendto with cliAddrLen (and initialize cliAddrLen to sizeof(echoClntAddr) prior to calling recvfrom).

over 4 years ago · Santiago Trujillo Relatório

0

Also be very careful to you pass VALID and EXISTING IP address to the bind()'s parameter: echoServAddr.sin_addr.s_addr

If you don't have network interface exposed with that exactly IP address the bind won't fail on some platforms (ESP32 for example) BUT the sendto() WILL definitely fail. I've spend 2 days debugging that and I'm mad!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda