• 网络编程01


    网络编程

    1.1、概述

    计算机网络:

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

    1.2、网络通信的要素

    通信双方的地址

    • IP
    • 端口号

    规则:网络通信协议
    七层模型
    图片出处:七层模型,侵权必删
    小结:

    1. 网络编程中的两个主要问题?
      • 如何准确的在网络上定义一台或多台主机
      • 找到主机周如何通信
    2. 网络编程中的要素?
      • IP和端口号 ip
      • 网络通信协议 udp tcp

    1.3、IP

    ip地址:InteAddress

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

    • 127.0.0.1 :本机localhost

    • ip地址的分类

      • ipv4/ipv6

        • IPV4:127.0.0.1 ,4个字节组成 0~255 ,42亿
        • IPV6:128位
        1234:bc11:1111:1111:1234:bc11:1111:1111
        
      • 公网(互联网)-私网(局域网)

        • ABCD类地址
        • 192.168.xx.xx,专门给组织内部使用
    • 域名:记忆IP问题!

      public class TestInetAdress {
          public static void main(String[] args) {
              try {
                  InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
                  System.out.println(inetAddress1);
                  //查询网络ip地址
                  InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
                  System.out.println(inetAddress2);
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              }
          }
      }
      

    1.4、端口

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

    • 不同的进程有不同的端口号,用来区分软件

    • 被规定:0~65535

    • TCP,UDP:65535*2

    • 端口分类:

      • 公有端口:0~1023

        • HTTP: 80
        • HTTPS : 443
        • FTP : 21
        • Telent : 23
      • 程序注册端口:1024~49151,分配用户或者程序

        • Tomcat : 8080
        • Mysql : 3306
        • Oracle : 1521
      • 动态、私有:49152~65535

        netstart -ano  #查看所有端口
        netstart -ano|findstr "5900"  #查看指定端口
        tasklist|findstr "8696"	#查看指定端口的进程
        Ctrl + Shift + esc	#任务管理器
        
      public class TestInetSocketAdress {
          public static void main(String[] args) {
              InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
              System.out.println(socketAddress);
              InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
              System.out.println(socketAddress2.getAddress());
              System.out.println(socketAddress2.getHostName());
              System.out.println(socketAddress2.getPort());
          }
      }
      

    1.5、通信协议

    TCP/IP协议簇:

    ​ TCP:用户传输协议

    ​ UDP:用户数据包协议

    TCP和UDP对比

    TCP:打电话

    • 连接 稳定
    • 三次握手 四次挥手
    • 客户端 服务端
    • 传输完成释放连接 效率低

    UDP:发短信

    • 不连接 不稳定
    • 没有明确的客户端和服务端
    • DDOS攻击:洪水攻击

    1.6、TCP

    客户端:

    1. 连接服务器Socketjian
    2. 发送消息
    
    public class TestClientDemo01 {
        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;
                socket = new Socket(serverIP,port);
                os = socket.getOutputStream();
                os.write("好好学习,天天向上".getBytes());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } 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. 建立服务的端口
    2. 等待用户的连接 accept
    3. 接收客户的信息
    
    public class TestServerDemo01 {
        public static void main(String[] args) {
            ServerSocket serverSocket = null;
            Socket socket = null;
            InputStream is = null;
            ByteArrayOutputStream baos = null;
            try {
                //1.有一个IP地址
                serverSocket = new ServerSocket(9999);
                //2.读取客户端连接
                socket = serverSocket.accept();
                //3.读取客户端消息
                is = socket.getInputStream();
                //4.管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) != -1){
                    baos.write(buffer,0,length);
                }
                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();
                    }
                }
            }
        }
    }
    
    刚刚参加工作,很有很多不懂不会的,发现错误,欢迎指正,谢谢!
  • 相关阅读:
    UiAutomator自动化测试框架介绍
    mongkeyrunner实现循环随机输入值的方法
    python出输出字符串方式:
    Python之字符串小代码解析
    安装JDK,Python SDK及环境变量的配置
    Monkeyrunner小脚本关于camera的使用
    ubuntu 下安装32位库 ia32-libs方法
    关于monkeyrunner的一些初步理解性的题目
    基于redis的限流
    表单防重复提交
  • 原文地址:https://www.cnblogs.com/xd-study/p/12988961.html
Copyright © 2020-2023  润新知