src/connect.c
int connect_to_twitserver( const char * restrict addr, const char * restrict port ){
	struct addrinfo *infop = NULL;
	struct addrinfo hint;
	int status = 0;
	int socketfd = -1;

	assert( addr != NULL );
	assert( port != NULL );
	
	do{
		// Get the addr of the twitserver
		( void )memset( &hint, 0, sizeof( hint ) );
		hint.ai_family = AF_INET;
		hint.ai_socktype = SOCK_STREAM;
		hint.ai_protocol = 0;
		if ( ( status = getaddrinfo( addr, port, &hint, &infop ) ) != 0 ){
			error( "Could not find the address of twitserver with %s and %s: (%s)\n",
				addr,
				port,
				gai_strerror( status )
				);
			status = -1;
			break;
		}

		// Create a socket
		errno = 0;
		if ( ( socketfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ){
			error( "Failed to create a socket: (%s)\n",
				strerror( errno )
				);
			status = -1;
			break;
		}

		// Connect that socket with the twitserver
		while ( connect( socketfd, infop->ai_addr, infop->ai_addrlen ) == -1 ){
			if ( errno == EINTR ){
				continue;	
			}
			error( "failed to connect to the twitserver: (%s)\n", strerror( errno ) );
			status = -1;
			break;
		}

		// If ok till now the function will return the socket fd
		if ( status == 0 ){
			status = socketfd;
		}
	}while ( 0 );

	// Clean-up code
	if( status == -1 ){
		if ( infop != NULL ){
			freeaddrinfo( infop );
		}
		if ( socketfd != -1 ){
			errno = 0;
			if ( safe_close( socketfd ) ){
				error( "failed to close socket: (%s)\n", strerror( errno ) );
			}
		}
	}
	
	return ( status );
}
// Establish a connection with the twitserver. Return the twitserver file descriptor if ok and -1 otherwise
Establish a connection with the twitserver. Return the twitserver file descriptor if ok and -1 otherwise
Establish a connection with the twitserver . Return the twitserver file descriptor if ok and - 1 otherwise
int connect_to_twitserver( const char * restrict addr, const char * restrict port ){
	struct addrinfo *infop = NULL;
	struct addrinfo hint;
	int status = 0;
	int socketfd = -1;
	assert( addr != NULL );
	assert( port != NULL );
	do{
		( void )memset( &hint, 0, sizeof( hint ) );
		hint.ai_family = AF_INET;
		hint.ai_socktype = SOCK_STREAM;
		hint.ai_protocol = 0;
		if ( ( status = getaddrinfo( addr, port, &hint, &infop ) ) != 0 ){
			error( "Could not find the address of twitserver with %s and %s: (%s)\n",
				addr,
				port,
				gai_strerror( status )
				);
			status = -1;
			break;
		}
		errno = 0;
		if ( ( socketfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ){
			error( "Failed to create a socket: (%s)\n",
				strerror( errno )
				);
			status = -1;
			break;
		}
		while ( connect( socketfd, infop->ai_addr, infop->ai_addrlen ) == -1 ){
			if ( errno == EINTR ){
				continue;	
			}
			error( "failed to connect to the twitserver: (%s)\n", strerror( errno ) );
			status = -1;
			break;
		}
		if ( status == 0 ){
			status = socketfd;
		}
	}while ( 0 );
	if( status == -1 ){
		if ( infop != NULL ){
			freeaddrinfo( infop );
		}
		if ( socketfd != -1 ){
			errno = 0;
			if ( safe_close( socketfd ) ){
				error( "failed to close socket: (%s)\n", strerror( errno ) );
			}
		}
	}
	return ( status );
}
int connect_to_twitserver ( const char * restrict addr , const char * restrict port ) { struct addrinfo * infop = NULL ; struct addrinfo hint ; int status = 0 ; int socketfd = -1 ; assert ( addr != NULL ) ; assert ( port != NULL ) ; do { ( void ) memset ( & hint , 0 , sizeof ( hint ) ) ; hint . ai_family = AF_INET ; hint . ai_socktype = SOCK_STREAM ; hint . ai_protocol = 0 ; if ( ( status = getaddrinfo ( addr , port , & hint , & infop ) ) != 0 ) { error ( " \n " , addr , port , gai_strerror ( status ) ) ; status = -1 ; break ; } errno = 0 ; if ( ( socketfd = socket ( AF_INET , SOCK_STREAM , 0 ) ) == -1 ) { error ( " \n " , strerror ( errno ) ) ; status = -1 ; break ; } while ( connect ( socketfd , infop -> ai_addr , infop -> ai_addrlen ) == -1 ) { if ( errno == EINTR ) { continue ; } error ( " \n " , strerror ( errno ) ) ; status = -1 ; break ; } if ( status == 0 ) { status = socketfd ; } } while ( 0 ) ; if ( status == -1 ) { if ( infop != NULL ) { freeaddrinfo ( infop ) ; } if ( socketfd != -1 ) { errno = 0 ; if ( safe_close ( socketfd ) ) { error ( " \n " , strerror ( errno ) ) ; } } } return ( status ) ; }
Establish a connection with the twitserver.
Establish a connection with the twitserver .
// Get the addr of the twitserver // Create a socket // Connect that socket with the twitserver // If ok till now the function will return the socket fd // Clean-up code