EN
Java - simple single client Fake-HTTP server example (socket server)
10 points
In this short article, we would like to show how using sockets, write a simple single client Fake-HTTP server in Java.
The solution in the article runs a simple Fake-HTTP server that is able to get only one client connection at the same time. The server listens on port 80. Client requests are only printed in the server console, sending back always the same simple web page - this is the reason why we called it Fake-HTTP server.
Server usage steps:
- run Fake-HTTP server (the below Java program),
- open
http://localhost
address in the web browser,The web browser should send some similar text to the server as request:
Where:xxxxxxxxxx
1GET / HTTP/1.1
2Host: localhost
3Connection: keep-alive
4User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Safari/605.1.15
5Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
6Accept-Encoding: gzip, deflate, br
7Accept-Language: en-US,en;q=0.9,pl;q=0.8
8
- each line should be separated with\r\n
,
- last empty line means request end. - as response in the web browser we should see:
The server sends text as a response (it is interpreted by the web browser and displayed as a web page):
Where each line should be separated by:xxxxxxxxxx
1HTTP/1.1 200 OK
2Server: My WWW Server
3Content-Type: text/html
4
56<html>
7<body>
8My site body...
9</body>
10</html>
-\r\n
in the header area,
-\n
in the content area (it is recommended).
Program.java
file:
xxxxxxxxxx
1
package com.example;
2
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.io.OutputStream;
8
import java.io.PrintWriter;
9
import java.net.ServerSocket;
10
import java.net.Socket;
11
import java.nio.charset.StandardCharsets;
12
13
public class Program {
14
15
public static void main(String[] args) throws IOException {
16
17
try (ServerSocket serverSocket = new ServerSocket(80)) { // starting server on port 80
18
while (true) {
19
try (
20
Socket clientSocket = serverSocket.accept(); // waiting for the web browser connection
21
BufferedReader bufferedReader = createReader(clientSocket);
22
PrintWriter outputWriter = createWriter(clientSocket);
23
) {
24
System.out.println("-----------------------------------------");
25
// reading data from the web browser (receiving as text)
26
while (true) {
27
String line = bufferedReader.readLine();
28
if (line == null || line.isEmpty()) {
29
break;
30
}
31
System.out.println(line);
32
}
33
// writing data to the web browser (sending as text)
34
outputWriter.print(
35
"HTTP/1.1 200 OK\r\n" +
36
"Server: My WWW Server\r\n" +
37
"Content-Type: text/html\r\n" +
38
"\r\n" +
39
"<!DOCTYPE html>\n" +
40
"<html>\n" +
41
"<body>\n" +
42
" My site body...\n" +
43
"</body>\n" +
44
"</html>"
45
);
46
outputWriter.flush();
47
} catch (IOException ex) {
48
System.err.println(ex.getMessage());
49
}
50
}
51
// closeObject(serverSocket);
52
}
53
}
54
55
private static BufferedReader createReader(Socket clientSocket) throws IOException {
56
InputStream inputStream = clientSocket.getInputStream();
57
InputStreamReader inputReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
58
return new BufferedReader(inputReader);
59
}
60
61
private static PrintWriter createWriter(Socket clientSocket) throws IOException {
62
OutputStream outputStream = clientSocket.getOutputStream();
63
return new PrintWriter(outputStream, true, StandardCharsets.UTF_8);
64
}
65
66
// private static boolean closeObject(Closeable object) {
67
// if (object == null) {
68
// return false;
69
// }
70
// try {
71
// object.close();
72
// return true;
73
// } catch (IOException ex) {
74
// return false;
75
// }
76
// }
77
}
Example output: