AutoSkill C TCP Socket Optimization Function
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.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/c-tcp-socket-optimization-function" ~/.claude/skills/ecnu-icalk-autoskill-c-tcp-socket-optimization-function && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/c-tcp-socket-optimization-function/SKILL.mdsource content
C TCP Socket Optimization Function
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.
Prompt
Role & Objective
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.
Operational Rules & Constraints
-
Function Signature: Create a function
.int optimize_socket(int sockfd, int optimize_for_latency)
: The socket file descriptor.sockfd
: Non-zero for low latency, zero for maximum throughput.optimize_for_latency- Return 0 on success, -1 on error.
-
Socket Options Logic:
- IP_TOS: Set to
if optimizing for latency,IPTOS_LOWDELAY
otherwise.IPTOS_THROUGHPUT - TCP_NODELAY: Enable (1) for latency, Disable (0) for throughput.
- TCP_CORK (Linux): Disable (0) for latency, Enable (1) for throughput. Use
.#ifdef TCP_CORK - TCP_NOPUSH (BSD): Disable (0) for latency, Enable (1) for throughput. Use
.#elif defined(TCP_NOPUSH) - TCP_QUICKACK: Enable (1) for latency. Do not enable for throughput (or set to 0).
- IP_TOS: Set to
-
Code Style Requirements:
- Use traditional
statements for all conditional logic. Do not use the ternary operator (if
).? : - Include detailed comments explaining what each option does and why it is set for the specific mode.
- Include error checking for
calls (check if result < 0).setsockopt - Use
to report errors.perror
- Use traditional
-
Platform Compatibility: Ensure the code handles Linux (
) and BSD (TCP_CORK
) differences using preprocessor directives.TCP_NOPUSH
Anti-Patterns
- Do not use the ternary operator (
) for assignments.? - Do not mix
andTCP_CORK
in a way that contradicts the mode (e.g., enabling both for latency).TCP_NODELAY - Do not omit error handling.
Triggers
- write a C function setting TCP_NODELAY TCP_CORK TCP_QUICKACK IP_TOS
- optimize socket for low latency or throughput
- socket configuration code with switch
- set tcp options for performance