{"id":31548115,"url":"https://github.com/s-r-e-e-r-a-j/csocket","last_synced_at":"2026-05-03T10:34:31.104Z","repository":{"id":315908161,"uuid":"1061194798","full_name":"s-r-e-e-r-a-j/CSocket","owner":"s-r-e-e-r-a-j","description":"CSocket is a powerful header-only C networking library supporting IPv4, IPv6, TCP, UDP, raw sockets, SSL/TLS, threading, host resolution, and advanced socket options.","archived":false,"fork":false,"pushed_at":"2026-04-12T08:58:07.000Z","size":35,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-12T10:24:20.536Z","etag":null,"topics":["archlinux","clibrary","cnetworkinglibrary","cprogramming","cprogramming-language","csocket","debian","header-only-library","linux","network-programming","networking","rhel","socketlibrary","termux"],"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/s-r-e-e-r-a-j.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-21T12:43:14.000Z","updated_at":"2026-04-12T08:58:11.000Z","dependencies_parsed_at":"2025-09-21T15:33:20.318Z","dependency_job_id":"3aff6734-6a67-4bda-a50a-f64bfae18540","html_url":"https://github.com/s-r-e-e-r-a-j/CSocket","commit_stats":null,"previous_names":["s-r-e-e-r-a-j/csocket"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/s-r-e-e-r-a-j/CSocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-r-e-e-r-a-j%2FCSocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-r-e-e-r-a-j%2FCSocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-r-e-e-r-a-j%2FCSocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-r-e-e-r-a-j%2FCSocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s-r-e-e-r-a-j","download_url":"https://codeload.github.com/s-r-e-e-r-a-j/CSocket/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-r-e-e-r-a-j%2FCSocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32566444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T06:36:36.687Z","status":"ssl_error","status_checked_at":"2026-05-03T06:36:09.306Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["archlinux","clibrary","cnetworkinglibrary","cprogramming","cprogramming-language","csocket","debian","header-only-library","linux","network-programming","networking","rhel","socketlibrary","termux"],"created_at":"2025-10-04T16:03:48.391Z","updated_at":"2026-05-03T10:34:31.087Z","avatar_url":"https://github.com/s-r-e-e-r-a-j.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"## CSocket \n\n**CSocket** is a powerful, header-only C networking library that simplifies working with TCP, UDP, and RAW sockets for IPv4 and IPv6. It includes SSL/TLS support, threaded TCP servers, host resolution, and advanced socket options.\n\n## Features\n\n- **Socket Types**: TCP, UDP, RAW\n\n- **IP Versions**: IPv4, IPv6\n\n- **SSL/TLS**: Supports SSL/TLS communication. The server can use a custom certificate and private key, or automatically generate a self-signed certificate and key if none are provided. Clients can securely connect over SSL/TLS.\n\n- **Blocking/Non-blocking sockets**\n\n- **Timeouts \u0026 Reuse address options**\n\n- **Threaded TCP servers** with automatic client handling\n\n- **Host resolution** (DNS → IP)\n\n- **Raw socket support** for advanced packet crafting\n\n- **Header-only**: just include the header, no extra compilation required\n\n- **Powerful**: supports SSL, threaded servers, and RAW sockets in a single, easy-to-use API.\n\n## Compatibility\n- Linux (Debian, RHEL, Arch, etc..)\n- Termux(Android)\n\n## Setup\n\n1. **Place `CSocket.h` in the same directory as your `.c` source file.**\n   \nExample:\n\n```bash\nproject/\n├── CSocket.h\n└── your_program.c\n```\n\n2. **Include the header**\n   \nAt the top of your `.c` file, include the library:\n\n```c\n#include \"CSocket.h\"\n```\n\n3. **Install OpenSSL**(required for SSL/TLS features).\n\n**Debian/Ubuntu/Parrot/Kali:**\n```bash\nsudo apt update\nsudo apt install libssl-dev\n```\n\n**RHEL/CentOS/Fedora:**\n```bash\nsudo dnf install openssl-devel\n```\n\n**Arch Linux/Manjaro:**\n```bash\nsudo pacman -S openssl\n```\n\n**Termux (Android):**\n```bash\npkg install openssl\n```\n\n4. **Compile your program**\nLink with **pthread** and **OpenSSL**:\n\n```bash\ngcc your_program.c -o your_program -pthread -lssl -lcrypto\n```\n\n5. **Run your program**\n\n```bash\n./your_program\n```\n\n## API Examples\n\n\n1. **Create Socket**\n```c\nCSocket CSocket_create(CS_Type type, CS_Family family);\n```\n- **type** → `CS_TCP`, `CS_UDP`, `CS_RAW`\n\n- **family** → `CS_AF_INET` (IPv4), `CS_AF_INET6` (IPv6)\n\n**Example:**\n\n```c\nCSocket tcp_sock = CSocket_create(CS_TCP, CS_AF_INET);\nCSocket udp_sock = CSocket_create(CS_UDP, CS_AF_INET6);\n```\n\n---\n\n2. **Close Socket**\n```c\nvoid CSocket_close(CSocket *s);\n```\n- `CSocket_close` – Closes the socket and cleans up associated resources\n(SSL, context, file descriptor).\n\n**Example:**\n```c\nCSocket_close(\u0026tcp_sock);\nCSocket_close(\u0026udp_sock);\n```\n\n---\n\n3. **Set Blocking / Non-blocking**\n```c\nvoid CSocket_set_blocking(CSocket *s, bool blocking);\n```\n- **Non-blocking:** sockets return immediately if no data is available, instead of waiting.\n\n- **Blocking:** sockets wait until data is available before returning.\n\n**Example:**\n```c\nCSocket_set_blocking(\u0026tcp_sock, false); // Non-blocking\n```\n\n---\n\n4. **Set Timeout**\n```c\nvoid CSocket_set_timeout(CSocket *s, int sec);\n```\n- `CSocket_set_timeout` – Sets a time limit for sending and receiving data on a socket.\n  \n**Example:**\n```c\nCSocket_set_timeout(\u0026udp_sock, 5); // 5 seconds timeout\n```\n\n---\n\n5. **Set Reuse Address**\n```c\nvoid CSocket_set_reuseaddr(CSocket *s, bool reuse);\n```\n- `CSocket_set_reuseaddr` – Allows a server socket to reuse a port immediately after it has been closed, preventing “Address already in use” errors when restarting the server.\n\n**Example:**\n```c\nCSocket_set_reuseaddr(\u0026tcp_sock, true);\n```\n\n---\n\n6. **CSocket_bind**\n```c\nbool CSocket_bind(CSocket *s, const char *host, uint16_t port);\n```\n- `CSocket_bind` – Assigns IP and port to a socket.\n\n**Example (TCP):**\n```c\nCSocket_bind(\u0026tcp_sock, \"127.0.0.1\", 5555);\n```\n\n**Example (UDP):**\n```c\nCSocket_bind(\u0026udp_sock, \"0.0.0.0\", 5000);\n```\n\n---\n\n7. **CSocket_listen (TCP only)**\n```c\nbool CSocket_listen(CSocket *s, int backlog);\n```\n- `CSocket_listen` – Prepares a TCP socket to accept incoming connections.\n  \n**Example:**\n  \n```c\nCSocket_listen(\u0026tcp_sock, 10); // Max 10 pending connections in the queue\n```\n\n---\n\n8. **CSocket_accept (TCP only)**\n```c\nCSocket CSocket_accept(CSocket *s);\n```\n\n- `CSocket_accept` – Accepts a new client connection on a server socket.\n\n**Example:**\n```c\nCSocket client = CSocket_accept(\u0026tcp_sock);\n```\n\n---\n\n9. **CSocket_connect**\n```c\nbool CSocket_connect(CSocket *s, const char *host, uint16_t port);\n```\n\n- `CSocket_connect` – Connects a client socket to a server at a given IP and port.\n\n**Example (TCP client):**\n```c\nCSocket_connect(\u0026tcp_sock, \"127.0.0.1\", 5555);\n```\n**Example (UDP client, optional bind not needed):**\n```c\nCSocket_connect(\u0026udp_sock, \"127.0.0.1\", 5000);\n```\n\n---\n\n10. **CSocket_send**\n```c\nssize_t CSocket_send(CSocket *s, const void *buf, size_t len, const char *host, uint16_t port);\n```\n\n**Examples:**\n\n**TCP (connection-oriented, host/port not needed):**\n\n```c\nconst char *msg = \"Hello TCP\";\nCSocket_send(\u0026tcp_sock, msg, strlen(msg), NULL, 0);\n```\n`len = strlen(msg)` → Number of bytes to send\n\n`host` and `port` are ignored for TCP because the socket is already connected\n\n**UDP (connectionless, host/port required):**\n```c\nconst char *msg = \"Hello UDP\";\nCSocket_send(\u0026udp_sock, msg, strlen(msg), \"127.0.0.1\", 5000);\n```\n\n\n`len = strlen(msg)` → Number of bytes to send\n\n`host = \"127.0.0.1\"` → Destination IP\n\n`port = 5000` → Destination port\n\n---\n\n11. **CSocket_recv**\n```c\nssize_t CSocket_recv(CSocket *s, void *buf, size_t len, char *host, uint16_t *port);\n```\n- **s** → Pointer to `CSocket` object.\n\n- **buf** → Buffer to store received data.\n\n- **len** → Size of the buffer.\n\n- **host** → (UDP only) Optional, stores sender’s IP.\n\n- **port** → (UDP only) Optional, stores sender’s port.\n\n**Example (TCP):**\n```c\nchar buf[1024];\nCSocket_recv(\u0026tcp_sock, buf, sizeof(buf), NULL, NULL);\n```\nFor TCP, `host` and `port` are ignored, so `NULL` can be passed.    \n\n\n**Example (UDP):**\n\n```c\nchar buf[1024];\nchar sender[128];\nuint16_t sender_port;\nCSocket_recv(\u0026udp_sock, buf, sizeof(buf), sender, \u0026sender_port);\n```\nFor UDP, you usually want to know who sent the packet, so provide `host` and `port`.\n\n---\n\n12. **CSocket_sendall**\n```c\nbool CSocket_sendall(CSocket *s, const void *buf, size_t len);\n```\n\n- **s** → Pointer to `CSocket`.\n\n- **buf** → Data buffer to send.\n\n- **len** → Number of bytes to send.\n\n**Example (TCP):**\n```c\nconst char *msg = \"This will send the full message even if split into chunks\";\nCSocket_sendall(\u0026tcp_sock, msg, strlen(msg));\n```\n- Sends all **data** (retries internally if needed).\n\n- Supports **plain TCP** and **TCP over SSL/TLS** (stream sockets).\n\n- Not used for UDP (datagrams).\n\n---\n\n13. **CSocket_recv_line**\n```c\nssize_t CSocket_recv_line(CSocket *s, char *buf, size_t max_len);\n```\n\n- Reads until a newline `\\n` is received.\n\n- **TCP-only** (stream-oriented).\n\n**Example (TCP):**\n```c\nchar buf[1024];\nCSocket_recv_line(\u0026tcp_sock, buf, sizeof(buf));\n```\n\n---\n\n14. **CSocket_recv_until**\n```c\nssize_t CSocket_recv_until(CSocket *s, char *buf, size_t max_len, char delim);\n```\n\n- Reads from the socket until the given delimiter character `delim` is found.\n\n- **TCP-only** (stream-oriented).\n\n- The delimiter is **any character you choose**. Common examples:\n\n   - `'\\n'` → read until      newline (line-based  protocol).\n\n   - `';'` → read until semicolon.\n\n   - `':'` → read until colon.\n\n\n**Example (TCP, read until `;`):**\n```c\nchar buf[1024];\nCSocket_recv_until(\u0026tcp_sock, buf, sizeof(buf), ';'); // reads until ';' is found\n```\n\n**Example (TCP, read until newline):**\n```c\nchar buf[1024];\nCSocket_recv_until(\u0026tcp_sock, buf, sizeof(buf), '\\n'); // reads until '\\n'\n```\n\n---\n\n15. **CSocket_resolve_host**\n```c\nbool CSocket_resolve_host(const char *hostname, char *ip, CS_Family family);\n```\n\n- Resolves a hostname to an IP address (IPv4 or IPv6).\n\n- Returns `true` on success, `false` on failure.\n\n**Example (IPv4):**\n```c\nchar ip[CS_MAX_ADDR_LEN];\nif (CSocket_resolve_host(\"facebook.com\", ip, CS_AF_INET)) {\n    printf(\"Resolved IP: %s\\n\", ip);\n} else {\n    printf(\"Failed to resolve hostname\\n\");\n}\n```\n\n**Example (IPv6):**\n```c\nchar ip[CS_MAX_ADDR_LEN];\nif (CSocket_resolve_host(\"facebook.com\", ip, CS_AF_INET6)) {\n    printf(\"Resolved IP: %s\\n\", ip);\n} else {\n    printf(\"Failed to resolve hostname\\n\");\n}\n```\n\n- For **IPv4**, the resolved IP will be in dotted-decimal form (e.g., `127.0.0.1`).\n\n- For **IPv6**, the resolved IP will be in standard IPv6 format.\n\n- Always check the return value: `true` = success, `false` = failure.\n\n---\n\n\n16. **CSocket_init_ssl**\n```c\nvoid CSocket_init_ssl();\n```\n\n- Initializes the OpenSSL library.\n\n- Must be called **once before using SSL sockets**.\n\n**Example:**\n```c\nCSocket_init_ssl();\n```\n---\n\n17. **CSocket_cleanup_ssl**\n```c\nvoid CSocket_cleanup_ssl();\n```\n\n- Cleans up OpenSSL resources.\n\n- Call this when your program is done with SSL.\n\n**Example:**\n```c\nCSocket_cleanup_ssl();\n```\n\n---\n\n18. **CSocket_enable_ssl**\n```c\nbool CSocket_enable_ssl(CSocket *s, bool server, const char *cert_file, const char *key_file);\n```\n\n- Enables SSL/TLS for a socket.\n\n- Arguments:\n\n  - `s` → pointer to your `CSocket`.\n\n  - `server` → `true` for server mode, `false` for client mode.\n\n  - `cert_file` → path to certificate file (required for server).\n\n  - `key_file` → path to private key file (required for server).\n\n- Returns `true` on success, `false` on error.\n\n- If the certificate/key file is missing, a self-signed certificate and key is automatically generated.\n\n**Example (Server):**\n```c\nCSocket tcp_server = CSocket_create(CS_TCP, CS_AF_INET);\nCSocket_bind(\u0026tcp_server, \"127.0.0.1\", 4433);\nCSocket_listen(\u0026tcp_server, 5);\n\nCSocket_enable_ssl(\u0026tcp_server, true, \"server.crt\", \"server.key\");\n```\n\n**Example (Client):**\n```c\nCSocket tcp_client = CSocket_create(CS_TCP, CS_AF_INET);\nCSocket_connect(\u0026tcp_client, \"127.0.0.1\", 4433);\nCSocket_enable_ssl(\u0026tcp_client, false, NULL, NULL);\n```\n\n---\n\n19. **CSocket_ssl_send**\n```c\nssize_t CSocket_ssl_send(CSocket *s, const void *buf, size_t len);\n```\n\n- Sends data over an SSL/TLS connection.\n\n- Arguments:\n\n  - `s` → pointer to your SSL-enabled `CSocket`.\n\n  - `buf` → data buffer to send.\n\n  - `len` → length of data.\n\n- Returns `-1` on error.\n\n**Example:**\n```c\nCSocket_ssl_send(\u0026tcp_client, \"Hello SSL\", strlen(\"Hello SSL\"));\n```\n\n---\n\n20. **CSocket_ssl_recv**\n```c\nssize_t CSocket_ssl_recv(CSocket *s, void *buf, size_t len);\n```\n\n- Receives data over an SSL/TLS connection.\n\n- Arguments:\n\n  - `s` → pointer to your SSL-enabled `CSocket`.\n\n  - `buf` → buffer to store received data.\n\n  - `len` → size of buffer.\n\n- Returns -1 on error.\n\n**Example:**\n```c\nchar buf[1024];\nCSocket_ssl_recv(\u0026tcp_client, buf, sizeof(buf));\n```\n\n---\n\n21. **CSocket_start_threaded_server**\n- **TCP-only**\n```c\nbool CSocket_start_threaded_server(\n    CSocket *server, \n    int backlog, \n    CSocket_client_handler handler, \n    void *userdata\n);\n```\n-  `CSocket_start_threaded_server` – Starts a TCP server that automatically accepts clients and creates a new thread for each connection, running the provided handler function.\n\n**Example**\n```c\n#include \"CSocket.h\"\n#include \u003cstdio.h\u003e\n\nvoid handle_client(CSocket client, void *userdata) {\n    char buffer[1024];\n    ssize_t n;\n    while((n = CSocket_recv(\u0026client, buffer, sizeof(buffer), NULL, NULL)) \u003e 0) {\n        // For text-based data, null-terminate for printf\n        if(n \u003c sizeof(buffer)) buffer[n] = '\\0';\n        printf(\"Received: %s\\n\", buffer);\n        CSocket_send(\u0026client, buffer, n, NULL, 0); // echo back\n    }\n}\n\nint main() {\n    CSocket server = CSocket_create(CS_TCP, CS_AF_INET);\n    CSocket_bind(\u0026server, \"0.0.0.0\", 5555);\n\n    printf(\"Starting threaded TCP server on port 5555...\\n\");\n    CSocket_start_threaded_server(\u0026server, 10, handle_client, NULL);\n    CSocket_close(\u0026server);\n    return 0;\n}\n\n```\n\n## Example program's \n\n### TCP Server\n```c\n\n#include \"CSocket.h\"  // Include our CSocket library\n#include \u003cstdio.h\u003e    // Include standard I/O functions\n\nint main() {\n    // Create a TCP socket using IPv4\n    CSocket server = CSocket_create(CS_TCP, CS_AF_INET);\n\n    // Allow the server to reuse the port immediately after restarting\n    CSocket_set_reuseaddr(\u0026server, true);\n\n    // Bind the server to all network interfaces on port 8080\n    if (!CSocket_bind(\u0026server, \"0.0.0.0\", 8080)) {\n        perror(\"Bind failed\");\n        return 1;\n    }\n\n    // Start listening for incoming connections\n    if (!CSocket_listen(\u0026server, 5)) {\n        perror(\"Listen failed\");\n        return 1;\n    }\n\n    printf(\"Server listening on port 8080...\\n\");\n\n    // Accept a single client connection\n    CSocket client = CSocket_accept(\u0026server);\n\n    if (client.fd \u003c 0) {\n        perror(\"Accept failed\");\n        CSocket_close(\u0026server);\n        return 1;\n    }\n\n    printf(\"Client connected!\\n\");\n\n    // Set a 5-second timeout for receiving data\n    CSocket_set_timeout(\u0026client, 5);\n\n    // Make the socket blocking(wait for responses)\n    CSocket_set_blocking(\u0026client, true);\n\n    char buffer[CS_BUFFER_SIZE];  // Buffer for incoming data\n    const char *reply = \"Message received!\\n\";  // Custom message to send\n\n    while (1) {\n        // Receive data from client\n        ssize_t r = CSocket_recv(\u0026client, buffer, sizeof(buffer)-1, NULL, NULL);\n\n        // If client disconnected or timed out, break\n        if (r \u003c= 0) {\n            printf(\"Client disconnected or timeout.\\n\");\n            break;\n        }\n\n        buffer[r] = '\\0';              // Null-terminate string\n        printf(\"Received: %s\\n\", buffer); // Print received message\n\n        // Send  reply to client\n        CSocket_send(\u0026client, reply, strlen(reply), NULL, 0);\n    }\n\n    // Close sockets\n    CSocket_close(\u0026client);\n    CSocket_close(\u0026server);\n\n    return 0;\n}\n\n```\n\n### TCP Client\n\n```c\n#include \"CSocket.h\"  // Include our CSocket library\n#include \u003cstdio.h\u003e    // Include standard I/O functions\n\nint main() {\n    // Create a TCP client socket using IPv4\n    CSocket client = CSocket_create(CS_TCP, CS_AF_INET);\n\n    // Set a 5-second timeout for receiving data\n    CSocket_set_timeout(\u0026client, 5);\n\n    // Make the socket blocking (wait for responses)\n    CSocket_set_blocking(\u0026client, true);\n\n    // Connect to the server at 127.0.0.1:8080\n    if (!CSocket_connect(\u0026client, \"127.0.0.1\", 8080)) {\n        perror(\"Connect failed\");  // Print error if connection fails\n        return 1;                  // Exit program\n    }\n\n    printf(\"Connected to server.\\n\");  // Inform user that connection succeeded\n\n    const char *msg = \"Hello Server!\\n\";  // Message to send\n\n    // Send the message to the server\n    CSocket_send(\u0026client, msg, strlen(msg), NULL, 0);\n\n    char buffer[CS_BUFFER_SIZE];  // Buffer to store server response\n\n    // Receive response from the server\n    ssize_t r = CSocket_recv(\u0026client, buffer, sizeof(buffer)-1, NULL, NULL);\n\n    if (r \u003e 0) {\n        buffer[r] = '\\0';                // Null-terminate the string\n        printf(\"Server replied: %s\\n\", buffer);  // Print server reply\n    } else {\n        printf(\"No reply (timeout or error)\\n\"); // Inform user if no response\n    }\n\n    // Close the client socket\n    CSocket_close(\u0026client);\n\n    return 0;  // Exit program successfully\n}\n\n```\n\n### UDP Server\n```c\n#include \"CSocket.h\"\n#include \u003cstdio.h\u003e\n#include \u003cstring.h\u003e\n\n#define SERVER_PORT 9090\n\nint main() {\n    // Create a UDP socket using IPv4\n    CSocket server = CSocket_create(CS_UDP, CS_AF_INET);\n\n    // Allow the server to reuse the port immediately\n    CSocket_set_reuseaddr(\u0026server, true);\n\n    // Bind the server to all interfaces on port 9090\n    if (!CSocket_bind(\u0026server, \"0.0.0.0\", SERVER_PORT)) {\n        perror(\"Bind failed\");\n        return 1;\n    }\n\n    printf(\"[UDP Server] Listening on port %d...\\n\", SERVER_PORT);\n\n    // blocking mode(wait for responses)\n    CSocket_set_blocking(\u0026server, true);\n\n    char buffer[CS_BUFFER_SIZE];\n    char client_ip[CS_MAX_ADDR_LEN];\n    uint16_t client_port;\n\n    while (1) {\n        // Receive message from any client\n        ssize_t r = CSocket_recv(\u0026server, buffer, sizeof(buffer)-1, client_ip, \u0026client_port);\n        if (r \u003c= 0) {\n            printf(\"[UDP Server] Timeout or error, waiting for new messages...\\n\");\n            continue;\n        }\n\n        buffer[r] = '\\0';   // Null-terminate the string\n        printf(\"[UDP Server] Received from %s:%d -\u003e %s\\n\", client_ip, client_port, buffer);\n\n        // Send a reply\n        const char *reply = \"UDP Message received!\\n\";\n        CSocket_send(\u0026server, reply, strlen(reply), client_ip, client_port);\n    }\n\n    CSocket_close(\u0026server);\n    return 0;\n}\n```\n\n### UDP Client \n```c\n#include \"CSocket.h\"\n#include \u003cstdio.h\u003e\n#include \u003cstring.h\u003e\n\n#define SERVER_IP \"127.0.0.1\"\n#define SERVER_PORT 9090\n\nint main() {\n    // Create a UDP client socket using IPv4\n    CSocket client = CSocket_create(CS_UDP, CS_AF_INET);\n\n    // blocking mode(wait for responses)\n    CSocket_set_blocking(\u0026client, true);\n\n    const char *msg = \"Hello UDP Server!\\n\";\n\n    // Send message to server\n    CSocket_send(\u0026client, msg, strlen(msg), SERVER_IP, SERVER_PORT);\n    printf(\"[UDP Client] Sent: %s\", msg);\n\n    char buffer[CS_BUFFER_SIZE];\n\n    // Receive reply from server\n    ssize_t r = CSocket_recv(\u0026client, buffer, sizeof(buffer)-1, NULL, NULL);\n    if (r \u003e 0) {\n        buffer[r] = '\\0';   // Null-terminate the string\n        printf(\"[UDP Client] Server replied: %s\", buffer);\n    } else {\n        printf(\"[UDP Client] No reply (timeout or error)\\n\");\n    }\n\n    CSocket_close(\u0026client);\n    return 0;\n}\n\n```\n### SSL/TLS TCP Server\n```c\n#include \"CSocket.h\"   //  CSocket library\n#include \u003cstdio.h\u003e\n#include \u003cstring.h\u003e\n\n#define SERVER_PORT 8443\n#define CERT_FILE \"server.crt\"   // Path to SSL certificate\n#define KEY_FILE  \"server.key\"   // Path to SSL private key\n\nint main() {\n    // Create a TCP socket\n    CSocket server = CSocket_create(CS_TCP, CS_AF_INET);\n\n    // Initialize SSL\n    CSocket_init_ssl();\n\n    // Enable SSL for this socket\n    CSocket_enable_ssl(\u0026server, true, CERT_FILE, KEY_FILE);\n\n    // Allow reusing the port immediately\n    CSocket_set_reuseaddr(\u0026server, true);\n\n    // Bind to all network interfaces on SERVER_PORT\n    if (!CSocket_bind(\u0026server, \"0.0.0.0\", SERVER_PORT)) {\n        perror(\"[Server] Bind failed\");\n        return 1;\n    }\n\n    // Start listening\n    if (!CSocket_listen(\u0026server, 5)) {\n        perror(\"[Server] Listen failed\");\n        return 1;\n    }\n\n    printf(\"[Server] Listening on port %d (SSL)...\\n\", SERVER_PORT);\n\n    // Accept a client connection\n    CSocket client = CSocket_accept(\u0026server);\n    if (client.fd \u003c 0) {\n        perror(\"[Server] Accept failed\");\n        CSocket_close(\u0026server);\n        return 1;\n    }\n\n    printf(\"[Server] Client connected via SSL!\\n\");\n\n    char buffer[CS_BUFFER_SIZE];\n\n    // Receive a message from the client\n    ssize_t r = CSocket_recv(\u0026client, buffer, sizeof(buffer)-1, NULL, NULL);\n    if (r \u003e 0) {\n        buffer[r] = '\\0';  // Null-terminate the string\n        printf(\"[Server] Received: %s\\n\", buffer);\n\n        // Send a reply\n        const char *reply = \"Hello from SSL Server!\\n\";\n        CSocket_send(\u0026client, reply, strlen(reply), NULL, 0);\n    } else {\n        printf(\"[Server] No message received.\\n\");\n    }\n\n    // Close client and server sockets\n    CSocket_close(\u0026client);\n    CSocket_close(\u0026server);\n    // Cleans up OpenSSL resources\n    CSocket_cleanup_ssl();\n    printf(\"[Server] SSL session closed.\\n\");\n    return 0;\n}\n```\n### SSL/TLS Client\n```c\n#include \"CSocket.h\"   // CSocket library\n#include \u003cstdio.h\u003e\n#include \u003cstring.h\u003e\n\n#define SERVER_IP \"127.0.0.1\"\n#define SERVER_PORT 8443\n\nint main() {\n    // Create a TCP client socket\n    CSocket client = CSocket_create(CS_TCP, CS_AF_INET);\n\n    // Initilaize SSL\n    CSocket_init_ssl();\n\n    // Enable SSL for this client socket\n    CSocket_enable_ssl(\u0026client, false, NULL, NULL);  // No cert and key needed for client\n\n    // Connect to SSL server\n    if (!CSocket_connect(\u0026client, SERVER_IP, SERVER_PORT)) {\n        perror(\"[Client] Connection failed\");\n        return 1;\n    }\n\n    printf(\"[Client] Connected to SSL server!\\n\");\n\n    // Send a message\n    const char *msg = \"Hello SSL Server!\\n\";\n    if (CSocket_send(\u0026client, msg, strlen(msg), NULL, 0) \u003e 0) {\n        printf(\"[Client] Sent: %s\", msg);\n    } else {\n        printf(\"[Client] Failed to send message.\\n\");\n    }\n\n    char buffer[CS_BUFFER_SIZE];\n\n    // Receive reply from server\n    ssize_t r = CSocket_recv(\u0026client, buffer, sizeof(buffer)-1, NULL, NULL);\n    if (r \u003e 0) {\n        buffer[r] = '\\0';   // Null-terminate the string\n        printf(\"[Client] Server replied: %s\", buffer);\n    } else {\n        printf(\"[Client] No reply or error.\\n\");\n    }\n\n    // Close the socket\n    CSocket_close(\u0026client);\n    // Cleans up OpenSSL resources\n    CSocket_cleanup_ssl();\n\n    printf(\"[Client] SSL session closed.\\n\");\n    return 0;\n}\n```\n## License\nThis project is licensed under the MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-r-e-e-r-a-j%2Fcsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs-r-e-e-r-a-j%2Fcsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-r-e-e-r-a-j%2Fcsocket/lists"}