Java网络编程

一、概述

1、计算机网络:

计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统、网络管理软件及网络通信协议的管理和协调下,实现资源共享信息传递的计算机系统。

2、网络编程的目的:

实现资源共享;信息传递。

3、如何实现:

1)、如何准确地定位网络上的一台主机

​ 主机IP:端口,定位到这个计算机上的某个资源

2)、找到了这个主机,如何传输数据?

javaweb:网页编程 B/S

网络编程:TCP/IP C/S

二、网络通信的要素

1、通信双方地址:

  • IP
  • 端口号

2、规则:网络通信的协议

TCP/IP四层模型、OSI七层网络模型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jreA3snA-1616610013598)(https://i.loli.net/2021/03/17/buOSL5g9ctKTZXo.png)]

三、IP

1、IP地址:InetAddress

  • 唯一定位一台网络上的计算机

  • 127.0.0.1:本机 localhost

  • IP地址的分类:

    • IPv4 / IPv6

      • IPv4 :127.0.0.1 IPv4使用32位(4字节)地址,4个字节组成,0~255

      • IPv6:IPv6的地址长度为128位,是IPv4地址长度的4倍,8个无符号整数。IPv6有3种表示方法:

        一、冒分十六进制表示法
          格式为X:X:X:X:X:X:X:X,其中每个X表示地址中的16b,以十六进制表示,例如:
          ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
          这种表示法中,每个X的前导0是可以省略的,例如:
          2001:0DB8:0000:0023:0008:0800:200C:417A → 2001:DB8:0:23:8:800:200C:417A

        二、0位压缩表示法
          在某些情况下,一个IPv6地址中间可能包含很长的一段0,可以把连续的一段0压缩为“::”。但为保证地址解析的唯一性,地址中”::”只能出现一次,例如:
          FF01:0:0:0:0:0:0:1101 → FF01::1101
          0:0:0:0:0:0:0:1 → ::1
          0:0:0:0:0:0:0:0 → ::

        三、内嵌IPv4地址表示法
          为了实现IPv4-IPv6互通,IPv4地址会嵌入IPv6地址中,此时地址常表示为:X:X:X:X:X:X:d.d.d.d,前96b采用冒分十六进制表示,而最后32b地址则使用IPv4的点分十进制表示,例如::192.168.0.1与::FFFF:192.168.0.1就是两个典型的例子,注意在前96b中,压缩0位的方法依旧适用

    • 公网(互联网) - 私网(局域网)

      • ABCD类地址

        • 一个A类IP地址是指, 在IP地址的四段号码中,第一段号码为网络号码,剩下的三段号码为本地计算机的号码。如果用二进制表示IP地址的话,A类IP地址就由1字节的网络地址和3字节主机地址组成,网络地址的最高位必须是“0”。A类IP地址中网络的标识长度为8位,主机标识的长度为24位,A类网络地址数量较少,有126个网络,每个网络可以容纳主机数达1600多万台。

          A类IP地址 地址范围1.0.0.1到127.255.255.254 [2] (二进制表示为:00000001 00000000 00000000 00000001 - 01111111 11111111 11111111 11111110)。最后一个是广播地址。

        • 一个B类IP地址是指,在IP地址的四段号码中,前两段号码为网络号码。如果用二进制表示IP地址的话,B类IP地址就由2字节的网络地址和2字节主机地址组成,网络地址的最高位必须是“10”。B类IP地址中网络的标识长度为16位,主机标识的长度为16位,B类网络地址适用于中等规模的网络,有16384个网络,每个网络所能容纳的计算机数为6万多台。

          B类IP地址地址范围128.0.0.1-191.255.255.254 [1] (二进制表示为:10000000 00000000 00000000 00000001----10111111 11111111 11111111 11111110)。 最后一个是广播地址。

          B类IP地址的子网掩码为255.255.0.0,每个网络支持的最大主机数为256的2次方-2=65534台。

        • 一个C类IP地址是指,在IP地址的四段号码中,前三段号码为网络号码,剩下的一段号码为本地计算机的号码。如果用二进制表示IP地址的话,C类IP地址就由3字节的网络地址和1字节主机地址组成,网络地址的最高位必须是“110”。C类IP地址中网络的标识长度为24位,主机标识的长度为8位,C类网络地址数量较多,有209万余个网络。适用于小规模的局域网络,每个网络最多只能包含254台计算机。

          C类IP地址范围192.0.0.1-223.255.255.254 [1] (二进制表示为: 11000000 00000000 00000000 00000001 - 11011111 11111111 11111111 11111110)。

          C类IP地址的子网掩码为255.255.255.0,每个网络支持的最大主机数为256-2=254台

        • D类IP地址在历史上被叫做多播地址(multicast address),即组播地址。在以太网中,多播地址命名了一组应该在这个网络中应用接收到一个分组的站点。多播地址的最高位必须是“1110”,范围从224.0.0.0到239.255.255.255。

      • 192.168.xx.xx 专门给组织内部使用

  • 域名 用于记忆IP

public class TestInetAddress {
    public static void main(String[] args) {
        try{
            //查询本机地址
            InetAddress inetAddress1 = InetAddress.getByName("127.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);
            
            //常用方法
            // System.out.println(inetAddress4.getAddress());
            System.out.println(inetAddress4.getCanonicalHostName()); // 规范的名字
            System.out.println(inetAddress4.getHostAddress()); // IP
            System.out.println(inetAddress4.getHostName()); // 域名,或者本机名字
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

四、端口

端口表示计算机上的一个程序的进程;

  • 不同的进程的端口号不同,用于区分软件
  • 端口号规定为0~65535
  • TCP,UDP:65536 * 2;TCP:80,UDP:80。单个协议下,端口号不能冲突
  • 端口分类:
    • 公有端口 0~1023
      • HTTP :80
      • HTTPS :443
      • FTP :21
      • Telent :23
    • 程序注册端口 1024~49151,分配给用户或者程序
      • Tomcat :8080
      • MySQL :3306
      • Oracle :1521
    • 动态、私有:49152~65536
netstat -ano						# 查看所有端口
netstat -ano|findstr "35726"		# 查看指定端口
tasklist|findstr "35726"			# 查看指定端口的进程
public class TestInetSocketAddress {
    public static void main(String[] args) {
        //查询本机地址
        InetAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1",8080);
        System.out.println(inetSocketAddress1);
        InetAddress inetSocketAddress2 = new InetSocketAddress("localhost",8080);
        System.out.println(inetSocketAddress2);

        //常用方法
        System.out.println(inetSocketAddress1.getAddress());
        System.out.println(inetSocketAddress1.getHostName()); // 地址
        System.out.println(inetSocketAddress1.getPort()); // 端口
    }
}

五、通信协议

网络通信协议:速率、传输码率、代码结构、传输控制…

TCP/IP协议簇:实际上是一组协议

  • IP:网络互联协议

  • TCP:用户传输协议

  • UDP:用户数据报协议

TCP 和 UDP 对比:

TCP:打电话

  • 连接:稳定

  • 三次握手 四次挥手

    最少需要三次,保证稳定连接
    A:你瞅啥?
    B:瞅你咋地?
    A:你过来啊!
    
    A:我要断开了
    B:你要断开了吗?我看看有没有东西要给你(半关闭状态)
    B:没有东西要给你
    A:好的,再见
    
  • 明确的客户端和服务端相连

  • 传输完成,释放连接

UDP:发短信

  • 不连接:不稳定
  • 客户端、服务端:没有明确的界限
  • 不管有没有准备好,都可以发给你
  • DDOS:洪水攻击(饱和攻击)

六、TCP

1、实现聊天

客户端

  1. 连接服务器Socket
  2. 发送消息
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("你好!".getBytes());
    } catch (IOException 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();
            }
        }
    }
}

服务端

  1. 建立服务的端口 ServerSocket
  2. 等待用户的连接 accept
  3. 接受用户的消息
public static void main(String[] args) {
    ServerSocket serverSocket = null;
    Socket socket = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
        // 1、首先服务端得有一个地址
        serverSocket = new ServerSocket(9999);
        while (true) {
            // 2、等待客户端连接过来
            socket = serverSocket.accept();
            // 3、读取客户端的消息
            is = socket.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 (IOException 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 (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2、实现文件上传

客户端

public static void main(String[] args) throws Exception {
    // 1、创建一个 Socket 连接
    Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9345);
    // 2、创建一个输出流
    OutputStream os = socket.getOutputStream();
    // 3、读取文件
    FileInputStream fis = new FileInputStream(new File("20201007012011.jpg"));
    // 4、写出文件
    byte[] buffer = new byte[1024];
    int len;
    while ((len = fis.read(buffer)) != -1) {
        os.write(buffer, 0, len);
    }

    // 通知服务器,我已经传输结束了
    socket.shutdownOutput(); // 我已经传输完了,shutdownInput:我已经接收完了

    // 确定服务器接收完毕,才能够断开连接
    InputStream is = socket.getInputStream();
    // 管道流,接收服务端传过来的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());

    // 5、关闭资源
    baos.close();
    is.close();
    fis.close();
    os.close();
    socket.close();
}

服务端

public static void main(String[] args) throws Exception {
    // 1、创建服务
    ServerSocket serverSocket = new ServerSocket(9345);
    // 2、监听客户端的连接
    Socket socket = serverSocket.accept(); // 阻塞式监听,会一直等待客户端连接
    // 3、获取输入流
    InputStream is = socket.getInputStream();
    // 4、文件输出
    FileOutputStream fos = new FileOutputStream(new File("A.jpg"));
    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) != -1) {
        fos.write(buffer,0,len);
    }

    // 通知客户端接收完毕了
    OutputStream os = socket.getOutputStream();
    os.write("已收到!可断开".getBytes());

    // 5、关闭资源
    os.close();
    fos.close();
    is.close();
    socket.close();
    serverSocket.close();
}

七、UDP

发短信:不用连接,需要知道对方的地址

1、发消息

发送端

/**
 * UDP
 * 实现消息发送——发送端,不需要连接服务端,直接发送
 *
 * @param args
 */
public static void main(String[] args) throws Exception {
    // 1、建立一个socket
    DatagramSocket socket = new DatagramSocket();

    // 2、建个包
    String msg = "Hello";

    // 发送的目标
    InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
    int port = 9090;
    // DatagramPacket(用于保存数据的缓冲区,缓冲区偏移量,要读取的字节数,发送目标地址,发送目标端口号)
    DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, inetAddress, port);

    // 3、发送包
    socket.send(packet);

    // 4、关闭流
    socket.close();
}

接收端

/**
 * UDP
 * 实现消息发送——接收端,服务端虽然不用与客户端建立连接,但它本身是开放端口的,这样 UDP 会直接发送过来
 * 并且还是要等 UDP 发过来数据包的,只是彼此之间不需要建立连接
 * 相当于导弹攻击,国家之间彼此并没有建立互相攻击的连接,但是可以知道一个国家的位置后,直接攻打搞偷袭
 *
 * @param args
 */
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();

}

2、循环发消息并接收

发送端

/**
 * UDP
 * 实现聊天——发送端
 * @param args
 */
public static void main(String[] args) throws Exception {
    DatagramSocket socket = new DatagramSocket(9035);

    // 准备数据,控制台读取 System.in
    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("127.0.0.1",35726));

        socket.send(packet);

        if (("bye").equalsIgnoreCase(data)) {
            break;
        }

    }

    socket.close();
}

接收端

/**
 * UDP
 * 实现聊天——接收端
 *
 * @param args
 */
public static void main(String[] args) throws Exception {
    DatagramSocket socket = new DatagramSocket(35726);

    // 准备接收包裹
    byte[] buffer = new byte[1024];
    DatagramPacket packet = null;

    while (true) {

        packet = new DatagramPacket(buffer, 0, buffer.length);

        socket.receive(packet); // 阻塞时接收包裹

        // 接收数据
        byte[] data = packet.getData();
        String receiveData = new String(data, 0, packet.getLength());
        System.out.println(receiveData);
        if (("bye").equalsIgnoreCase(receiveData)) {
            break;
        }

    }
    socket.close();

}

3、多线程双方在线聊天

发送端线程

/**
 * UDP
 * 多线程在线聊天——发送端线程
 * @author Cyril.P
 * @description
 * @date 2020-10-30-10:14
 */
public class sendThread implements Runnable {

    /**
     * 建立 socket
     */
    DatagramSocket socket = null;
    /**
     * 准备数据,控制台读取 System.in
     */
    BufferedReader reader = null;

    private int fromPort;
    private String toIP;
    private int toPort;

    public Hw2_6(int fromPort, String toIP, int toPort) {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;

        try {
            // 建立 socket
            socket = new DatagramSocket(fromPort);
            // 准备数据,控制台读取 System.in
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @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 (("bye").equalsIgnoreCase(data)) {
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}

接收端线程

/**
 * UDP
 * 多线程在线聊天——接收端线程
 * @author Cyril.P
 * @description
 * @date 2020-10-30-10:14
 */
public class receiveThread implements Runnable {

    DatagramSocket socket = null;

    private int port;
    private String msgFrom;

    public Hw2_7(int port, String msgFrom) {
        this.port = port;
        this.msgFrom = msgFrom;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        // 准备接收包裹
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

        while (true) {

            try {
                socket.receive(packet); // 阻塞时接收包裹

                // 接收数据
                byte[] data = packet.getData();
                String receiveData = new String(data, 0, packet.getLength());
                if (("bye").equalsIgnoreCase(receiveData)) {
                    System.out.println("对方已断开连接!");
                    break;
                }

                System.out.println(this.msgFrom + ":" + receiveData);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        socket.close();
    }
}

角色一

/**
 * UDP
 * 多线程在线聊天——角色一
 * @author Cyril.P
 * @description
 * @date 2020-10-30-10:14
 */
public class Hw2_8 {
    public static void main(String[] args) {
        // 开启两个线程
        new Thread(new Hw2_6(123,"127.0.0.1",222)).start();
        new Thread(new Hw2_7(333,"你好,这里是123,我要通过333发给321,你可以通过222发给我,收到请回复")).start();
    }
}

角色二

/**
 * UDP
 * 多线程在线聊天——角色二
 * @author Cyril.P
 * @description
 * @date 2020-10-30-10:14
 */
public class Hw2_9 {
    public static void main(String[] args) {
        new Thread(new Hw2_6(321,"127.0.0.1",333)).start();
        new Thread(new Hw2_7(222,"收到,我是321,我暴露了333,收到了你123的消息,我这就通过222发给你")).start();
    }
}

八、URL

https://www.baidu.com

统一资源定位符:定位互联网上的某一个资源

DNS 域名解析 将域名解析为 IP 地址

协议://ip地址:端口号/项目名/资源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值