{"id":31703859,"url":"https://github.com/pak-app/c-raw-tcp-server","last_synced_at":"2025-10-25T01:10:15.495Z","repository":{"id":311576883,"uuid":"1039733472","full_name":"pak-app/c-raw-tcp-server","owner":"pak-app","description":"Raw TCP server using C language from scratch","archived":false,"fork":false,"pushed_at":"2025-08-25T08:36:12.000Z","size":41,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-25T10:57:57.595Z","etag":null,"topics":["clanguage","net","server","tcp-server"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pak-app.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-17T21:45:26.000Z","updated_at":"2025-08-25T08:36:15.000Z","dependencies_parsed_at":"2025-08-25T10:58:02.633Z","dependency_job_id":"949244ac-7735-454d-b94f-6308cc59d8d2","html_url":"https://github.com/pak-app/c-raw-tcp-server","commit_stats":null,"previous_names":["pak-app/c-raw-tcp-server"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/pak-app/c-raw-tcp-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pak-app%2Fc-raw-tcp-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pak-app%2Fc-raw-tcp-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pak-app%2Fc-raw-tcp-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pak-app%2Fc-raw-tcp-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pak-app","download_url":"https://codeload.github.com/pak-app/c-raw-tcp-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pak-app%2Fc-raw-tcp-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000778,"owners_count":26082851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clanguage","net","server","tcp-server"],"created_at":"2025-10-08T22:45:00.656Z","updated_at":"2025-10-08T22:45:04.730Z","avatar_url":"https://github.com/pak-app.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raw TCP Server in C\n\nA lightweight TCP server implementation in **C** with event-driven design.  \nIt allows you to register event listeners for socket communication, send messages, and manage connections gracefully or forcefully.\n\n---\n\n## ✨ Features\n- Event listeners:\n  - `on_data` → Triggered whenever data is received from the client.\n  - `once` → Triggered only on the first message of the connection.\n  - `on_close` → Triggered when a client disconnects or resources are released.\n  - `on_end` → Triggered when the server initiates a graceful end.\n- Send messages with:\n  - `c_socket.emit(...)`\n- Connection management:\n  - `c_socket.end()` → Gracefully close the connection (sends **FIN**).\n  - `c_socket.destroy()` → Immediately close the connection (like sending **RST**).\n\n---\n\n## 📂 Project Structure\n```\n.\n├── src/\n│   ├── main.c      # Example usage\n│   └── tcp.c       # TCP server implementation\n├── include/\n│   └── tcp.h       # Header file\n```\n\n---\n\n## 🚀 Build \u0026 Run\nCompile the server:\n```bash\ngcc src/main.c src/tcp.c -I include -o tcp_server\n```\n\nCompile and run the server:\n```bash\n./tcp_server\n```\n\nBy default, the server listens on **localhost:8080** with a buffer size of **1024 bytes**.\n\n---\n\n## 📖 Usage Example (`main.c`)\n\nHere’s how to define your event handlers and start the server:\n\n```c\n#include \u003cstdio.h\u003e\n#include \"tcp.h\"\n\nvoid on(Socket* c_socket, char *data, int bytes)\n{\n    data[bytes] = '\\0';\n    printf(\"[ON EVENT]: Received: %s\\n\", data);\n    const char response[] = \"Hello client!!!\\n\";\n    c_socket-\u003eemit(response, 0);\n}\n\nvoid once(Socket* c_socket, char *data, int bytes)\n{\n    data[bytes] = '\\0';\n    printf(\"[ONCE EVENT]: Received: %s\\n\", data);\n    const char response[] = \"Hello client for the first connection!!!\\n\";\n    c_socket-\u003eemit(response, 0);\n}\n\nvoid on_close(__attribute__((unused)) Socket* c_socket)\n{\n    printf(\"[CLOSE EVENT]: client disconnected\\n\");\n}\n\nvoid end(__attribute__((unused)) Socket* c_socket)\n{\n    printf(\"[END EVENT] end called.\\n\");\n}\n\nint main(void)\n{\n    Socket* my_socket = server(1024);\n    my_socket-\u003eevents.on_close = on_close;\n    my_socket-\u003eevents.on_data = on;\n    my_socket-\u003eevents.once = once;\n    my_socket-\u003eevents.on_end = end;\n    \n    start_server(my_socket, \"localhost\", 8080);\n\n    return 0;\n}\n```\n\n---\n\n## 🛠 Events Summary\n- **once**  \n  Called on the very first message received after the connection is established. Useful for authentication, initialization, or caching.\n\n- **on_data**  \n  Called on every subsequent message received.\n\n- **on_end**  \n  Triggered when you call `c_socket.end()` → server sends FIN and gracefully shuts down.\n\n- **on_close**  \n  Triggered after connection termination (graceful or forced). This is where you can free resources.\n\n---\n\n## 📡 Connection Management\n- **Send a message to client**\n  ```c\n  c_socket-\u003eemit(\"Hello world!\", 0);\n  ```\n\n- **Graceful close (FIN)**\n  ```c\n  c_socket-\u003eend();\n  ```\n\n- **Force destroy (RST / immediate close)**\n  ```c\n  c_socket-\u003edestroy();\n  ```\n\n---\n\n## 🧪 Test with `netcat`\nOn a second terminal, run:\n```bash\nnc -v localhost 8080\n```\nYou should see the server respond according to your event handlers.\n\n---\n\n## 📌 Notes\n- The server forks for each client (`fork()` in `communication_handler`).\n- Child processes handle communication; the parent keeps listening.\n- Zombie processes are cleaned up via `SIGCHLD` handler.\n\n---\n\n## 📜 License\nMIT License – free to use, modify, and distribute.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpak-app%2Fc-raw-tcp-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpak-app%2Fc-raw-tcp-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpak-app%2Fc-raw-tcp-server/lists"}