Skip to content
Back to skills

C Tcp Socket Optimization Function

ASecurity

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.

  • 17 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added May 30, 2026
developmentnodeperformance

Security analysis

A100/100

Scanned May 30, 2026

npx -y skills add gabrielmoreira/agent-skills-mirror --skill c-tcp-socket-optimization-function --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of C Tcp Socket Optimization Function?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for C Tcp Socket Optimization Function
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/gabrielmoreira-c-tcp-socket-optimization-function/badge)](https://www.skillsdirectory.com/skills/gabrielmoreira-c-tcp-socket-optimization-function)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "084bed7d-f3ca-4527-9d9d-973ad7e8d09c"
name: "C TCP Socket Optimization Function"
description: "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."
version: "0.1.0"
tags:
  - "C"
  - "TCP"
  - "socket programming"
  - "network optimization"
  - "low latency"
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"
---

# 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
1. **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.
   - Return 0 on success, -1 on error.

2. **Socket Options Logic**:
   - **IP_TOS**: Set to `IPTOS_LOWDELAY` if optimizing for latency, `IPTOS_THROUGHPUT` otherwise.
   - **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).

3. **Code Style Requirements**:
   - Use **traditional `if` statements** for all conditional logic. **Do not use the ternary operator (`? :`)**.
   - Include detailed comments explaining what each option does and why it is set for the specific mode.
   - Include error checking for `setsockopt` calls (check if result < 0).
   - Use `perror` to report errors.

4. **Platform Compatibility**: Ensure the code handles Linux (`TCP_CORK`) and BSD (`TCP_NOPUSH`) differences using preprocessor directives.

# Anti-Patterns
- Do not use the ternary operator (`?`) for assignments.
- Do not mix `TCP_CORK` and `TCP_NODELAY` in a way that contradicts the mode (e.g., enabling both for latency).
- 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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.