int type;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
memset(&sin, 0, sizeof(sin));
memset(&sin6, 0, sizeof(sin6));
if ( inet_pton( AF_INET, ip, &sin.sin_addr ) == 1 )
{
type = AF_INET;
sin.sin_family = AF_INET;
sin.sin_port = htons (port);
sin.sin_addr.s_addr= inet_addr(ip);
}
else
{
if ( inet_pton(AF_INET6, ip, &sin6.sin6_addr ) == 1 )
{
type = AF_INET6;
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons (port);
sin6.sin6_addr.s_addr= inet_addr(ip);
} }fd = socket(type, SOCK_STREAM, 0);//设置socket选项 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {printf("error"); } struct sockaddr* pa = 0; int len = 0; if ( type == AF_INET ) { pa = (struct
sockaddr*)&sin; len = sizeof(sin); } else { pa = (struct sockaddr*)&sin6; len = sizeof(sin6); }