最近学到了网络编程,有个练习:
模拟BS服务器
模拟网站服务器,使用浏览器访问本身编写的服务端程序,查看网页效果。
踩了两个坑,就是:html
相对于咱们写的服务端类的项目位置
好比个人服务端: 咱们的地址就要这样输入:注意事项:java
②谷歌看不了,建议换其余浏览器看。web
// 写入HTTP协议响应头,固定写法 os.write("HTTP/1.1 200 OK\r\n".getBytes()); os.write("Content‐Type:text/html\r\n".getBytes()); // 必需要写入空行,不然浏览器不解析 os.write("\r\n".getBytes());
写代码思路编程
须要使用HTTP协议,而不能使用HTTPS协议
,这个协议被加密了,因此客户端只能输出加密的请求头。服务端的执行步骤:浏览器
要一直监听请求,由于他传输的不是一张照片
使用多线程,加快传输效率
下面放出所有代码:
html服务端:服务器
import java.io.*; import java.net.ServerSocket; import java.net.Socket; //http://localhost:8888/Advanced/src/Net/BSTCP/web/index.html HTTP/1.1 //不能使用https,加密协议 public class Server { public static void main(String[] args) throws IOException { System.out.println("服务端启动,等待链接。。。。"); //建立一个服务器ServerSocket,和系统要指定的端口号 ServerSocket server = new ServerSocket(8888); //使用accept方法获取到请求的客户端对象(浏览器) Socket socket = server.accept(); //使用Socket对象中的方法getInputStream,获取到网络字节输入流InputStream对象 InputStream is = socket.getInputStream(); //输出客户端的请求 /*byte[] bytes = new byte[1024]; int len = is.read(bytes); System.out.println(new String(bytes,0,len)); System.out.println("cao");*/ //把is字节流,转换成字符流,而且使用缓冲流读取 BufferedReader br = new BufferedReader(new InputStreamReader(is)); //读取第一行GET请求的内容 String s = br.readLine(); //按空格把它分开 String[] s1 = s.split(" "); //把前面的"/"给排除,使用字符串的截取 String path = s1[1].substring(1); System.out.println(path); //建立一个fis流,读取本地的index.html文件 FileInputStream fis = new FileInputStream(path); //获取网络输出流OutputStream,向客户端输出页面 OutputStream os = socket.getOutputStream(); // 写入HTTP协议响应头,固定写法 os.write("HTTP/1.1 200 OK\r\n".getBytes()); os.write("Content‐Type:text/html\r\n".getBytes()); // 必需要写入空行,不然浏览器不解析 os.write("\r\n".getBytes()); //向客户端输出 byte bytes[] = new byte[1024]; int len = 0; while((len = fis.read(bytes))!=-1){ os.write(bytes); } //关闭资源 fis.close(); br.close(); socket.close(); } }
照片服务端网络
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class ImageServer { public static void main(String[] args) throws IOException { System.out.println("服务端启动,等待链接。。。。"); //建立一个服务器ServerSocket,和系统要指定的端口号 ServerSocket server = new ServerSocket(8888); while (true){ //使用accept方法获取到请求的客户端对象(浏览器) Socket socket = server.accept(); //使用Socket对象中的方法getInputStream,获取到网络字节输入流InputStream对象 InputStream is = socket.getInputStream(); //输出客户端的请求 /*byte[] bytes = new byte[1024]; int len = is.read(bytes); System.out.println(new String(bytes,0,len)); System.out.println("cao");*/ //多线程,加快传输速度 new Thread(new Runnable() { @Override public void run() { try{ //把is字节流,转换成字符流,而且使用缓冲流读取 BufferedReader br = new BufferedReader(new InputStreamReader(is)); //读取第一行GET请求的内容 String s = br.readLine(); //按空格把它分开 String[] s1 = s.split(" "); //把前面的"/"给排除,使用字符串的截取 String path = s1[1].substring(1); System.out.println(path); //建立一个fis流,读取本地的index.html文件 FileInputStream fis = new FileInputStream(path); //获取网络输出流OutputStream,向客户端输出页面 OutputStream os = socket.getOutputStream(); // 写入HTTP协议响应头,固定写法 os.write("HTTP/1.1 200 OK\r\n".getBytes()); os.write("Content‐Type:text/html\r\n".getBytes()); // 必需要写入空行,不然浏览器不解析 os.write("\r\n".getBytes()); //向客户端输出 byte bytes[] = new byte[1024]; int len = 0; while((len = fis.read(bytes))!=-1){ os.write(bytes); } //关闭资源 fis.close(); br.close(); socket.close(); }catch (IOException e){ e.printStackTrace(); } } }).start(); } } }