package javaWeb原理; import java.io.FileInputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws Exception{ ServerSocket server = new ServerSocket(9999);//开启一个端口与网络连接 Socket sock = server.accept();//等待客户访问,建立连接 FileInputStream in = new FileInputStream("src/javaWeb原理/web.html"); //连接后,读取本地的html文件 OutputStream out = sock.getOutputStream();//建立发送通道 int len = 0 ; byte buffer[] = new byte[1024]; while((len = in.read(buffer))>0){ //System.out.println(new String(buffer,0,len)); out.write(buffer, 0, len);//给客户端浏览器发送html文件 } //发送完毕,关闭连接 in.close(); out.close(); sock.close(); server.close(); //.............后续操作,客户端浏览器自己解析html文档,并显示在浏览器 } }
原文地址:http://blog.csdn.net/u010218226/article/details/44116769