根据客户端套接字获取客户机的IP和端口号,其形式为IP:port

时间:2021-08-12 10:22:00
char *sock_ntop(const struct sockaddr *sa, socklen_t salen)
{
 char portstr[7];
 static char str[128];
 switch (sa->sa_family)
 {
  case AF_INET:
    {
      struct sockaddr_in *sin = (struct sockaddr_in *)sa;
      if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str))==NULL)
        return NULL;
      if (ntohs(sin->sin_port)!=0)
      {
        snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin_port));
        strcat(str, portstr);
      }
      return str;
    }
     break;
  case AF_INET6:
    {
      struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
      if (inet_ntop(AF_INET6, &sin->sin6_addr, str, sizeof(str))==NULL)
        return NULL;
      if (ntohs(sin->sin6_port)!=0)
      {
        snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin6_port));
        strcat(str, portstr);
      }
      return str;
    }
     break;
  default:
     return NULL;
     break;
 }
}