IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> B站狂神网络编程多多三连 -> 正文阅读

[网络协议]B站狂神网络编程多多三连

1.计算机网络

如何实现网络通信?

通信双方地址

  • ip

  • 端口号

规则:协议通信的协议

http , ftp ,smtp , tcp , udp

TCP/IP 参考模型

2.IP

专门表示Internet协议(IP)地址 : InetAdress.

ip地址 : InetAddress

  • ip地址的分类

    • ipv4/ipv6

      ipv6 : 2001 : 0bb2: abcd :ab34 : 0015 : 1a31 : 0000 2的128次方

  • 公网(互联网) - 私网

    • ABCD类地址

//测试Ip
public class InetAddressDemo {

    public static void main(String[] args) {
        try {
			//没什么用
            //查询本机地址
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);


            //查询网络ip地址
            InetAddress baiduIP = InetAddress.getByName("www.baidu.com");
            System.out.println(baiduIP);

            //常用方法
            System.out.println("规范的名字:"+baiduIP.getCanonicalHostName());  //规范的名字
            System.out.println("ip:"+baiduIP.getHostAddress());        //ip
            System.out.println("域名:"+baiduIP.getHostName());           //域名


        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

3.端口

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

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

  • 范围规定在0~65535

  • TCP,UDP : 65535 * 2 TCP与UDP协议端口不冲突

  • 端口分类

    • 公有端口 0~1023

      • HTTP :80

      • HTTPS :443

      • FTP : 21

      • SSH : 22

      • Telnet :23

    • 程序注册端口 : 1024~49151,分配用户或者程序

      • Tomcat : 8080

      • MySQL :3306

      • Oracle :1521

    • 动态,私有 :49152~65535

public class InetSocketAddressDemo {

    public static void main(String[] args) {
        //没什么用
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);  
        System.out.println(inetSocketAddress);

        System.out.println(inetSocketAddress.getAddress());
        System.out.println(inetSocketAddress.getHostName());  //host 文件
        System.out.println(inetSocketAddress.getPort());      //端口

    }
}

3.1 Dos命令

netstat -ano  #查看所有的端口
netstat -ano|findstr "5900"
tasklist|findstr "13640" #查看指定端口的进程

4.通信协议

协议 :约定,就好比我们现在说的是普通话.

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

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

重要:

  • TCP/IP : 用户传输协议

  • UDP : 用户数据报协议

出名的协议:

  • TCP:

  • IP:网络互联协议

TCP UDP 对比

TCP :打电话

  • 连接,稳定

  • 三次握手,四次挥手

最少需要三次,保证稳定连接
A:你愁啥?
B:瞅你咋地?
A:干一场

A:我要走了!
B:你真的要走了吗?
B:你真的真的要走了吗?
A:我真的要走了!
  • 客户端,服务端

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

UDP:发短信

  • 不连接,不稳定

  • 客户端,服务端,没有明确的接线

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

  • DDOS:洪水攻击!(饱和攻击)

5.TCP

客户端

1.连接服务器Socket

2.发送消息

服务器

1.建立服务的端口ServerSocket

2.等待用户的连接 accept

3.接受客户端的消息

Client:

//客户端
public class TcpClient1 {

    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;

        try {
            //1.要知道服务器的地址,端口号
            InetAddress serverIP =
                    InetAddress.getByName("127.0.0.1");
        Integer port = 9999;
        //2.创建一个socket连接
             socket = new Socket(serverIP,port);
        //3.发送消息 IO流
            os = socket.getOutputStream();
            os.write("你好,欢迎学习狂神说Java".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();
                }
            }
        }
    }
}

Server

public class TcpServer1 {

    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();
               //管道流
               baos = new ByteArrayOutputStream();
               byte[] buffer = new byte[1024];
               Integer 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();
                }
            }

        }
    }
}

5.1文件上传

Server

//服务端
public class TcpServerDemo02 {

    public static void main(String[] args) {
     //1.创建服务
        ServerSocket serverSocket = null;

        Socket socket = null;

        InputStream is = null;

        FileOutputStream fos = null;

        try {
            //1.创建服务
            serverSocket = new ServerSocket(9000);
            //2.监听客户端的连接
             socket = serverSocket.accept();

            //3.获取输入流
             is = socket.getInputStream();

            //4.文件输出
            fos = new FileOutputStream("receive.png");
            byte[] buffer = new byte[1024];
            Integer len;
            while((len=is.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }

            //通知客户端接收完毕
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("123".getBytes());


        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(fos != null){
                try {
                    fos.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();
                }
            }

        }

    }
}

Client

//客户端
public class TcpClientDemo02 {

    public static void main(String[] args) {
        //创建一个Socket连接
        InetAddress inetAddress = null;

        Socket socket = new Socket();

        FileInputStream fis = null;

        OutputStream outputStream = null;

        ByteArrayOutputStream baos = null;

        InputStream inputStream = null;

        try {
            //1.创建一个Socket连接
            inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress,9000);

            //2.创建一个输出流
             outputStream = socket.getOutputStream();

            //3.读取文件流
             fis = new FileInputStream(new File("D:\\bilibiliLesson\\NetProgram\\netProject\\src\\报名ER.png"));

            //4.写出文件
            byte[] buffer = new byte[1024];
            Integer length;
            while((length=fis.read(buffer))!=-1){
                outputStream.write(buffer,0,length);
            }


            //通知服务器已经传输完了
            socket.shutdownOutput();  //我已经传输完了


            //5.确定服务器接收完毕,才能断开连接
             inputStream = socket.getInputStream();

             baos = new ByteArrayOutputStream();


            byte[] buffer2 = new byte[1024];
            Integer length2;
            while((length2 = inputStream.read(buffer2))!=-1){
                baos.write(buffer,0,length2);
            }

            System.out.println(baos.toString());


        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream != null){

                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

6.UDP

6.1阻塞接收

UDP1

//不需要连接服务器
public class UdpDemo1 {

    public static void main(String[] args) {

        DatagramSocket socket = null;

        try {
            //1.建立一个Socket
             socket = new DatagramSocket(8080);

             //2.建一个包
            String message = "hello server";

            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");

            Integer port = 9090;

            //数据,数据的长度起始,要发送给谁
            DatagramPacket packet = new DatagramPacket(message.getBytes(), 0, message.getBytes().length, inetAddress, port);

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


        }
        catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(socket != null){
                socket.close();
            }
        }

    }
}

UDP2

//不需要连接服务器
public class UdpDemo1 {

    public static void main(String[] args) {

        DatagramSocket socket = null;

        try {
            //1.建立一个Socket
             socket = new DatagramSocket(8080);

             //2.建一个包
            String message = "hello server";

            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");

            Integer port = 9090;

            //数据,数据的长度起始,要发送给谁
            DatagramPacket packet = new DatagramPacket(message.getBytes(), 0, message.getBytes().length, inetAddress, port);

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


        }
        catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(socket != null){
                socket.close();
            }
        }

    }
}

UDP2

//还是要等待客户端的连接
public class UdpDemo2 {

    public static void main(String[] args) {

        DatagramSocket socket = null;

        try {
            //开放端口
             socket = new DatagramSocket(9090);

            //接收数据包
            byte[] buffer = new byte[1024];

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

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

            System.out.println(
                    new String( packet.getData(),0,packet.getLength()));

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(socket != null){
                socket.close();
            }
        }
    }
}

6.2 UDP Chat 单方发送接收

UdpChat1

public class UdpChat1 {

    public static void main(String[] args) {

        DatagramSocket socket = null;

        try {
            //socket
            socket = new DatagramSocket(8888);
            //准备数据 : 控制台读取 System.in
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            while(true){
                String data = reader.readLine();
                byte[] s1 = data.getBytes();
                //ip
                InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
                //向谁发送什么消息
                DatagramPacket packet = new DatagramPacket(s1,0,s1.length,inetAddress,9999);
                //发送
                socket.send(packet);
                if(data.equals("bye")){
                    break;
                }
            }



        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(socket != null){
                socket.close();
            }
        }
    }
}

UdpChat2

public class UdpChat2 {

    public static void main(String[] args) {

        try {
            DatagramSocket socket = new DatagramSocket(9999);

            while(true){    //循环接收
                //准备接收包裹
                byte[] bytes = new byte[1024];
                DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
                socket.receive(packet);    //阻塞接收

                //断开连接 bye
                byte[] data = packet.getData();
                String receiveData = new String(data,0,data.length);

                System.out.println(receiveData);

                if(receiveData.equals("bye")){
                    break;
                }
            }

            
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

7 Url 统一资源定位符

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

DNS域名解析 www.baidu.com 解析成 XXX.XXX.XXX.XXX

协议://ip地址:端口/项目名/资源

资源下载

public class UrlDownload {
    public static void main(String[] args) throws MalformedURLException {
        //1.下载地址
        URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg12.51tietu.net%2Fpic%2F2016-080609%2F201608060952210bwjqxkdu1l68137280x180.jpg");

        HttpURLConnection urlConnection = null;

        InputStream inputStream = null;

        FileOutputStream fos = null;

        try {
            //2.连接到这个资源HTTP
             urlConnection = (HttpURLConnection)url.openConnection();

             inputStream = urlConnection.getInputStream();

             fos = new FileOutputStream("girl.jpg");

            byte[] buffer = new byte[1024];
            Integer length;
            while((length=inputStream.read(buffer)) != -1){
                fos.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            if(urlConnection != null){
                try {
                    urlConnection.connect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:33:55  更:2021-08-09 10:34:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 8:02:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码