当前位置: 动力学知识库 > 问答 > 编程问答 >

Cant get port when accept with sockaddr_storage - ipv6

问题描述:

I can not get port number when i accept with sockaddr_storage in ipv6.

Here is my v4 code:

peersockfd = accept(xtcpsock_fd,(struct sockaddr*)&addr,(socklen_t*)&len);

.....

sprintf(szSocket,"%s:%d",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));

I am trying to do the same thing for v6 but sockaddr_storage doesn't have any member as port. How can i do that?

peersockfd = accept(xtcpsock6_fd, (struct sockaddr *)&their_addr, &sin_size);

.....

inet_ntop(their_addr.ss_family, &((struct sockaddr_in6 *)&their_addr)->sin6_addr,s,sizeof s);

sprintf(szSocket,"%s:%d",s,***ntohs(their_addr.sin_port));

网友答案:

For IPv6 you need to cast to sockaddr_in6 and then the member is named sin6_port rather than sin_port.

分享给朋友:
您可能感兴趣的文章:
随机阅读: