This is a basic TCP file server implemented in C using the socket API. It listens for incoming connections and sends the requested file to the client.
- Creates a TCP socket.
- Binds to port
8080(0x901f in hex, which is little-endian for 8080). - Listens for incoming connections.
- Accepts a client connection.
- Reads the client's request (expects an HTTP-like request format:
GET /filename). - Extracts the requested filename and attempts to open it.
- Sends the file's contents back to the client.
- Closes the connection.
- A Linux-based system with GCC installed.
- Basic understanding of sockets and system calls.
Compile the server using:
gcc -o file_server file_server.cRun the server:
./file_serverThen, from another terminal, request a file using wget:
wget -O downloaded_file.txt http://localhost:8080/testfile.txt- The server expects a request in the format
GET /filename. - It does not handle HTTP responses properly and sends only raw file data.
- The buffer size for
sendfileis limited to 256 bytes, which may not work well for larger files. - This implementation lacks error handling for invalid or missing files.
This is a minimal implementation and should not be used in production. It lacks security measures such as input validation and proper error handling.