Created
September 5, 2014 17:31
-
-
Save zweng/9e987b49704f87eace29 to your computer and use it in GitHub Desktop.
bind a socket
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
struct addrinfo hints, *res; | |
int sockfd; | |
// first, load up address structs with getaddrinfo(): | |
memset(&hints, 0, sizeof hints); | |
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever | |
hints.ai_socktype = SOCK_STREAM; | |
hints.ai_flags = AI_PASSIVE; | |
// fill in my IP for me | |
getaddrinfo(NULL, "3490", &hints, &res); | |
// make a socket: | |
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
// bind it to the port we passed in to getaddrinfo(): | |
bind(sockfd, res->ai_addr, res->ai_addrlen); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment