Created
June 22, 2014 04:40
-
-
Save matter123/81e4cf0f6607146ab1f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int sock; | |
addrinfo *info; | |
int Init(const char *addr,int port) { | |
addrinfo hints, *res; | |
memset(&hints,0,sizeof(addrinfo)); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = SOCK_STREAM; | |
hints.ai_flags = AI_PASSIVE; | |
hints.ai_protocol = 0; | |
hints.ai_canonname = NULL; | |
hints.ai_addrlen = 0; | |
hints.ai_addr = NULL; | |
hints.ai_next = NULL; | |
char buf[6]; | |
sprintf(buf,"%d",port); | |
printf("%s:%s\n",addr,buf); | |
int retv=getaddrinfo(addr,buf,&hints,&res); | |
if(retv) { | |
std::cout<<gai_strerror(retv); | |
errno=EINVAL; | |
return errno; | |
} | |
info=res; | |
while(info) { | |
if((sock=socket(info->ai_family,info->ai_socktype,info->ai_protocol))==-1) { | |
printf("%s\n",strerror(errno)); | |
info=info->ai_next; | |
continue; | |
} | |
if(bind(sock,info->ai_addr,info->ai_addrlen)) { | |
printf("%s\n",strerror(errno)); | |
close(sock); | |
info=info->ai_next; | |
continue; | |
} | |
break; | |
} | |
if(!info) { | |
errno=EINVAL; | |
return errno; | |
} | |
freeaddrinfo(res); | |
if(listen(sock,20)) { | |
return errno; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment