sendto() - UDP unicast in C

7.2k Views Asked by At

I have the function below that sends the message to the next_hop (e.g. 192.168.0.10) as UDP unicast. When I compile the code I get the following warning passing argument 2 of 'sendto' makes pointer from integer without a cast for the line if(sendto(s, *message, BUFSIZE, 0, &si_other, slen) == -1). How can I fix the code? Thank you for the answers.

void log_msg_send(char *message, char *next_hop){

struct sockaddr_in si_other;
int s, slen = sizeof(si_other);
int port_no = 3333;
char *nexthop;
unsigned short Server_port;
nexthop = next_hop;
Server_port = port_no;

if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    fprintf(stderr,"socket error");
    exit(1);
}

memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(Server_port);

if(inet_aton(nexthop, &si_other.sin_addr) == 0){
    fprintf(stderr,"socket error");
    exit(1);
}

if(sendto(s, *message, BUFSIZE, 0, &si_other, slen) == -1){
    fprintf(stderr,"socket error");
    exit(1);
}

close(s);

}

1

There are 1 best solutions below

0
On BEST ANSWER
char * message

Means:

(char *) message // message is a pointer-to-char

And:

char (*message) // *message is a char

So when you need to pass a char*, use message. If you need the first char in that buffer, use *message.


Warning:

char * a, b; 

Makes a a char*, but b a plain char, wherever you put spaces.


The fifth argument might need to be cast to a struct sockaddr* manually, depending on how it is prototyped on your system.