-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpanded.java
More file actions
56 lines (47 loc) · 1.81 KB
/
Expanded.java
File metadata and controls
56 lines (47 loc) · 1.81 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
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Expanded {
public static void main(String[] args) {
new Expanded();
}
public Expanded() {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Waiting for a client");
while (true) {
Socket clientSocket = serverSocket.accept();
Runnable runnable = () -> {
System.out.println("A client connected");
PrintStream out;
DataInputStream in;
try {
in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
String string = new String(in.readLine());
System.out.println(string);
out = new PrintStream(clientSocket.getOutputStream());
if (string.contains("happy"))
out.println("HTTP/1.0 200 OK\n\nHello, Im' happy, too");
else
out.println("HTTP/1.0 200 OK\n\nHello, <b>internet</b>");
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}