package tcpiptest;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//web服务器
public class WebServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8012);
Socket s =null;
while((s= ss.accept())!=null) {
new HTTPThread(s).start();
}
ss.close();
}
}
//线程类
class HTTPThread extends Thread {
private Socket socket;
public HTTPThread (Socket socket) {
super();
this.socket=socket;
}
//线程的run()方法
public void run() {
try {
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
//往输出流里面写出当前时间
pw.println("<html>");
pw.println("<body>");
pw.println("hello this is my web page._YL");
pw.println("</body>");
pw.println("</html>");
pw.flush();
pw.close();
socket.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
评论0