-
Notifications
You must be signed in to change notification settings - Fork 0
/
简易Web服务器
42 lines (40 loc) · 1.23 KB
/
简易Web服务器
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
import java.io.*;
import java.net.*;
public class Net {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(80);
System.out.println("Web Server OK!");
while(true) {
Socket s = ss.accept();
vebserv v = new vebserv(s);
Thread t = new Thread(v);
t.start();
}
} catch(Exception e) {}
}
}
class vebserv implements Runnable {
Socket s;
static int i;
public vebserv(Socket s1) {
s = s1;
}
public void run() {
try {
PrintStream out = new PrintStream(s.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String info = in.readLine();
System.out.println("now get: " + info);
out.println("HTTP/1.0");
out.println("Content_Type: text/html");
i++;
String c = "<html><head><title>Java Web</title></head><body><h1>hi this is " + i + "</h1></body></html>";
out.println();
out.println(c);
out.close();
s.close();
in.close();
} catch(Exception e) {}
}
}