{"id":22886423,"url":"https://github.com/dexter-xd/tcp_server_c","last_synced_at":"2025-04-05T21:06:19.407Z","repository":{"id":260523164,"uuid":"881528878","full_name":"dexter-xD/tcp_server_c","owner":"dexter-xD","description":"TCP server in C with dynamic HTML templating.","archived":false,"fork":false,"pushed_at":"2025-02-07T17:02:12.000Z","size":36,"stargazers_count":127,"open_issues_count":1,"forks_count":15,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T20:03:54.235Z","etag":null,"topics":["c","dynamic-html","tcp-server"],"latest_commit_sha":null,"homepage":"https://medium.com/@trish07/building-a-simple-tcp-web-server-in-c-a-beginners-guide-43d1d494c6c6","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/dexter-xD.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":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-31T18:53:15.000Z","updated_at":"2025-03-26T15:35:35.000Z","dependencies_parsed_at":"2025-02-05T19:44:13.780Z","dependency_job_id":"2826de46-bf51-4c11-84e5-c9e9254ba1ef","html_url":"https://github.com/dexter-xD/tcp_server_c","commit_stats":null,"previous_names":["dexter-xd/tcp_server_c"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dexter-xD%2Ftcp_server_c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dexter-xD%2Ftcp_server_c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dexter-xD%2Ftcp_server_c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dexter-xD%2Ftcp_server_c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dexter-xD","download_url":"https://codeload.github.com/dexter-xD/tcp_server_c/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399871,"owners_count":20932876,"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","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":["c","dynamic-html","tcp-server"],"created_at":"2024-12-13T20:18:45.119Z","updated_at":"2025-04-05T21:06:19.366Z","avatar_url":"https://github.com/dexter-xD.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TCP Server Project\n\nThis project is a simple TCP server written in C, using socket programming concepts. It listens on a specified port, accepts client connections, and serves dynamic HTML content based on incoming requests using a templating system that supports dynamic values, if-else conditions, and loops. The code is organized into multiple files with CMake as the build system.\n\n## Project Structure\n\n```\nproject/\n├── CMakeLists.txt             # Build configuration\n├── include/\n│   ├── server.h               # Main server header file\n│   ├── html_serve.h           # Header for serve_html function\n│   ├── request_handler.h      # Header for handle_client function\n│   ├── template.h             # Header for template processing functions\n│   └── socket_utils.h         # Header for socket utility functions\n├── src/\n│   ├── server.c               # Main server program\n│   ├── html_serve.c           # serve_html function\n│   ├── request_handler.c      # handle_client function\n│   ├── template.c             # Template processing functions\n│   └── socket_utils.c         # Utility functions for socket operations\n└── README.md                  # Project documentation\n```\n\n### Files \n- **`server.c`**: Contains the main server loop, which listens for and accepts client connections.\n- **`html_serve.c`**: Handles HTML file reading and serves HTML content to clients. Contains the `serve_html` function.\n- **`request_handler.c`**: Manages client requests by processing incoming HTTP requests and serving appropriate responses. Contains the `handle_client` function.\n- **`template.c`**: Implements template processing functions to support dynamic values, if-else conditions, and loops in HTML content.\n- **`socket_utils.c`**: Includes utility functions for socket initialization and client data handling. Contains the `initialize_server` and `read_client_data` functions.\n- **`server.h`**: Declares main server-related constants and functions.\n- **`html_serve.h`**: Declares the function used to read and serve HTML files.\n- **`request_handler.h`**: Declares the function to handle client requests.\n- **`template.h`**: Declares functions for processing templates with dynamic content.\n- **`socket_utils.h`**: Declares utility functions for initializing sockets and reading client data.\n\n## Prerequisites\n\n- **CMake** (version 3.10 or higher)\n- **GCC** or another compatible C compiler\n- **Linux** or **WSL** (Windows Subsystem for Linux) recommended for running this server\n\n## Setup Instructions\n\n### 1. Clone the Repository\n\n```bash\ngit clone \u003crepository_url\u003e\ncd project\n```\n\n### 2. Build the Project\n\n1. Create a `build` directory and navigate into it:\n   ```bash\n   mkdir build \u0026\u0026 cd build\n   ```\n   \n2. Generate the makefiles with CMake:\n   ```bash\n   cmake ..\n   ```\n\n3. Compile the project using `make`:\n   ```bash\n   make\n   ```\n\nThis will create an executable file named `server` inside a `bin` directory under the `build` folder.\n\n### 3. Run the Server\n\nAfter building, you can start the server as follows:\n\n```bash\nvalgrind --leak-check=full ./bin/server \n```\n\nThe server will listen on port `8080` (default setting) and process incoming client requests.\n\n## Template Features\n\nThe server supports a templating system that allows for dynamic content in HTML files. The following features are implemented:\n\n### 1. Dynamic Values\n\nYou can replace placeholders in the template with dynamic values. Placeholders are defined as `{{key}}`, where `key` corresponds to the variable you want to insert.\n\n### 2. If-Else Conditions\n\nThe server can handle if-else statements within templates. Use the following syntax:\n\n```\n{% if condition %}\n    Content to show if condition is true\n{% else %}\n    Content to show if condition is false\n{% endif %}\n```\n\n### 3. For Loops\n\nYou can create loops to iterate over a list of items in your templates using the following syntax:\n\n```\n{% for item in items %}\n    Content to repeat for each item\n{% endfor %}\n```\n\n## Example Usage\n\n### HTML Template\n\nCreate an HTML file (e.g., `index.html`) with the following content to test the features:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003eDynamic Template Example\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003eWelcome to the Template Server\u003c/h1\u003e\n    \n    \u003cp\u003e{{ greeting }}\u003c/p\u003e\n\n    {% if is_logged_in %}\n        \u003cp\u003eHello, {{ username }}!\u003c/p\u003e\n    {% else %}\n        \u003cp\u003ePlease log in to access more features.\u003c/p\u003e\n    {% endif %}\n\n    \u003ch2\u003eYour Items:\u003c/h2\u003e\n    \u003cul\u003e\n        {% for item in items %}\n            \u003cli\u003e{{ item }}\u003c/li\u003e\n        {% endfor %}\n    \u003c/ul\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Sending a Request\n\nTo send a request to the server, you can use a web browser or a tool like `curl`. Make sure to specify the correct HTML file to serve based on your request.\n\n## Troubleshooting\n\nIf you encounter errors:\n1. Ensure that no other process is using port `8080`.\n2. Verify that CMake and GCC are correctly installed.\n3. Use the command `netstat -tuln | grep 8080` to check if the port is occupied.\n\n## Acknowledgments\n\n- [CMake](https://cmake.org/) - Cross-platform build system\n- [Beej's Guide to Network Programming](https://beej.us/guide/bgnet/) - A great resource for learning socket programming\n\n---\n\nFeel free to reach out for any questions or improvements!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdexter-xd%2Ftcp_server_c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdexter-xd%2Ftcp_server_c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdexter-xd%2Ftcp_server_c/lists"}