网络编程和http协议和Tomcat模拟(java)

网络编程和http协议和Tomcat模拟

一、网络的介绍

把分布在不同地理区域的计算机与专门的外部设备用通信线路连成一个规模大、功能强的网络系统,从而使纵多的计算机可以方便地相互传递信息、共享硬件、软件、数据信息等资源

二、IP地址

(一)概念

是互联网上的每一台计算机都有的一个唯一表示自己的标记,通过IP可以找到网络上的一台计算机设备

(二)分类

1)IPv4:32位,分4段,每段的范围0-255,用圆点隔开

2)IPv6:128位,分8段,每段的范围0000~FFFF的十六进制数值,用冒号分割

3)地址号段分类

地址分类使用方地址范围
A类地址1.0.0.1~126.255.255.254政府机构
B类地址128.0.0.1~191.255.255.254中等规模的公司
C类地址192.0.0.1~191.223.255.254任意需要的人
D类地址224.0.0.1~239.255.255.254组播
E类地址240.0.0.1~255.255.255.254实验

4)常用方法

InetAddress类用来表示IP地址

1.在给定主机名的情况下确定主机的 IP 地址 查找局部网内部存在的一个IP地址

//static InetAddress   getByName(String host)
InetAddress byName = InetAddress.getByName("SC-201807181034");

2.根据一个ip地址获得一个IP地址对象

//static InetAddress   getByAddress(byte[] addr)
InetAddress byAddress = InetAddress.getByAddress(new byte[]{(byte) 172,16,3,(byte) 206});

3.测试是否可以达到该地址 (类似ping)

//boolean isReachable(int timeout)
boolean b = byAddress2.isReachable(100);

4.返回本地主机的ip地址和主机名

//static InetAddress   getLocalHost()
InetAddress lh = InetAddress.getLocalHost();

三、端口

(一)概念

软件数据交互的一个出口,范围是从0 到65535(2^16-1)。一个电脑上的应用程序都绑定了一个对应的编号(端口),通过IP可以定位到指定的主机,加上端口就可以定位到指定的程序

(二)分类

1)公认端口:0~1023,紧密绑定一些服务

2)注册端口:1024~49151,松散绑定一些服务

3)动态端口:49152~65535,动态使用的端口,程序一般不会使用这些端口

注意:同一台机器上不能有两个程序使用同一个端口,会冲突

四、URL

Uniform Resource Locator:统一资源定位符

(一)网页

协议://主机ip:端口/资源路径磁盘下面的文件

http://itsource.cn:80/class_info/java_jichu.html

(二)常用方法

1.URL的连接

//URL(String protocol, String host, int port, String file)
URL url = new URL("http", "172.16.103.5", 8088, "");

2.打开URL的连接并返回一个该连接的输入流

// InputStream 	openStream()
InputStream openStream = url.openStream();

3.下载

public static void main(String[] args) throws IOException {
		URL url = new URL("http", "172.16.103.5", 8088, "");
		/*下载*/
		//打开URL的连接并返回一个该连接的输入流
		InputStream openStream = url.openStream();
		FileOutputStream fos = new FileOutputStream("F:/test/dl.html");
		int len = -1;
		byte[] b = new byte[1024];
		while ((len = openStream.read(b)) != -1) {
			fos.write(b,0,len);
		}
		System.out.println("下载完成!");
		fos.close();
		openStream.close();
	}

(三)编码URLEncode

		String str = "http://www.lty.com/祈心.gif";
		String encode = URLEncoder.encode(str, "UTF-8");
		System.out.println(encode);

(四)解码URLDecode

		String str2 = "%E7%A5%88%E5%BF%83";
		String decode = URLDecoder.decode(str2, "UTF-8");
		System.out.println(decode);//祈心

五、TCP协议

Transmission Control Protocol :传输控制协议

特点

  1. 基于连接

  2. 安全可靠,保证数据正确性和数据顺序

  3. 流模式

  4. 程序结构复杂,经过三次握手,速度慢

六、UDP协议

面向数据报的运输层协议

特点

  1. 无连接

  2. 不安全可能丢包,不保证数据顺序

  3. 数据报模式

  4. 程序结构较简单,速度快

七、HTTP协议

(一)Request请求对象

请求头信息(问)

GET 请求获取Request-URI所标识的资源

POST 在Request-URI所标识的资源后附加新的数据

发送给服务器的信息

​ 请求行、消息报头、请求正文

请求行:Method Request-URI HTTP-Version CRLF

get/post /xx/xxx/index.html Http1.1 空格换行[结束]

(二) Response相应对象

响应头的信息(答)

请求和响应:

消息头、请求行、响应头、请求头

八、Socket

客户端

public static void main(String[] args) throws IOException {
		//监听指定的端口
		ServerSocket ss = new ServerSocket(9699);
		//接收端口
		Socket accept = ss.accept();
		//获得客户端发送的数据
		InputStream is = accept.getInputStream();
		//转成字符流
		InputStreamReader isr = new InputStreamReader(is);
		//字符流转成字符缓冲流
		BufferedReader br = new BufferedReader(isr);
		String readLine = br.readLine();
		System.out.println(readLine);
		br.close();
		isr.close();
		is.close();
	}
}

服务端

public static void main(String[] args) throws UnknownHostException, IOException {
		// 连接指定的服务端IP地址和端口
		Socket s = new Socket("localhost", 9699);
		//获得输出流
		OutputStream os = s.getOutputStream();
		//打印各种数据值的流
		PrintStream ps = new PrintStream(os);
		ps.println("emmmmm.......");
		ps.close();
		os.close();
	}
}

九、模拟Tomcat

public static void main(String[] args) {
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		FileInputStream fis = null;
		OutputStream os = null;
		PrintWriter pw = null;
		try {
			//创建服务端
			ServerSocket ss = new ServerSocket(9699);
			//获得套接字
			Socket accept = ss.accept();
			//获得输入流
			is = accept.getInputStream();
			//将字节输入流包装
			isr = new InputStreamReader(is);
			//字符输入流转换成缓存流
			br = new BufferedReader(isr);
			String readLine = br.readLine();
			//根据空格拆分
			String str = readLine.split(" ")[1];
			//通过拼接找本地文件
			File file = new File("F:"+str);
			fis = new FileInputStream(file);
			//准备输出流,把输入流fis读取的数据写出去
			os = accept.getOutputStream();
			//写文件数据出去之前,先写响应头信息
			pw = new PrintWriter(new OutputStreamWriter(os));//通过套接字得到一个输出流
			pw.println("HTTP"
		               + "/1.1 200 OK");
			 pw.println("Content-Type: text/html;Charset=UTF-8");//消息头
	         pw.println("Content-Length: " + file.length());// 消息头 字节长度
	         pw.println();//写的响应头的信息出去
	         //读取本地文件,通过pw 写到浏览器客户端
	         byte[] b = new byte[1024];
	         int len = -1;
	         while((len = fis.read(b))!=-1){
	        	 String str2 = new String(b, 0,len);
	        	 pw.write(str2);
	         }
	     }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值