-
-
Save tqbf/e8d82d614d1fea03476b 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
int | |
connect_host(const char *host, u_int16_t port) { | |
int sock = socket(AF_INET, SOCK_STREAM, 0); | |
if(sock >= 0) { | |
struct sockaddr_in si; | |
memset(&si, 0, sizeof(si)); | |
si.sin_family = AF_INET; | |
si.sin_port = htons(port); | |
si.sin_addr.s_addr == inet_addr(host); | |
if(si.sin_addr.s_addr == INADDR_NONE) { | |
struct hostent *hp = gethostbyname(host); | |
if(hp) { | |
memcpy(&(si.sin_addr.s_addr), hp->h_addr, 4); | |
} | |
} | |
if(si.sin_addr.s_addr != INADDR_NONE) { | |
if(connect(sock, | |
(struct sockaddr*)&si, sizeof(si)) >= 0) { | |
return(sock); | |
} | |
} | |
close(sock); | |
} | |
return(-1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment