number, &hints, &server_addr); if (rc != 0) { on_func_failure(“Failed to resolve hostname”); } getaddrinfo(CPP_HOSTNAME, port_number, &hints, &server_addr): Resolves the hostname (CPP_HOSTNAME) and port number to an IP address and protocol-specific address structure. The function fills in the struct addrinfo server_addr with the resolved information. If it fails, the program exits. Create A Socket int client_socket = socket(server_addr->ai_family, server_addr->ai_socktype, server_addr->ai_protocol); if (client_socket == -1) { on_func_failure(“Failed to create a socket”); } socket(server_addr->ai_family, server_addr->ai_socktype, server_addr->ai_protocol): Creates a new socket using the address information provided by getaddrinfo. The socket is assigned to client_socket for further use. Connect to HTTP server int result = connect(client_socket, server_addr->ai_addr, server_addr->ai_addrlen); if (result == -1) { on_func_failure(“Failed to connect to the server”); } connect(client_socket, server_addr->ai_addr, server_addr->ai_addrlen): Initiates a connection to the HTTP
https://github.com/nguyenchiemminhvu/LinuxNetworkProgramming