Skip to content

Instantly share code, notes, and snippets.

@zweng
Created September 5, 2014 17:31
Show Gist options
  • Save zweng/9e987b49704f87eace29 to your computer and use it in GitHub Desktop.
Save zweng/9e987b49704f87eace29 to your computer and use it in GitHub Desktop.
bind a socket
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