May 2019
Intermediate to advanced
478 pages
11h 21m
English
It is useful to have a helper function that converts a given client's IP address into text. This function is given in the following code snippet:
/*web_server.c except*/const char *get_client_address(struct client_info *ci) { static char address_buffer[100]; getnameinfo((struct sockaddr*)&ci->address, ci->address_length, address_buffer, sizeof(address_buffer), 0, 0, NI_NUMERICHOST); return address_buffer;}
get_client_address() is a simple function. It first allocates a char array to store the IP address in. This char array is declared static, which ensures that its memory is available after the function returns. This means that we don't need to worry about having the caller free() the memory. The downside to this method ...
Read now
Unlock full access