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 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 网络编程——Java -> 正文阅读

[网络协议]网络编程——Java


1、什么是计算机网络

在这里插入图片描述
什么是计算及网络?
计算机网络系统就是利用通信设备和线路将地理位置不同、功能独立的多个计算机系统互联起来,以功能完善的网络软件实现网络中资源共享和信息传递的系统。
网络编程的目的
数据交换----通信
在这里插入图片描述

在这里插入图片描述


2、网络通信的两个要素

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

  • 如何准确的定位到网络.上的一台或者多台主机
  • 找到主机之后如何进行通信

2.网络编程中的要素

  • IP和端口号IP.
  • 网络通信协议udp, tcp

3.万物皆对象


3、IP地址

java.net.InetAddress
在这里插入图片描述
在这里插入图片描述

package com.yangbocsu.lesson1;


import java.net.InetAddress;
import java.net.UnknownHostException;

//测试IP
public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查询本机地址
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println("inetAddress1:"+inetAddress1);

            InetAddress inetAddress2 = InetAddress.getByName("localhost");
            System.out.println("inetAddress2:"+inetAddress2);

            InetAddress inetAddress3 = InetAddress.getLocalHost();
            System.out.println("inetAddress3:"+inetAddress3);


            //查询网站Ip地址
            InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
            System.out.println("inetAddress1:"+inetAddress4);

            //常用方法
            System.out.println(inetAddress4.getAddress());
            System.out.println(inetAddress4.getHostAddress());
            System.out.println(inetAddress4.getCanonicalHostName());
            System.out.println(inetAddress4.getHostName());

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


4、端口Port

在这里插入图片描述
在这里插入图片描述

package com.yangbocsu.lesson1;
 
import java.net.InetSocketAddress;

public class TestInetSocketAddress {
    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);

        System.out.println(socketAddress.getAddress());
        System.out.println(socketAddress.getHostName());
        System.out.println(socketAddress.getPort());
    }
    }



5、通信协议

协议:约定,就好比我们现在说的普通话。
网络通信协议: 速率、传输码率、代码结构、传输控制。。。。
**问题:**非常复杂?
大事化小:分层!
TCP/IP协议簇
重要:

  • TCP(Transmission Control Protocol,传输控制协议)
  • UDP(User Data Protocol,用户数据报协议)

出名的协议:

  • TCP:
  • IP : 网络互连协议

在这里插入图片描述在这里插入图片描述


6、TCP实现聊天

在这里插入图片描述

服务端 - 客户端

服务端

package com.yangbocsu.lesson2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
public class TcpServerDemo01 {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is =null;
        ByteArrayOutputStream baos = null;
        try {
            //1. 我得要有一个地址  localhost:9999
            serverSocket = new ServerSocket(9999);

            //2.等待客户端连接过来
            socket = serverSocket.accept();

            //3.读取客户端的消息
            is = socket.getInputStream();

            //管道流
            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();
                }
            }

        }
    }
    
}

客户端

package com.yangbocsu.lesson2;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//客户端
public class TcpClientDemo01 {
    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("hello world!".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();
                }
            }


        }

    }
}

服务端一直监听:

package com.yangbocsu.lesson2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
public class TcpServerDemo01 {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is =null;
        ByteArrayOutputStream baos = null;
        try {
            //1. 我得要有一个地址  localhost:9999
            serverSocket = new ServerSocket(9999);

            while (true) //服务端一直监听
            {
                //2.等待客户端连接过来
                socket = serverSocket.accept();

                //3.读取客户端的消息
                is = socket.getInputStream();

                //管道流
                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();
                }
            }

        }
    }
    
}


7、TCP文件上传实现

服务端

package com.yangbocsu.lesson2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo02 {
    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("receive1.png"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer))!=-1)
        {
            fos.write(buffer,0,len);
        }

        //通知客户端我接收完毕了
        OutputStream os = socket.getOutputStream();
        os.write("Over!  Over!".getBytes());

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



    }
}

客户端

package com.yangbocsu.lesson2;

import java.io.*;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.Socket;

import static java.net.InetAddress.*;

public class TcpClientDemo02 {
    public static void main(String[] args) throws Exception {
        //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();
        //String byte[]
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

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

        System.out.println(baos.toString());
        //5.关闭资源
        baos.close();
        inputStream.close();
        fis.close();
        os.close();
        socket.close();



    }

}


8、初识Tomcat

在这里插入图片描述


9、UDP消息发送

UDP

package com.yangbocsu.lesson03;

import sun.security.krb5.internal.PAData;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpServerDemo01 {
    public static void main(String[] args) throws Exception {
        //1.开放端口
        DatagramSocket socket = new DatagramSocket(9090);

        //2.接收数据包
        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();




    }

}

package com.yangbocsu.lesson03;

import com.yangbocsu.lesson1.TestInetAddress;

import java.net.*;
import java.nio.charset.StandardCharsets;

//不需要连接服务器
public class UdpClientDemo01 {
    public static void main(String[] args) throws Exception {
        //1.简历一个socket
        DatagramSocket socket = new DatagramSocket();

        //2.建立一个包
        String msg = "你好呀,服务器!";
        //发送给谁?

        InetAddress localhost = InetAddress.getByName("localhost");

        int port = 9090;
       DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);

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

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


10、UDP聊天实现

package com.yangbocsu.chat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpReceiveDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);


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

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

            System.out.println(receivedata);

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


        }
        socket.close();

    }
}

package com.yangbocsu.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class UdpSendDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);

        //准备数据:从控制台读取
        while (true) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String data = reader.readLine();
            byte[] datas = data.getBytes();

            DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));

            socket.send(packet);
            if (data.equals("bye")) {
                break;
            }

        }
        socket.close();
    }
}


11、UDP多线程在线咨询


12、URL下载网络资源

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-08-17 15:45:03  更:2021-08-17 15:45:30 
 
开发: 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/17 15:34:06-

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