Reputation: 337
I'm trying to open a socket in UDP and subsequently send a message using it. This is the code that opens the socket.
sockfd = startServer(char* ip_addr, int port, struct sockaddr_in server){
int sockfd, len;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){
perror("Failed to open socket: ");
exit(-1);
}
len = sizeof(server);
bzero(&server, len);
server.sin_family= AF_INET;
inet_aton(ip_addr, &server.sin_addr.s_addr);
server.sin_port= htons(port);
if((bind(sockfd, (struct sockaddr*)&server, len)) < 0){
perror("Bind failed: ");
exit(-1);
}
return sockfd;
}
Now to send a message on this socket. I use this function.
void sendMessage(char* msg, int sockfd, struct sockaddr_in client, int len){
int sbytes = 0;
sbytes = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr*)&client, len);
if(sbytes < 0){
perror("Error sending message: ");
}
printf("Routing vector sent to %s\n", inet_ntoa(client.sin_addr));
}
I keep getting the following error.
error: incompatible pointer types passing 'in_addr_t *' (aka 'unsigned int *') to parameter of type 'struct in_addr *' [-Wincompatible-pointer-types]
inet_aton(ip_addr, &server.sin_addr.s_addr);
^~~~~~~~~~~~~~~~~~~~~~~
Any help appreciated.
Upvotes: 0
Views: 366
Reputation: 224207
The second parameter to inet_aton
expects a struct in_addr *
. You're passing in &server.sin_addr.s_addr
, which is an in_addr_t *
.
What you want is this:
inet_aton(ip_addr, &server.sin_addr);
Since server.sin_addr
is a struct in_addr
.
Upvotes: 1