I use the code on http://www.abc.se/~m6695/udp.html
the code works fine, I can see that the message is sent and the server receives it. However, When I build the program, I get the following “passing argument 5 of ‘sendto’ from incompatible pointer type [enabled by default]” for the line “if (sendto(s, buf, BUFSIZE, 0, &si_other, slen)==-1){“. I would appreciate if anyone could tell me how to fix. thank you
#define MAXSTRINGLENGTH 128
#define BUFSIZE 512
void log_msg_send(char *message, char *next_hop);
void log_msg_send(char *message, char *next_hop);
int main(int argc, char** argv) {
char hello[] = "hello bitches";
char next_hop[]= "192.168.1.178";
log_msg_send(hello, next_hop);
}
void log_msg_send(char *message, char *next_hop){
char SRV_IP[16];
strcpy(SRV_IP, next_hop);
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[50] ;
strcpy(buf, message);
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(33333);
if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
printf("Sending the packet %d\n", i);
if (sendto(s, buf, BUFSIZE, 0, &si_other, slen)==-1){
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
close(s);
}
Quick fix:
This conversion is required because
sendto()expects a pointer tostruct sockaddras its fifth argument while your particular socket address type isstruct sockaddr_in.