I lost a lot of time in understanding the message "sendto Invalid argument" when I execute the following code.
This code is a simple UDP sender improved with some reliability feature.
My goal is to send a file. I've reported only the code which may be useful.
Can anyone help me?
Thank you in advance.
typedef struct{
char* filename;
unsigned long encoded_file_size;
}first_message;
typedef struct {
char data[DATALENGTH];
int nseq;
}packet;
memset((char *)&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(PORT);
dest.sin_addr.s_addr = inet_addr(argv[1]); /*destination's address*/
if ((sd = socket(AF_INET,SOCK_DGRAM,0))<0) {
perror("socket creation failed");
exit(EXEC_FAILED);
fin = fopen(argv[2],"rb");
if (fin != NULL){
fm.filename = find_fileName(argv[2]);
fm.encoded_file_size = htonl(get_file_size(fin));
printf("sizeof(fm) %ld",sizeof(fm));
/*HERE THE ERROR*/
while (send_first(sd,&fm,sizeof(fm),"unable to send first message",(struct sockaddr*)&dest)<0){
send_first(sd,&fm,2*sizeof(fm),"unable to send first message",(struct sockaddr*)&dest);}
.....
} /*end of main*/
ssize_t send_first(int sd, void* buf,size_t len,char* err, const struct sockaddr* to){
unsigned long byte_sent;
if(byte_sent = sendto(sd, buf, len, 0, to, sizeof(to))<0){
perror(err); }
sleep(TIMEOUT);
if(fcntl(sd,F_SETFL,O_NONBLOCK)==-1){
perror("Impossible to set NONBLOCK mode for the socket");
return recvfrom(sd, buf,len,0,NULL,NULL);