potato.h
/* This function finds an avaiable address with port_num,
   and returns a socket fd which is bound to port_num */
This function finds an avaiable address with port_num,
and returns a socket fd which is bound to port_num
This function finds an avaiable address with port_num and returns a socket fd which is bound to port_num
int socket_and_bind(const char * port_num) {
  int status;
  int listen_fd;
  struct addrinfo hints;
  struct addrinfo * addr_info_list;  
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;      
  hints.ai_socktype = SOCK_STREAM;  
  hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
  status = getaddrinfo(NULL, port_num, &hints, &addr_info_list);
  if (status != 0) {
    return -1;
  }
  struct addrinfo * curr;
  for (curr = addr_info_list; curr != NULL; curr = curr->ai_next) {
    listen_fd = socket(curr->ai_family, curr->ai_socktype, curr->ai_protocol);
    if (listen_fd == -1) {
      continue;  
    }
    int yes = 1;
    status = setsockopt(listen_fd, SOL_SOCKET, 0, &yes, sizeof(yes));
    if (yes == -1) {
      freeaddrinfo(addr_info_list);
      checkedClose(listen_fd);
      return -1;
    }
    status = bind(listen_fd, curr->ai_addr, curr->ai_addrlen);
    if (status == 0) {
      break;  
    }
    else {  
      checkedClose(listen_fd);
    }
  }
  freeaddrinfo(addr_info_list);  
  if (curr == NULL) {
    return -1;
  }
  return listen_fd;
}
int socket_and_bind ( const char * port_num ) { int status ; int listen_fd ; struct addrinfo hints ; struct addrinfo * addr_info_list ; memset ( & hints , 0 , sizeof ( hints ) ) ; hints . ai_family = AF_UNSPEC ; hints . ai_socktype = SOCK_STREAM ; hints . ai_flags = AI_PASSIVE | AI_NUMERICSERV ; status = getaddrinfo ( NULL , port_num , & hints , & addr_info_list ) ; if ( status != 0 ) { return -1 ; } struct addrinfo * curr ; for ( curr = addr_info_list ; curr != NULL ; curr = curr -> ai_next ) { listen_fd = socket ( curr -> ai_family , curr -> ai_socktype , curr -> ai_protocol ) ; if ( listen_fd == -1 ) { continue ; } int yes = 1 ; status = setsockopt ( listen_fd , SOL_SOCKET , 0 , & yes , sizeof ( yes ) ) ; if ( yes == -1 ) { freeaddrinfo ( addr_info_list ) ; checkedClose ( listen_fd ) ; return -1 ; } status = bind ( listen_fd , curr -> ai_addr , curr -> ai_addrlen ) ; if ( status == 0 ) { break ; } else { checkedClose ( listen_fd ) ; } } freeaddrinfo ( addr_info_list ) ; if ( curr == NULL ) { return -1 ; } return listen_fd ; }
This function finds an avaiable address with port_num,
and returns a socket fd which is bound to port_num
This function finds an avaiable address with port_num and returns a socket fd which is bound to port_num
/* Get the listenable address list with port_num */ // head of the addr info list // SearchingIPv4 and IPv6 // TCP /* Iterate the returned address list to find one using which we
     can successfully create and bind a socket */ // Create socket failed, try next address // Note, here we don't set SO_REUSEADDR, because we don't want // different players in the same host bound to the same port. // Otherwise, the second player's listen() will fail. // bind() succeed // bind() failed, close the socket and try next address // Free dynamically allocated space