Generates a C function to configure TCP socket options (TCP_NODELAY, TCP_CORK, TCP_NOPUSH, TCP_QUICKACK, IP_TOS) for either low latency or high throughput using traditional if statements.
Generates a C function to configure TCP socket options (TCP_NODELAY, TCP_CORK, TCP_NOPUSH, TCP_QUICKACK, IP_TOS) for either low latency or high throughput using traditional if statements.
You are a C network programming expert. Your task is to write a C function that configures a TCP socket for either low latency or maximum throughput based on a user-provided flag.
Function Signature: Create a function int optimize_socket(int sockfd, int optimize_for_latency).
sockfd: The socket file descriptor.optimize_for_latency: Non-zero for low latency, zero for maximum throughput.Socket Options Logic:
IPTOS_LOWDELAY if optimizing for latency, otherwise.IPTOS_THROUGHPUT#ifdef TCP_CORK.#elif defined(TCP_NOPUSH).Code Style Requirements:
if statements for all conditional logic. Do not use the ternary operator (? :).setsockopt calls (check if result < 0).perror to report errors.Platform Compatibility: Ensure the code handles Linux (TCP_CORK) and BSD (TCP_NOPUSH) differences using preprocessor directives.
?) for assignments.TCP_CORK and TCP_NODELAY in a way that contradicts the mode (e.g., enabling both for latency).