我正在尝试编写一个监听IPv4和IPv6地址的Web服务器。然而,我最初编写的代码并没有起作用。后来我发现IPv6结构可用于IPv4和IPv6。因此,我现在使用IPv6结构,但只有IPv4地址有效。这篇文章为什么不能将IPv6套接字绑定到本地链接地址建议添加
谢谢!
server.sin6_scope_id = 5;
,所以我这样做了,但它仍然无法接受IPv6 telnet连接。因为我被彻底困惑了,所以任何帮助都将不胜感激。谢谢!
我的代码如下:
void initialize_server(int port, int connections, char* address)
{
struct sockaddr_in6 socket_struct;
/*Creates the socket*/
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
syslog(LOG_ERR, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}/*Ends the socket creation*/
/*Populates the socket address structure*/
socket_struct.sin6_family = AF_INET6;
if(address == NULL)
socket_struct.sin6_addr=in6addr_any;
else
{
inet_pton(AF_INET6, "fe80::216:3eff:fec3:3c22", (void *)&socket_struct.sin6_addr.s6_addr);
}
socket_struct.sin6_port =htons(port);
socket_struct.sin6_scope_id = 0;
if (bind(sock_fd, (struct sockaddr*) &socket_struct, sizeof(socket_struct)) < 0)
{
syslog(LOG_ERR, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}//Ends the binding.
if (listen(sock_fd, connections) <0)
{
syslog(LOG_ERR, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}//Ends the listening function
}//ends the initialize server function.