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学习(17)—— 网络编程 -> 正文阅读

[网络协议]Java学习(17)—— 网络编程

目录

一.?IP和端口号

? ? ? 1.?IP

? ? ? 2. 端口号

二.?网络协议

? ? ? 1.TCP/IP

?(1)客户端发送数据给服务器

?(2)客户端发送文件给服务器,服务器将文件保存到本地

?(3)客户端发送文件给服务器,服务器将文件保存到本地,并返回“发送成功”给? ?客户端

? ? ? 2.UDP/IP

三.?URL编程


网络编程的两个要素:① IP和端口号;② 提供网络通信协议

一.?IP和端口号

? ? ? 1.?IP

? ? ? ? ? ①IP:唯一标识Internet上的计算机;

? ? ? ? ? ②域名:www.baidu.com

? ? ? ? ? ③本地地址(localhost):172.0.0.1

? ? ? ? ? ④在Java中使用InetAddress类来代表IP,实例化InetAddress类:getByName(String host)、getLocalHost()

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

public class Test{
    public static void main(String[] args){
        try {
            InetAddress address1 = InetAddress.getByName("192.168.10.14");
            System.out.println(address1);    ///192.168.10.14

            InetAddress address2 = InetAddress.getByName("www.baidu.com");
            System.out.println(address2);    //www.baidu.com/14.215.177.38

            InetAddress address3 = InetAddress.getLocalHost();
            System.out.println(address3);

            //getHostName():获取域名
            System.out.println(address2.getHostName());    //www.baidu.com
            //getHostAddress():获取地址
            System.out.println(address2.getHostAddress());    //14.215.177.39

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

    }
}

? ? ? 2. 端口号

? ? ? ? ? 端口号:标识计算机上正在运行的进程,不同的进程有不同的端口号,如Tomcat的端口号是8080,MySQL端口号是3306。

? ? ? ? ? 端口号与IP地址组合得到网络套接字:Socket

二.?网络协议

? ? ? TCP:①使用TCP协议之前,需要先建立TCP连接,形成数据传输通道;②采用“三次握手”方式;③TCP协议用于客户端和服务器之间进行通信;④可以进行大量数据传输;⑤传输完毕后,需要释放已建立的连接,效率低。

? ? ? UDP:①将数据、源封装成数据包,不需要建立连接;②发送时不需要确定对方是否准备好,并且接收方收到后不需要确认;③可以广播发送;④每个数据包大小限制在64KB;⑤传输完毕后,不需要释放资源,开销小,速度快。

? ? ? 1.TCP/IP

? ? ? ? (1)客户端发送数据给服务器

import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest {

    //客户端
    @Test
    public void client() {
        InetAddress address = InetAddress.getByName("10.177.205.186");   //指明服务器的IP
        Socket socket = new Socket(address,8888);     //指明服务器的端口号

        OutputStream os = socket.getOutputStream();
        os.write("Hello".getBytes());

        os.close();
        socket.close();
    }



    //服务器
    @Test
    public void server(){
        ServerSocket serverSocket = new ServerSocket(8888);

        Socket socket1 = serverSocket.accept();

        InputStream is = socket1.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[20];
        int len = is.read(buffer);
        while(len != -1){
            baos.write(buffer,0,len);
            len = is.read(buffer);
        }
        System.out.println(baos.toString());

        baos.close();
        is.close();
        socket1.close();
        serverSocket.close();
    }

}
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest {

    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress address = InetAddress.getByName("10.177.205.186");
            socket = new Socket(address, 8888);     //1.创建客户端的Socket,指明服务器的IP和端口号

            os = socket.getOutputStream();     //2.获取一个输出流,用于输出数据
            os.write("Hello".getBytes());      //3.写出数据(发送给客户端的数据)
        } catch (IOException e) {
            e.printStackTrace();
        } finally {                         //4.关闭
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    //服务器
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket1 = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            serverSocket = new ServerSocket(8888);    //1.创建服务器的ServerSocket,指明自己的端口号

            socket1 = serverSocket.accept();        //2.调用accept()用于接收来自客户端的socket

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

            baos = new ByteArrayOutputStream();    //4.获取输入流中的数据
            byte[] buffer = new byte[20];
            int len = is.read(buffer);
            while (len != -1) {
                baos.write(buffer, 0, len);
                len = is.read(buffer);
            }
            
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {                            //5.关闭
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket1 != null) {
                try {
                    socket1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

? ? ? ? (2)客户端发送文件给服务器,服务器将文件保存到本地

import org.junit.Test;

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

public class TCPTest {

    //客户端
    @Test
    public void client() {
        InetAddress address = InetAddress.getByName("10.177.205.186");
        Socket socket = new Socket(address,8888);

        OutputStream os = socket.getOutputStream();

        File file = new File("test1.jpg");
        FileInputStream fis = new FileInputStream(file);

        byte[] buffer = new byte[1024];
        int len = fis.read(buffer);
        while(len != -1){
            os.write(buffer,0,len);
            len = fis.read(buffer);
        }

        fis.close();
        os.close();
        socket.close();
    }



    //服务器
    @Test
    public void server(){
        ServerSocket serverSocket = new ServerSocket(8888);

        Socket socket = serverSocket.accept();

        InputStream is = socket.getInputStream();

        File file = new File("test2.jpg");
        FileOutputStream fos = new FileOutputStream(file);

        byte[] buffer = new byte[1024];
        int len = is.read(buffer);
        while(len != -1){
            fos.write(buffer,0,len);
            len = is.read(buffer);
        }

        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }

}
import org.junit.Test;

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

public class TCPTest {

    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            InetAddress address = InetAddress.getByName("10.177.205.186");
            socket = new Socket(address,8888);

            os = socket.getOutputStream();

            File file = new File("test1.jpg");
            fis = new FileInputStream(file);

            byte[] buffer = new byte[1024];
            int len = fis.read(buffer);
            while(len != -1){
                os.write(buffer,0,len);
                len = fis.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    //服务器
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            serverSocket = new ServerSocket(8888);

            socket = serverSocket.accept();

            is = socket.getInputStream();

            File file = new File("test2.jpg");
            fos = new FileOutputStream(file);

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

}

? ? ? ? (3)客户端发送文件给服务器,服务器将文件保存到本地,并返回“发送成功”给客户端

import org.junit.Test;

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

public class TCPTest {

    //客户端
    @Test
    public void client() {
        InetAddress address = InetAddress.getByName("10.177.205.186");
        Socket socket = new Socket(address,8888);

        OutputStream os = socket.getOutputStream();

        File file = new File("test1.jpg");
        FileInputStream fis = new FileInputStream(file);

        byte[] buffer = new byte[1024];
        int len = fis.read(buffer);
        while(len != -1){
            os.write(buffer,0,len);
            len = fis.read(buffer);
        }

        socket.shutdownOutput();   //关闭数据的输出

        //接收来自服务器的数据
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer1 = new byte[1024];
        int len1 = is.read(buffer1);
        while(len1 != -1){
            baos.write(buffer1,0,len1);
            len1 = is.read(buffer1);
        }
        System.out.println(baos.toString());

        baos.close();
        is.close();
        fis.close();
        os.close();
        socket.close();
    }



    //服务器
    @Test
    public void server(){
        ServerSocket serverSocket = new ServerSocket(8888);

        Socket socket = serverSocket.accept();

        InputStream is = socket.getInputStream();

        File file = new File("test2.jpg");
        FileOutputStream fos = new FileOutputStream(file);

        byte[] buffer = new byte[1024];
        int len = is.read(buffer);
        while(len != -1){
            fos.write(buffer,0,len);
            len = is.read(buffer);
        }

        //向客户端发送数据
        OutputStream os = socket.getOutputStream();
        os.write("文件已收到");

        os.close();
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }

}
import org.junit.Test;

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

public class TCPTest {

    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            InetAddress address = InetAddress.getByName("10.177.205.186");
            socket = new Socket(address,8888);

            os = socket.getOutputStream();

            File file = new File("test1.jpg");
            fis = new FileInputStream(file);

            byte[] buffer = new byte[1024];
            int len = fis.read(buffer);
            while(len != -1){
                os.write(buffer,0,len);
                len = fis.read(buffer);
            }

            socket.shutdownOutput();   //关闭数据的输出

            //接收来自服务器的数据
            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[1024];
            int len1 = is.read(buffer1);
            while(len1 != -1){
                baos.write(buffer1,0,len1);
                len1 = is.read(buffer1);
            }
            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(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    //服务器
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            serverSocket = new ServerSocket(8888);

            socket = serverSocket.accept();

            is = socket.getInputStream();

            File file = new File("test2.jpg");
            fos = new FileOutputStream(file);

            byte[] buffer = new byte[1024];
            int len = is.read(buffer);
            while(len != -1){
                fos.write(buffer,0,len);
                len = is.read(buffer);
            }

            //向客户端发送数据
            os = socket.getOutputStream();
            os.write("文件已收到".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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();
                }
            }
        }
    }

}

? ? ? 2.UDP/IP

import org.junit.Test;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPTest {

    //客户端
    @Test
    public void client() {
        DatagramSocket socket = new DatagramSocket();

        String str = "Hello";
        byte[] data = str.getBytes();

        InetAddress address = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,address,8888);

        socket.send(packet);

        socket.close();
    }



    //服务器
    @Test
    public void server(){
        DatagramSocket socket = new DatagramSocket(8888);

        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

        socket.receive(packet);

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

        socket.close();
    }

}
import org.junit.Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPTest {

    //客户端
    @Test
    public void client() {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();

            String str = "Hello";
            byte[] data = str.getBytes();

            InetAddress address = InetAddress.getLocalHost();
            DatagramPacket packet = new DatagramPacket(data,0,data.length,address,8888);

            socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            socket.close();
        }
    }



    //服务器
    @Test
    public void server(){
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(8888);

            byte[] buffer = new byte[100];
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

            socket.receive(packet);

            System.out.println(new String(packet.getData(),0, packet.getLength()));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            socket.close();
        }
    }

}

三.?URL编程

? ? ? ? URL:统一资源定位符

????????getProtocol():获取URL的协议名

? ? ? ? getHost():获取URL的主机名

? ? ? ? getPort():获取URL的端口号

? ? ? ? getPath():获取URL的文件路径

? ? ? ? getFile():获取URL的文件名

? ? ? ? getQuery():获取URL的查询名

import java.net.MalformedURLException;
import java.net.URL;

public class Test{
    public static void main(String[] args){
        try {
            URL url = new URL("https://www.baidu.com/");

            System.out.println(url.getProtocol());
            System.out.println(url.getHost());
            System.out.println(url.getPort());
            System.out.println(url.getPath());
            System.out.println(url.getFile());
            System.out.println(url.getQuery());

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

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

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