sockfd_ 为已成功建立连接的套接字
std::string local_ip_;
sockaddr_in name;
socklen_t namelen = sizeof(name);
int err = getsockname(sockfd_, (sockaddr*) &name, &namelen);
if (err < 0)
{
print_fatal("Could not get local IP");
close(sockfd_);
return false;
}
char str[18];
inet_ntop(AF_INET, &name.sin_addr, str, 18);
local_ip_ = str;
getsockname:https://www.cnblogs.com/zl-graduate/p/5934192.html
https://www.cnblogs.com/tangxin-blog/p/7348558.html
inet_ntop:https://blog.csdn.net/zyy617532750/article/details/58595700/