网络编程
- 计算机网络: 
  
- 计算机网络是指地理位置的具有独立的多台计算机及其外部设备,通过通信路线连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
 
 - 目的: 
  
- 无线电台……传播交流信息,数据交换。通信
 
 - javaweb: 网页编程 B/S
 - 网页编程:TCP/IP C/S
 
通信双方地址:
- ip
 - 端口号
 
规则:网络通信的协议
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SX88N6hI-1645795060887)(E:\图片集合\Pc\1644484259231.png)]
IP
ip地址:InetAddress
- 唯一定位一台网络上计算机
 - 127.0.0.1: 本机localhost
 - ip地址分类 
  
- ipv4/ipv6 
    
- ipv4:127.0.0.1, 4字节组成。 0-255 42亿;30亿在北美,亚洲4亿。2011年用完;
 - ipv6: :128位 8个无符号整数
 
 - 公网(互联网) - 私网(局域网) 
    
- ABCD类地址
 - 192.168.xx.xx,专门给组织内部使用的
 
 
 - ipv4/ipv6 
    
 
import java.net.InetAddress;
public class demo5 {
    public static void main(String[] args) {
        try {
            //本机地址查询
            InetAddress byName = InetAddress.getByName("127.0.0.1");
            System.out.println(byName);
            InetAddress localhost = InetAddress.getByName("localhost");
            System.out.println(localhost);
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);
            //查询网站ip地址
            InetAddress byName1 = InetAddress.getByName("www.baidu.com");
//            System.out.println(byName1.getAddress());
            System.out.println(byName1.getCanonicalHostName());//规范的名字
            System.out.println(byName1.getHostAddress());//ip
            System.out.println(byName1.getHostName());//域名,或者自己电脑的名字
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
 
端口
端口表示计算机上的一个程序的进程
-  
不同的程序有不同的端口号!用来区分软件
 -  
被规定0-65535
 -  
TCP,UDP 65535* 2 tcp:80 udp: 80, 单个协议下,端口号不能冲突
 -  
端口分类
-  
公有端口0-1023
- http:80
 - https: 443
 - ftp: 21
 - Telent: 23
 
 -  
程序注册端口:1024~49151,分配用户或者程序
- Tomcat: 8080
 - MySql: 3306
 - Oracle: 1521
 
 -  
动态,私有: 49152~65535
netstat -ano #查看所有端口 netstat -ano | findstr "5900" tasklist|findstr "12500" ctrl+alt +esc 
 -  
 
通信协议
 TCP UDP对比
TCP:打电话
-  
连接 稳定
 -  
三次握手,四次挥手
最少需要三次,保证稳定连续 A:你瞅啥? B:瞅你咋地? A:干一场 A:我要走了 B:你真的要走了嘛? B:你真的真的要走了嘛? A:我真的要走了 -  
客户端,服务端
 -  
传输完成,释放完成,效率低
 
UPD:发短信
- 不连接 不稳定
 - 客户端,服务端:没有明确的界限
 - 不管有没有准备好,都可以发给你
 - 导弹
 - DDOS: 洪水攻击! (饱和攻击)
 
TCP
服务端
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServiceDemo2 {
    public static void main(String[] args) {
        ServerSocket serverSocket =null;
        Socket accept = null;
        InputStream is = null;
        ByteArrayOutputStream baos=null;
        try {
            //1. 我得有一个地址
            serverSocket = new ServerSocket(9999);
            //2. 等待客户端连接过来
            accept = serverSocket.accept();
            //3. 读取客户端的消息
            is = accept.getInputStream();
//            byte[] buffer = new byte[1024];
//            int len;
//            while ((len=is.read(buffer))!=-1){
//                String msg=new String(buffer,0,len);
//                System.out.println(msg);
//            }
            //管道流
            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 (Exception e){
            e.printStackTrace();
        } finally {
            //关闭资源
            if (baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
客户端
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
//客户端
public class TcpClientDemo1 {
    public static void main(String[] args) {
        Socket socket =null;
        OutputStream os=null;
        try {
            //1. 要知道服务器的地址,端口号
            InetAddress byName = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2. 创建一个socket连接
            socket = new Socket(byName, port);
            //发送消息IO流
            os = socket.getOutputStream();
            os.write("你好,欢迎学习java".getBytes(StandardCharsets.UTF_8));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
文件上传
服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class TcpServiceDemo90 {
    public static void main(String[] args) throws IOException {
        //1. 创建服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //2. 监听客户端连接
        Socket accept = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
        //3. 获取输入流
        InputStream is = accept.getInputStream();
        //4. 文化输出
        FileOutputStream fos = new FileOutputStream(new File("re.png"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len= is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        //通知客户端我接收完毕了
        OutputStream os = accept.getOutputStream();
        os.write("我接收完了,你可以断开了".getBytes(StandardCharsets.UTF_8));
        //关闭资源
        fos.close();
        is.close();
        accept.close();
        serverSocket.close();
    }
}
 
客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo2 {
    public static void main(String[] args) throws IOException {
        //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("img.png"));
        //4. 写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
        //通知服务器,我已经结束了
        socket.shutdownOutput();//我已经传输完了
        //确定服务器接收完毕,才能够断开连接
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[2014];
        int len2;
        while ((len2=inputStream.read(buffer2))!=-1){
            baos.write(buffer2,0,len2);
        }
        //5. 关闭资源
        fis.close();
        inputStream.close();
        baos.close();
        os.close();
        socket.close();
    }
}
 
URl
https://www.baidu.com/?tn=44004473_38_oem_dg
统一资源定位符:定位资源的,定位互联网上的某一个资源
import java.net.MalformedURLException;
import java.net.URL;
public class demo1 {
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("https://www.baidu.com/?tn=44004473_38_oem_dg");
        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.getFile());//全路径
        System.out.println(url.getQuery());//参数
    }
}
 
w URL(“https://www.baidu.com/?tn=44004473_38_oem_dg”);
 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.getFile());//全路径
 System.out.println(url.getQuery());//参数
 }
 }










