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?