Programmation Socket: sendto toujours échoue avec errno 22 (EINVAL)

Je suis toujours pas d'octets envoyés, avec errno 22 (EINVAL, Argument non Valide) avec ce code. Le destination_host est défini ailleurs et connu pour être valable, donc je ne vois vraiment pas ce qui se passe. MAXMSGSIZE est de 1000. Pas d'erreurs ou d'avertissements. Je suis de la compilation avec -Wall -Werror -pedantic

char *data_rec;
u_int data_len;

int sockfd;
uint16_t *ns;
struct sockaddr_in address;
struct sockaddr *addr;

char *ip;

int i;
int errno;
int bytes_sent;

data_len = MAXMSGSIZE;
data_rec = malloc(sizeof(char)*MAXMSGSIZE);
ns = malloc(MAXMSGSIZE*sizeof(uint16_t));
ip = malloc(MAXMSGSIZE*sizeof(char));

data_rec = "some random test stuff";

sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd<0) {
    printf("socket() failed\n");
}

inet_ntop(AF_INET,destination_host->h_addr,ip,MAXMSGSIZE);
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(theirPort);
address.sin_addr.s_addr = (unsigned long)ip;

addr = (struct sockaddr*)&address;
bind(sockfd,addr,sizeof(address));
/*Convert the message to uint16_t*/
for(i=0; i<MAXMSGSIZE; i++) {
    ns[i] = htons(data_rec[i]);
}


/* send the message */
bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));
if(bytes_sent == -1) {
    printf("Error sending: %i\n",errno);
}
Vous pouvez utiliser perror() pour obtenir un message d'erreur plus descriptif que errno 22
Ce qui se passe quand s_addr est INADDR_ANY? Si cela fonctionne, vous connaissez le problème, c'est... 🙂
Aussi, il semble que vous déclarez errno et de ne jamais céder. Peut-être que vous avez voulu écrire extern int errno?

OriginalL'auteur dc. | 2010-10-24