Getting IPV4 address from a sockaddr structure

后端 未结 6 1741
一整个雨季
一整个雨季 2020-12-07 22:32

How can I extract an IP address into a string? I can\'t find a reference that tells me how char sa_data[14] is encoded.

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 22:55

    Just cast the entire sockaddr structure to a sockaddr_in. Then you can use:

    char *ip = inet_ntoa(their_addr.sin_addr)
    

    To retrieve the standard ip representation.

提交回复
热议问题