我无法弄清楚这一点。任何人都可以告诉我为什么我会收到此错误:
warning: passing argument 2 of ‘getsockname’ from incompatible pointer type
在以下代码中:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
int main() {
int sd;
struct sockaddr_in my_addr;
bzero(&my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
my_addr.sin_port = htons(0);
my_addr.sin_addr.s_addr = INADDR_ANY;
socklen_t my_addr_size = sizeof my_addr;
if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stdout, "Cannot create socket for master socket.\n");
fprintf(stdout, "Terminating program\n\n");
exit(1);
}
if (bind(sd, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
fprintf (stdout, "Binding failed for master socket\n\n");
perror("bind failed");
exit (1);
}
if (getsockname(sd, &my_addr, &my_addr_size) == -1) {
perror("getsockname() failed");
return -1;
}
}
答案 0 :(得分:5)
getsockname
的第二个参数应为struct sockaddr *
。您传递的是struct sockaddr_in
。