(1) open system interconnect
OSI是Open System Interconnection的缩写,意为开放式系统互联 。OSI是一个开放性的通信系统互连参考模型,他是一个定义得非常好的协议规范。OSI模型有7层结构,每层都可以有几个子层。 OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 ;其中高层(即7、6、5、4层)定义了应用程序的功能,下面3层(即3、2、1层)主要面向通过网络的端到端的数据流。
各层功能
应用层
表示层
会话层
传输层
分层优点
(2) http协议
一、
HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则。计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求信息和服务,HTTP目前协议的版本是1.1.HTTP是一种无状态的协议,无状态是指Web浏览器和Web服务器之间不需要建立持久的连接,这意味着当一个客户端向服务器端发出请求,然后Web服务器返回响应(response),连接就被关闭了,在服务器端不保留连接的有关信息.HTTP遵循请求(Request)/应答(Response)模型。Web浏览器向Web服务器发送请求,Web服务器处理请求并返回适当的应答。所有HTTP连接都被构造成一套请求和应答。
HTTP使用内容类型,是指Web服务器向Web浏览器返回的文件都有与之相关的类型。所有这些类型在MIME Internet邮件协议上模型化,即Web服务器告诉Web浏览器该文件所具有的种类,是HTML文档、GIF格式图像、声音文件还是独立的应用程序。大多数Web浏览器都拥有一系列的可配置的辅助应用程序,它们告诉浏览器应该如何处理Web服务器发送过来的各种内容类型。
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤:
GET /index.html?name=123&pwd=5456 HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-US,zh-CN;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2)
Accept-Encoding: gzip, deflate
Host: localhost:8888
Connection: Keep-Alive
POST /index.html HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-US,zh-CN;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8888
Content-Length: 17
Connection: Keep-Alive
Cache-Control: no-cache
uname=123&pwd=213&fav=0&fav=1&fav=2
<head>
协议状态代码描述HTTP响应的第一行类似于HTTP请求的第一行,它表示通信所用的协议是HTTP1.1服务器已经成功的处理了客户端发出的请求(200表示成功):
package com.zwj.demo01; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /** * 创建服务器,并启动 * @author Administrator * */ public class Server { private ServerSocket server; /** * @param args */ public static void main(String[] args) { Server server = new Server(); server.start(); } /** * 启动方法 */ public void start(){ try { server = new ServerSocket(8888); this.receive(); } catch (IOException e) { e.printStackTrace(); } } /** * 接收客户端 */ private void receive(){ try { Socket client =server.accept(); StringBuilder sb =new StringBuilder(); String msg =null; BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); while((msg=br.readLine()).length()>0){ sb.append(msg); sb.append("\r\n"); } //接收客户端的请求信息 String requestInfo =sb.toString().trim(); System.out.println(requestInfo); } catch (IOException e) { //e.printStackTrace(); } } /** * 听着服务器 */ public void stop(){ } }
package com.zwj.demo01; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /** * 创建服务器,并启动 * @author Administrator * */ public class Server2 { private ServerSocket server; /** * @param args */ public static void main(String[] args) { Server2 server = new Server2(); server.start(); } /** * 启动方法 */ public void start(){ try { server = new ServerSocket(8888); this.receive(); } catch (IOException e) { e.printStackTrace(); } } /** * 接收客户端 */ private void receive(){ try { Socket client =server.accept(); byte[] data=new byte[20480]; int len =client.getInputStream().read(data); //接收客户端的请求信息 String requestInfo=new String(data,0,len).trim(); System.out.println(requestInfo); } catch (IOException e) { //e.printStackTrace(); } } /** * 听着服务器 */ public void stop(){ } } /* POST /log HTTP/1.1 Host: localhost:8888 Connection: keep-alive Content-Length: 17 Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 Origin: null Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,* ;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 uname=123&pwd=123 */
<html> <head> <title>登录</title> </head> <body> <form method="post" action="http://localhost:8888/index.html"> 用户名:<input type="text" name="uname" id="uname"/> 密码:<input type="password" name="pwd" id="pwd"/> <input type="submit" value="登录"/> </form> </body> </html>