-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySrv.java
More file actions
51 lines (34 loc) · 1.26 KB
/
MySrv.java
File metadata and controls
51 lines (34 loc) · 1.26 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
import java.io.*;
import java.net.*;
public class MySrv {
public static void main(String[] args) throws IOException {
String domain = "localhost"; // or 127.0.0.1
int port = 8080;
ServerSocket listener = new ServerSocket(port);
int n = 0;
while (true) {
System.out.println("Waiting for next client");
Socket sock = listener.accept();
System.out.println("new client arrived, client number " + n++);
PrintStream out = new PrintStream(sock.getOutputStream());
InputStreamReader in = new InputStreamReader(sock.getInputStream());
String req = "";
for (int b = (char)in.read(); b != -1; b = (char)in.read()) {
req += b;
}
System.out.print("client request: " + req);
String CRLF = "\r\n";
String body = "<html><head><h1>Hello, you are web client request number " + n + "</h1></head></html>";
//body = "4\r\nWiki\r\n6\r\npedia \r\n10\r\nin \r\n\r\n\n\nchunks.\r\n0\r\n\r\n";
String head = "HTTP/1.1 200 OK" + CRLF + "Server: Lehman IT" + CRLF + "Content-Length: " +
body.length() + CRLF +
"Set-Cookie: SID=fadsvdgvjdafnkvj" + CRLF + CRLF;
String resp = head + body;
out.print(resp);
in.close();
out.close();
sock.close();
System.out.println("done with that client");
}
}
}