Java implementations for client-server practical work using both socket programming and the built-in HTTP server API.
This project contains two independent lab exercises:
SocketServerExamimplements a multi-threaded echo server usingServerSocketandSocket.HttpServerExamimplements a simple HTTP server usingcom.sun.net.httpserver.HttpServer.
Each class can be run separately.
src/main/java/com/mycompany/lab_based_practical_mock_socket/SocketServerExam.javasrc/main/java/com/mycompany/lab_based_practical_mock_httpserver/HttpServerExam.javapom.xml
- Java 17
- Maven 3.8+ or later
From the project root:
mvn clean packageThis compiles the sources into target/classes.
The socket server listens on port 7000 and echoes each line sent by a client.
java -cp target/classes com.mycompany.lab_based_practical_mock_socket.SocketServerExamExample client test:
nc localhost 7000Type a message and press Enter. The server responds with Echo: <message>.
The HTTP server listens on port 8000.
java -cp target/classes com.mycompany.lab_based_practical_mock_httpserver.HttpServerExamAvailable endpoints:
GET /returns a simple HTML welcome page.POST /datareturns a mock JSON response withContent-Type: application/json.
Example requests:
curl http://localhost:8000/
curl -X POST http://localhost:8000/data -H "Content-Type: application/json" -d '{"name":"test"}'- The socket server handles each client connection on a separate thread.
- Both servers are intentionally simple and match the lab-style TODO structure in the source code.
- If you use Windows PowerShell, you can run the same
java -cp ...commands from the project root.