This project implements a basic HTTP server in C, designed to practice network programming. The server listens on port 8002 and handles HTTP GET and POST requests. It serves a simple HTML webpage with a file upload form and supports static file retrieval.
-
Basic HTTP Server:
- Handles HTTP
GETandPOSTrequests. - Serves static files like HTML pages and images.
- Handles HTTP
-
Static File Serving:
- Responds to requests for an HTML page and a static image (
images.jpeg). - Uses
sendfile()to serve files efficiently.
- Responds to requests for an HTML page and a static image (
-
File Upload Simulation:
- Processes
POSTrequests to extract the file name from the upload form.
- Processes
-
Process Management:
- Uses
fork()to handle multiple clients simultaneously. - Prevents zombie processes with signal handling (
SIGCHLD).
- Uses
server.c: Main C program file implementing the server.images.jpeg: Image file served by the server and displayed on the webpage.
Follow these steps to compile and run the server:
Use the following command to compile the server program:
gcc -o server server.c
Run the compiled server program:
./server
Open a web browser and navigate to:
http://<server-ip>:8002
Replace <server-ip> with the actual IP address of the server machine.
- View the HTML Page:
The server responds with a simple HTML page containing a title, an image, and a file upload form. - Upload a File:
Use the upload form on the webpage to send a file. The server extracts the file name and prints it in the terminal:
receive filename: <uploaded-file-name> - Access the Image:
The image (images.jpeg) can be accessed directly by visiting:
http://<server-ip>:8002/images.jpeg
- Creates a socket and binds it to port 8002.
- Configures the server to listen for incoming connections.
- Uses
fork()to spawn child processes for handling individual client connections. - Manages signals (
SIGCHLD) to prevent zombie processes.
GET /:
Responds with a pre-defined HTML webpage.GET /images.jpeg:
Serves the image file usingsendfile().POST /:
Parses the HTTP request body to extract the file name of the uploaded file.
The HTML page served by the server includes:
- A title: "Network Programming Homework 1".
- An image:
images.jpeg. - A file upload form that supports
POSTrequests.
- Each client connection is handled in a separate process created using
fork(). - Signals are used to clean up zombie processes (
SIGCHLD).
When the server starts, it prints:
Server listen address: 0.0.0.0:8002
When a file is uploaded using the form, the server prints:
receive filename: example.txt
- The
POSTrequest processing only extracts the file name and does not store the uploaded file. - Minimal error handling for malformed HTTP requests.
- Not optimized for high-concurrency environments or large-scale applications.
- Implement functionality to save uploaded files.
- Enhance request parsing and error handling.
- Support additional HTTP methods and headers.
- Optimize performance for better handling of concurrent client connections.
This project is intended for learning purposes, focusing on:
- Socket programming in C.
- HTTP protocol basics.
- Client-server communication.