目录
计算机网络:
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
计算机网络主要是由一些通用的、可编程的硬件互连而成的,而这些硬件并非专门用来实现某一特定目的(例如,传送数据或视频信号)。这些可编程的硬件能够用来传送多种不同类型的数据,并能支持广泛的和日益增长的应用。
网络编程的目的:
传播交流信息,数据交换,通信
网络编程的需求:
JavaWeb:网页编程 B/S
网络编程:TCP/IP C/S
如何实现网络的通信
通信双方地址:
规则:网络通信的协议
TCP/IP参考模型:
小结:
ip地址:InetAddress
ipv4
127.0.0.1 4个字节组成 0~255 全球一共42亿个ip地址 30亿在北美 4亿在亚洲 2011年就已经用尽ipv6
128位,8个无符号整数演示
- package net;
-
- import java.net.InetAddress;
- import java.net.UnknownHostException;
-
- //测试IP
- public class TestInetAddress {
- public static void main(String[] args) {
- try {
- //查询本机地址方式一
- InetAddress inetAddress1 = InetAddress.getByName("172.0.0.1");
- System.out.println(inetAddress1);
-
- //查询本机地址方式二
- InetAddress inetAddress2 = InetAddress.getByName("localhost");
- System.out.println(inetAddress2);
-
- //查询本机地址方式三
- InetAddress inetAddress3 = InetAddress.getLocalHost();
- System.out.println(inetAddress3);
-
- //查询网站ip地址
- InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
- System.out.println(inetAddress4);
- } catch (UnknownHostException e) {
- throw new RuntimeException(e);
- }
- }
- }
端口表示计算机上的一个程序的进程
演示
- package net;
-
- import java.net.InetSocketAddress;
-
- public class TestInetSocketAddress {
- public static void main(String[] args) {
-
- InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
- System.out.println(inetSocketAddress);
-
- InetSocketAddress inetSocketAddress1 = new InetSocketAddress("localhost", 8080);
- System.out.println(inetSocketAddress1);
-
- System.out.println("===========================");
-
- System.out.println(inetSocketAddress.getAddress());
- System.out.println(inetSocketAddress.getHostName()); //地址
- System.out.println(inetSocketAddress.getPort()); //端口
- }
- }
协议:约定,就好比我们现在说的普通话。
网络通信协议:速率,传输码率,代码结构,传输控制
TCP:传输控制协议
UDP:用户数据报协议
IP:网络互联协议
TCP和UDP对比
TCP:打电话
三次握手,四次挥手
UDP:发短信
演示:TCP实现聊天
TcpClientDemo01.java
- package net;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
-
- //客户端
- public class TcpClientDemo01 {
- public static void main(String[] args) {
-
- Socket socket = null;
- OutputStream os = null;
-
- try {
- //1.要知道服务器的地址,端口号
- InetAddress serverIp = InetAddress.getByName("127.0.0.1");
- int port = 9999;
-
- //2.创建一个socket连接
- socket = new Socket(serverIp,port);
-
- //3.发送消息 IO流
- os = socket.getOutputStream();
-
- os.write("你好,欢迎来到Java世界".getBytes());
-
-
- } catch (Exception e) {
- throw new RuntimeException(e);
- }finally {
- if (os!=null){
- try {
- os.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- if (socket!=null){
- try {
- socket.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- }
- }
- }
TcpServerDemo01.java
- package net;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
-
- //服务端
- public class TcpServerDemo01 {
- public static void main(String[] args) {
-
- ServerSocket serverSocket = null;
- Socket socket = null;
- InputStream is = null;
- ByteArrayOutputStream baos = null;
-
- try {
- //1.我得有一个地址,端口号
- serverSocket = new ServerSocket(9999);
-
- //2.等待客户端连接过来
- socket = serverSocket.accept();
-
- //3.读取客户端的消息
- is = socket.getInputStream();
-
- //管道流
- baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len;
- while ((len=is.read(buffer))!=-1){
- baos.write(buffer,0,len);
- }
-
- System.out.println(baos.toString());
-
- } catch (IOException e) {
- throw new RuntimeException(e);
- }finally {
- //关闭资源
- if (baos!=null){
- try {
- baos.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- if (is!=null){
- try {
- is.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- if (socket!=null){
- try {
- socket.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- if (serverSocket!=null){
- try {
- serverSocket.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- }
- }
- }
演示:TCP文件上传实现
TcpClientDemo02.java
- package net;
-
- import java.io.*;
- import java.net.InetAddress;
- import java.net.Socket;
-
- public class TcpClientDemo02 {
- public static void main(String[] args) throws Exception{
- //1.创建一个socket连接
- Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
-
- //2.创建一个输出流
- OutputStream os = socket.getOutputStream();
-
- //3.读取文件
- FileInputStream fis = new FileInputStream(new File("吴彦祖.gif"));
-
- //4.写出文件
- byte[] buffer = new byte[1024];
- int len;
- while((len=fis.read(buffer))!=-1){
- os.write(buffer,0,len);
- }
-
- //5.通知服务器,已经结束传输了
- socket.shutdownOutput();
-
- //6.确定服务器接受完毕,才能够断开连接
- InputStream is = socket.getInputStream();
- //String byte[]
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- byte[] buffer2 = new byte[1024];
- int len2;
- while((len2=is.read(buffer2))!=-1){
- baos.write(buffer2,0,len2);
- }
-
- System.out.println(baos.toString());
-
- //7.关闭资源
- baos.close();
- is.close();
- fis.close();
- os.close();
- socket.close();
- }
- }
TcpServerDemo02.java
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
-
- public class TcpServerDemo02 {
- public static void main(String[] args) throws Exception{
- //1.创建服务
- ServerSocket serverSocket = new ServerSocket(9000);
-
- //2.监听客户端的连接
- Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
-
- //3.获取输入流
- InputStream is = socket.getInputStream();
-
- //4.文件输出
- FileOutputStream fos = new FileOutputStream(new File("receive.gif"));
- byte[] buffer = new byte[1024];
- int len;
- while((len=is.read(buffer))!=-1){
- fos.write(buffer,0,len);
- }
-
- //5.通知客户端我已经接受完毕了
- OutputStream os = socket.getOutputStream();
- os.write("已接受完毕,可以断开!".getBytes());
-
- //6.关闭资源
- fos.close();
- is.close();
- socket.close();
- serverSocket.close();
-
-
- }
- }
协议简介
UDP(UserDatagramProtocol)是一个简单的面向消息的传输层协议,尽管UDP提供标头和有效负载的完整性验证(通过校验和),但它不保证向上层协议提供消息传递,并且UDP层在发送后不会保留UDP 消息的状态。因此,UDP有时被称为不可靠的数据报协议。如果需要传输可靠性,则必须在用户应用程序中实现。
综上所述,UDP使用场景为流媒体应用、语音交流、视频会议所使用的传输层协议,还有许多基于互联网的电话服务使用的VOIP(基于IP的语音)也是基于UDP运行的,实时视频和音频流协议旨在处理偶尔丢失的数据包,因此,如果重新传输丢失的数据包,则只会发生质量略有下降,而不是出现较大的延迟。
DatagramSocket 类
DatagramSocket 类用于表示发送和接收数据报包的套接字。数据报包套接字是包投递服务的发送或接收点。每个在数据报包套接字上发送或接收的包都是单独编址和路由的。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达。
DatagramPacket 类
java.net 包中的 DatagramPacket 类用来表示数据报包,数据报包用来实现无连接包投递服务。每条报文仅根据该包中包含的信息从一台机器路由到另一台机器。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达。
演示:UDP消息发送
UdpClientDemo01.java
- package net;
-
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
-
- //不需要连接服务器
- public class UdpClientDemo01 {
- public static void main(String[] args) throws Exception{
- //1.建立一个Socket
- DatagramSocket socket = new DatagramSocket();
-
- //2.新建一个包
- String msg = "你好啊,服务器";
-
- //发送给谁
- InetAddress localhost = InetAddress.getByName("localhost");
- int port =9090;
-
- //数据,数据的长度起始,要发送谁
- DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
-
- //3.发送包
- socket.send(packet);
-
- //4.关闭流
- socket.close();
- }
- }
UdpServerDemo01.java
- package net;
-
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
-
- //还是要等待客户端的连接
- public class UdpServerDemo01 {
- public static void main(String[] args) throws Exception{
- //开放端口
- DatagramSocket socket = new DatagramSocket(9090);
-
- //接受数据包
- byte[] buffer = new byte[1024];
- DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);//接收
-
- socket.receive(packet);//阻塞接收
-
- System.out.println(packet.getAddress().getHostAddress());
- System.out.println(new String(packet.getData(),0,packet.getLength()));
-
- //关闭连接
- socket.close();
-
- }
- }
演示:UDP聊天实现
UdpSenderDemo01.java
- package net;
-
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetSocketAddress;
-
- public class UdpSenderDemo01 {
- public static void main(String[] args) throws Exception{
- DatagramSocket socket = new DatagramSocket(8888);
-
- //准备数据: 控制台读取System.in
- //表示缓冲区的。之前的StringBuffer,缓冲区中的内容可以更改,可以提高效率。
- //如果想接收任意长度的数据,而且避免乱码的产生,就可以使用BufferedReader。[下图BufferedReader注解]
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
-
- while (true){
- String data = reader.readLine();
- byte[] datas = data.getBytes();
- DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
-
- socket.send(packet);
- if(data.equals("bye")){
- break;
- }
- }
-
- socket.close();
- }
- }
UdpReceiveDemo01.java
- package net;
-
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
-
- public class UdpReceiveDemo01 {
- public static void main(String[] args) throws Exception{
- DatagramSocket socket = new DatagramSocket(6666);
-
-
- while (true){
- //准备接受包裹
- byte[] container = new byte[1024];
- DatagramPacket packet = new DatagramPacket(container,0,container.length);
- socket.receive(packet);//阻塞式接受包裹
-
- //断开连接
- byte[] data = packet.getData();
- String receiveData = new String(data, 0, data.length);
-
- System.out.println(receiveData.trim());
-
- if(receiveData.equals("bye")){
- break;
- }
- }
- }
- }
UDP多线程聊天
TalkSend.java
- package net;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetSocketAddress;
- import java.net.SocketException;
-
- public class TalkSend implements Runnable{
-
- DatagramSocket socket = null;
- BufferedReader reader = null;
-
- private int fromPort;
- private String toIp;
- private int toPort;
-
- public TalkSend(int fromPort, String toIp, int toPort) {
- this.fromPort = fromPort;
- this.toIp = toIp;
- this.toPort = toPort;
-
- try {
- socket = new DatagramSocket(fromPort);
- reader = new BufferedReader(new InputStreamReader(System.in));
- } catch (SocketException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public void run() {
- while (true)
- try {
- String data = reader.readLine();
- byte[] datas = data.getBytes();
- DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIp,this.toPort));
-
- socket.send(packet);
- if(data.equals("bye")){
- break;
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- socket.close();
- }
- }
TalkReceive.java
- package net;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.SocketException;
-
- public class TalkReceive implements Runnable{
-
- DatagramSocket socket = null;
- private int port;
- private String msgFrom;
-
- public TalkReceive(int port,String msgFrom) {
- this.port = port;
- this.msgFrom = msgFrom;
-
- try {
- socket = new DatagramSocket(port);
- } catch (SocketException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public void run() {
- while (true){
- try{
- //准备接受包裹
- byte[] container = new byte[1024];
- DatagramPacket packet = new DatagramPacket(container,0,container.length);
- socket.receive(packet);//阻塞式接受包裹
-
- //断开连接 bye
- byte[] data = packet.getData();
- String receiveData = new String(data, 0, data.length);
-
- System.out.println(msgFrom+":"+receiveData.trim());
-
- if(receiveData.equals("bye")){
- break;
- }
- }catch (IOException e){
- e.printStackTrace();
- }
- }
- socket.close();
-
- }
- }
TalkStudent.java
- package net;
-
- public class TalkStudent {
- public static void main(String[] args) {
- //开启两个线程
- //学生发送消息的目的地址是IP,目的端口9999,学生接受消息的端口8888,接受的是老师这个人
- new Thread(new TalkSend(7777,"localhost",9999)).start();
- new Thread(new TalkReceive(8888,"老师")).start();
- }
- }
TalkTeacher.java
- package net;
-
- public class TalkTeacher {
- public static void main(String[] args) {
- //老师发送消息的目的地址是IP,目的端口8888,老师接受消息的端口9999,接受的是学生这个人
- new Thread(new TalkSend(5555,"localhost",8888)).start();
- new Thread(new TalkReceive(9999,"学生")).start();
- }
- }
URL:统一资源定位符
URL是对互联网上得到的资源的位置和访问方法的一种简洁表示,是互联网上标准资源的地址。URL它具有全球唯一性,正确的URL应该是可以通过浏览器打开此网页的,但如果您访问外网,会提示网页无法打开,这并不能说明这个URL是错误的。只不过在国内不能访问而已。
URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。
演示:URL的get用法
- package net;
-
- import java.net.URL;
-
- public class UrlDemo1 {
- public static void main(String[] args) throws Exception{
- URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=chenxin&password=123456");
- System.out.println(url.getProtocol()); //协议
- System.out.println(url.getHost()); //主机ip
- System.out.println(url.getPort()); //端口
- System.out.println(url.getPath()); //路径
- System.out.println(url.getQuery()); //参数
- }
- }
URL的down用法
- package net;
-
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLConnection;
-
- public class UrlDown {
- public static void main(String[] args) throws Exception{
- //1.下载地址
- URL url = new URL("https://gimg2.baidu.com/image_search/src=http%316%2F06%2F13%2F146578167579371187.PNG");
-
- //2.连接到这个资源 HTTP
- HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
-
- InputStream inputStream = urlConnection.getInputStream();
-
- FileOutputStream fos = new FileOutputStream("吴彦祖.gif");
-
- byte[] buffer = new byte[1024];
- int len;
- while ((len=inputStream.read(buffer))!=-1){
- fos.write(buffer,0,len); //写出这个程序
- }
- fos.close();
- inputStream.close();
- urlConnection.disconnect();//断开连接
- }
- }