zoukankan      html  css  js  c++  java
  • Java网络编程

    网络编程

    1.1概述

    1.2 网络通信的要素

    如何实现网络的通信

    通信双方的地址:

    • ip

    • 端口号

     

    规则:网络通信的协议

    TCP/IP参考模型

    小结:

    1. 网络编程中有两个主要的问题

      • 如何准确的定位到网络的一台或多台主机

      • 找到主机后如何进行通信

    2. 网络编程中的要素

      • IP和端口号

      • 网络通信协议,TCP,UDP

    3. 万物皆对象

     

     

    1.3IP

    ip地址:InetAddress

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

    • 本机localhost

    • IP地址的分类

      • IP地址IPV4/IPV6

        • IPV4 3 2位 4 个字节 0~255 例如:127.0.0.1

        • IPV6 128位 16个字节

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

        • ABCD类地址

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

    • 域名,是为了更好的记忆

     //查询本机地址
             InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
             System.out.println("inetAddress1 = " + inetAddress1);
             System.out.println("---------------------");
             InetAddress inetAddress2 = InetAddress.getByName("localhost");
             System.out.println("inetAddress2 = " + inetAddress2);
             System.out.println("---------------------");
             InetAddress inetAddress3 = InetAddress.getLocalHost();
             System.out.println("inetAddress3 = " + inetAddress3);
             System.out.println("----------------------");
     
             //查询网站ip地址
             InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
             System.out.println("inetAddress4 = " + inetAddress4);
             System.out.println("----------------------");
     
     
             //常用方法
             System.out.println(inetAddress4.getAddress());
             System.out.println(inetAddress4.getCanonicalHostName());   //获得规范的名字
             System.out.println(inetAddress4.getHostAddress());        //获得ip
             System.out.println(inetAddress4.getHostName());    //域名,或者自己本机的名字

     

    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

        常用cmd语句

         netstat -ano #查看所有的端口
         netstat -ano|findstr "**" #查看指定(**)的端口
         tasklist|findstr "**"  #查看指定(**)的端口进程
     InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
             System.out.println("socketAddress = " + socketAddress);
             InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
             System.out.println("socketAddress2 = " + socketAddress2);
     
             System.out.println(socketAddress.getAddress());
             System.out.println(socketAddress.getHostName());
             System.out.println(socketAddress.getPort());

     

     

    1.5通信协议

    协议:约定

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

    问题:非常的复杂

    TCP/IP协议族

    重要:

    • TCP:用户传输协议

    • UDP:用户数据报协议

     

    TCP UDP的对比

    TCP:

    • 连接稳定

    • 三次握手,四次挥手

    • 客户端,服务端

    • 传输完成,释放连接,效率低

    UDP:

    • 无连接,不稳定

    • 客户端,服务端:没有明确的界限

    • 不管你有没有准备好,都可以发给你

    • DDOS:饱和攻击

     

     

    1.6 TCP

    客户端

    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");
                 //2. 端口号
                 int port=9999;
     
                 //3. 创建一个socket连接
                 socket=new Socket(serverIP,port);
                 //4.发送消息
                 os = socket.getOutputStream();
                 os.write("hello".getBytes());
     
            } 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();
                    }
                }
            }
        }

     

    服务器

    1. 建立服务的端口ServerSocket

    2. 等待用户的连接accept()

    3. 接受用户的消息

     public static void main(String[] args) {
             ServerSocket serverSocket=null;
             Socket accept=null;
             InputStream inputStream=null;
             ByteArrayOutputStream byteArrayOutputStream=null;
             try {
                 //1.我先得有一个地址
                 serverSocket = new ServerSocket(9999);
                 while(true){
                     //2. 等待客户端连接
                     accept = serverSocket.accept();
                     //3. 读取客户端的消息
                     inputStream = accept.getInputStream();
     
                     //管道流
                     byteArrayOutputStream = new ByteArrayOutputStream();
                     byte[] buffer = new byte[1024];
                     int len;
                     while((len=inputStream.read(buffer))!=-1)
                    {
                         byteArrayOutputStream.write(buffer,0,len);
     
                    }
                     System.out.println(byteArrayOutputStream.toString());
                }
            } catch (IOException e) {
                 e.printStackTrace();
            }finally {
                 //关闭资源
                 if (byteArrayOutputStream!=null){
                     try {
                         byteArrayOutputStream.close();
                    } catch (IOException e) {
                         e.printStackTrace();
                    }
                }
                 if(inputStream!=null){
                     try {
                         inputStream.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();
                    }
                }
     
            }
        }

    文件上传

    服务器端

      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.png"));
             byte[] bytes = new byte[1024];
             int len;
             while((len=is.read(bytes))!=-1){
                 fos.write(bytes,0,len);
            }
     
             //通知客户端我已接收完毕了
             OutputStream os=socket.getOutputStream();
             os.write("我接收完毕了,你可以断开了".getBytes());
     
             // 关闭资源
             fos.close();
             is.close();
             socket.close();
             serverSocket.close();
        }

    客户端

     public static void main(String[] args) throws Exception {
             //1. 创建一个Socket连接
             Socket socket=new Socket(InetAddress.getLocalHost(),9000);
             //2.创建一个输出流
             OutputStream os = socket.getOutputStream();
             //3. 读取文件
             FileInputStream fis = new FileInputStream(new File("po.png"));
             //4. 写出文件
             byte[] buffers = new byte[1024];
             int len;
             while((len=fis.read(buffers))!=-1){
                 os.write(buffers,0,len);
            }
     
             //通知服务器,我已经结束了
             socket.shutdownOutput();//我已经传输完了
     
             //确定服务器接收完毕,才能够断开连接
             InputStream inputStream = socket.getInputStream();
             //String byte[]
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             byte[] bytes=new byte[2014];
             int len2;
             while((len2=inputStream.read(bytes))!=-1)
                 bos.write(bytes,0,len2);
             System.out.println(bos.toString());
     
             //5. 关闭资源
             bos.close();
             fis.close();
             os.close();
             socket.close();
        }

     

    Tomcat

    服务器

    • 自定义

    • tomcat服务器S:Java后台开发

    客户端

    • 自定义C

    • 浏览器B

     

    UDP

     

  • 相关阅读:
    POJ 2427 Smith's Problem Pell方程
    Codeforces Round #194 (Div. 2) 部分题解
    SPOJ 3899. Finding Fractions 连分数
    Codeforces Round #193 (Div. 2) 部分题解
    HDU 1402 A * B Problem Plus FFT
    F的ACM暑期集训计划
    HDU 4607 Park Visit HDU暑期多校1
    Windows 下 Sublime Text 默认打开方式问题解决办法
    Roman To Integer
    Longest Common Prefix
  • 原文地址:https://www.cnblogs.com/Athena-life/p/13530402.html
Copyright © 2011-2022 走看看