Address family not supported by protocol family: connect 解决方案

在Spring Boot使用OkHttp请求其他服务接口时,出现Address family not supported by protocol family: connect异常,单独运行代码无问题。在StackOverflow找到解决方案,在VM options追加参数 -Djava.net.preferIPv4Stack=true 可解决,暂不清楚仅在Spring Boot环境下出现异常的原因。

在springboot 使用okhttp请求其他服务的接口时,出现了Address family not supported by protocol family: connect这个异常, 后来把这一块代码单独拎出去运行,是没有任何问题的。

后来在StackOverflow上找到了一个可用的解决方案,具体为什么只在springboot环境下出现异常暂时还不太清楚。

解决方法
VM options 追加参数: -Djava.net.preferIPv4Stack=true

IDEA下的VM options设置方法如下,其他环境请自行百度。
在这里插入图片描述

转自stackoverflow–Address family not supported by protocol family - SocketException on a specific computer

解释一下 getaddrinfo - node name-to-address translation * * This routine needs an IP node name (for example, the host or router node), * an IP address in <hostname>, and/or a service name (such as "ftp") in * <servname>. The routine provides a linked list of 'addrinfo' structures * in <res>. <res> must be freed with 'freeaddrinfo' after use to avoid * memory leaks. The address information is returned in network byte order. * * The search can be narrowed by specifying different flags for <hints> or can * be NULL. (See below). The search starts in the host table; if the search * cannot be satisfied there, a DNS request is carried out (if a DNS server * has been initialized). * * The 'addrinfo' structure is as follows: * \cs * struct addrinfo * { * int ai_flags; -> AI_PASSIVE, ... * int ai_family; -> AF_INET, AF_INET6, AF_UNSPEC * int ai_socktype; -> SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, ANY * int ai_protocol; -> IPPROTO_TCP, IPPROTO_ICMP, ... * size_t ai_addrlen; -> length of ai_addr * Don't use for hints! * char *ai_canonname; -> ptr to canonical name of host * Don't use for hints! * struct sockaddr *ai_addr; -> ptr to socket address structure * Don't use for hints! * struct addrinfo *ai_next; -> ptr to next structure in linked list * Don't use for hints! * }; * \ce * The following flags are defined: * \is * \i AI_PASSIVE * Specifies the returned address if the hostname is NULL. If this * flag is specified the returned address must be suitable for * a call to bind (-> IN(6)ADDR_ANY(_INIT)); otherwise, the returned * address must be suitable for a call to connect (-> loopback address). * \i AI_CANONNAME * Attempts to determine the canonical name of this node. (The * specified hostname must not be NULL). * \i AI_NUMERICHOST * Returns the host address string instead of <hostname>; this flag * prevents using any type of name resolution service (for example, DNS). * \i AI_NUMERICSERV * Returns the numeric port string instead of the service name. * \i AI_V4MAPPED * If no IPv6 record is found in either the host table or DNS, * a request for IPv4 records is sent and the found IPv4 address * is returned as IPv4-mapped IPv6 address. * Used for IPv6 (AF_INET6) only. * \i AI_ALL * Used in conjunction with AI_V4MAPPED. The caller accepts all addresses * in IPv6 and IPv4-mapped IPv6 formats. IPv4 (AF_INET) is ignored. * Not implemented. * \i AI_ADDRCONFIG * Causes a query to IPv6 to occur only if at least one IPv6 * address is locally configured. (The same is true for IPv4 queries.) * Not implemented. * \ie * EXAMPLE * \cs * struct addrinfo hints, *res; * * bzero ((char *) &hints, sizeof (hints)); * hints.ai_flags = AI_V4MAPPED; * hints.ai_family = AF_INET6; * * getaddrinfo ("alpha","ftp",&hints,&res); * \... * freeaddrinfo (res); * \ce * * RETURNS * A numeric error code that can be converted into a string with * gai_strerror() (see netdb.h). * \is * \i EAI_ADDRFAMILY * This address family is not supported for <hostname>. For example, a host * name with an IPv4 address string and hints is incompatible with AF_INET6. * \i EAI_AGAIN * A temporary failure in name resolution occurred. * \i EAI_BADFLAGS * The value for 'ai_flags' is invalid. * \i EAI_FAIL * A non-recoverable failure in name resolution occurred. * \i EAI_FAMILY * The specified 'ai_family' is not supported. * \i EAI_MEMORY * A memory allocation failure occurred. * \i EAI_NODATA * There is no address associated with <hostname>. * \i EAI_NONAME * No <hostname> or <servname> is provided, or the provided one is not known. * \i EAI_SERVICE * The provided <servname> is not supported for 'ai_socktype'. * \i EAI_SOCKTYPE * The value of 'ai_socktype' is not supported. * \i EAI_SYSTEM * A system error was returned in 'errno'. * \i EAI_BADHINTS * The value for <hints> is invalid. * \i 0 * Returns zero if OK. * \ie */
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值