-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPServer.java
More file actions
76 lines (62 loc) · 2.72 KB
/
HTTPServer.java
File metadata and controls
76 lines (62 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;
public class HTTPServer {
public static void main(String[] args) {
//Move out of a static context
new HTTPServer();
}
public HTTPServer() {
//The socket number we will connect to.
//Sockets have to be below 2^16
//So they can have a range of 0 to 65535.
//Lower number are reserved, so we usually pick high numbers
int socket = 5000;
//Try with resources statement
//Create a new server. This server will connect to our local network
//at the given socket number.
try (ServerSocket serverSocket = new ServerSocket(socket)) {
System.out.println("Waiting for a client");
//Loop forever waiting for connections
//We can do this because we run our HTTP server code in a different thread.
while (true) {
//This is a blocking call. accept() will not return until a client connects to our socket.
Socket clientSocket = serverSocket.accept();
//Create an instance of Runnable. Passing this allows us to easily create threads in Java.
Runnable runnable = () -> {
System.out.println("A client connected");
//Try with resource statement that lets us read from the socket (scanner) and write to the socket (PrintStream).
try (Scanner scanner = new Scanner(clientSocket.getInputStream()); PrintStream out = new PrintStream(clientSocket.getOutputStream())) {
//Get the header line
String requestLine = scanner.nextLine();
System.out.println("Request line " + requestLine);
//Read lines from the client until we get a blank line.
String line;
do {
line = scanner.nextLine();
System.out.println(line);
} while (line.trim().length() != 0); //We have a blank line if the line is empty
//Respond with our own header line
String responseLine = "HTTP/1.0 200 OK\n";
//Then with the header response fields
String[] responseHeaderFields = {"Content-Type: text/html;"};
//Concatonate everything with the body of our response
String response = responseLine + String.join("\n", Arrays.asList(responseHeaderFields)) + "\n\nHello, <b>internet 2</b>";
out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
};
//Place the above code in a thread
Thread thread = new Thread(runnable);
//Spawn the thread
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}