#include "conn_handler.h" // Return the maximum socket descriptor set in the argument. int find_max_fd(const fd_set *fs) { int ret = 0; for(int i = FD_SETSIZE - 1; i>=0 && ret==0; --i){ if( FD_ISSET(i, fs) ){ ret = i; } } return ret; } // Return the file descriptor when the listen socket binds a new connection. int bind_and_listen( const char *service ) { struct addrinfo hints; struct addrinfo *rp, *result; int s; /* Build address data structure */ memset( &hints, 0, sizeof( struct addrinfo ) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; hints.ai_protocol = 0; /* Get local address info */ if ( ( s = getaddrinfo( NULL, service, &hints, &result ) ) != 0 ) { fprintf( stderr, "stream-talk-server: getaddrinfo: %s\n", gai_strerror( s ) ); return -1; } /* Iterate through the address list and try to perform passive open */ for ( rp = result; rp != NULL; rp = rp->ai_next ) { if ( ( s = socket( rp->ai_family, rp->ai_socktype, rp->ai_protocol ) ) == -1 ) { continue; } if ( !bind( s, rp->ai_addr, rp->ai_addrlen ) ) { break; } close( s ); } if ( rp == NULL ) { perror( "stream-talk-server: bind" ); return -1; } if ( listen( s, MAX_PENDING ) == -1 ) { perror( "stream-talk-server: listen" ); close( s ); return -1; } freeaddrinfo( result ); return s; }