- struct sockaddr_in sa;
-
- sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- sa.sin_addr.s_addr = INADDR_ANY;
- sa.sin_family = AF_INET;
- sa.sin_port = htons(port);
- bind(sockfd,(struct sockaddr *)&sa,sizeof(sa));
+ struct addrinfo hints, *ai;
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_family = PF_INET6;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ hints.ai_addrlen = 0;
+ hints.ai_canonname = hints.ai_addr = hints.ai_next = NULL;
+ getaddrinfo(NULL,[[[NSNumber numberWithShort:port] stringValue] cString],&hints,&ai);
+ sockfd = socket(PF_INET6,SOCK_STREAM,IPPROTO_TCP);
+ bind(sockfd,ai->ai_addr,ai->ai_addrlen);