“‘sockaddr_in’ undeclared (first use in this function)” error despite including the requisite headers

老子叫甜甜 提交于 2019-12-07 06:15:37

问题


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>#include <net/if.h>
#include <stdarg.h>

#define BACKLOG 10

void * get_in_addr(struct sockaddr *sa){
    if(sa->sa_family == AF_INET){
            return &((sockaddr_in *)sa)->sin_addr;
    }
    else if(sa->sa_family == AF_INET6){
            return &((sockaddr_in6 *)sa)->sin6_addr;
    }
}

I am using the sockaddr_in struct in my code to chekc whether an incoming connection is an IPv4 or an IPV6 address. I get the error "‘sockaddr_in’ undeclared (first use in this function)" here despite including the netinet/in.h header in my code. Is there something that I am not seeing here?


回答1:


sockaddr_in is not typedef, so try using it with struct like following

(struct sockaddr_in *)


来源:https://stackoverflow.com/questions/16606386/sockaddr-in-undeclared-first-use-in-this-function-error-despite-including

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!