Android基础Java 基础

Java 基础 11. Java 网络编程

2022-01-21  本文已影响0人  yjtuuige
1. 概述
2. 网络通信要素

如何实现网络的通信?

小结:

  1. 网络编程中有两个主要的问题:
    • 如何让准确的定位到网络上的一台或多台主机;
    • 找到主机之后如何通信;
  2. 网络编程中的要素:
    • IP和端口号:IP
    • 网络通信协议:UDP、TCP
  3. 万物皆对象
3. IP

IP 地址:InetAddress

2406:da18:ddf5:4000:67d5:b226:cad7:125b
package com.xxx.net.demo01;

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);
            InetAddress inetAddress3 = InetAddress.getByName("localhost");
            System.out.println(inetAddress3);
            InetAddress inetAddress4 = InetAddress.getLocalHost();
            System.out.println(inetAddress4);
            // 查询网站地址
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);
            // 常用方法
            // System.out.println(inetAddress2.getAddress());   // 返回的是一个字节数组  无用
            System.out.println(inetAddress2.getCanonicalHostName());    // 规范的名字
            System.out.println(inetAddress2.getHostAddress());  // IP
            System.out.println(inetAddress2.getHostName()); // 域名,或者自己电脑的名字
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
4. 端口
netstat -ano # 查看所有的端口
netstat -nao|findstr "7808" # 查看指定的端口
tasklist|findstr "8696"  # 查看指定端口的进程
Ctrl + Shift + Esc  # 任务管理器,快捷键
package com.xxx.net.demo01;

import java.net.InetSocketAddress;

/**
 * InetSocketAddress
 */
public class TestInetSocketAddress {
    public static void main(String[] args) {
        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
        InetSocketAddress socketAddress1 = new InetSocketAddress("localhost", 8080);
        System.out.println(socketAddress);
        System.out.println(socketAddress1);

        System.out.println(socketAddress.getAddress());
        System.out.println(socketAddress.getHostName());    // 地址
        System.out.println(socketAddress.getPort());    // 端口
    }
}
5. 通信协议

重要:

出名的协议:

TCP UDP 对比

6. TCP

先启动服务端,再启动客户端!!!!

package com.xxx.net.demo01;

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

/**
 * 客户端
 * Socket:客户端与服务器连接的插槽
 */
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("客户端发送消息...".getBytes());
        } catch (Exception e) { // 异常的作用域提升到最大,方便捕获 Socket 的异常
            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.xxx.net.demo01;

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

/**
 * 服务端
 * Socket:服务器与客户端连接的插槽
 */
public class TcpServerDemo01 {
    public static void main(String[] args) {
        // 提升变量作用域,便于在 finally 中操作
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1.创建一个地址
            serverSocket = new ServerSocket(9999);
            while (true) {
                // 2.等待客户端连接过来 accept() 监听
                // 连接成功后,Socket 与客户端为同一对象
                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());
            }
            /*
            // 此方式,会有中文乱码
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                String s = new String(buffer, 0, len);
                System.out.println(s);
            }
            */
        } 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();
                }
            }
        }
    }
}
  1. 文件上传
package com.xxx.net.demo01;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 客户端:文件上传
 */
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("001.jpg");
        // 4.写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        // 5.通知服务器,已经结束
        // shutdownOutput() 传输完毕
        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);
        }
        // 6.关闭资源
        baos.close();
        inputStream.close();
        fis.close();
        os.close();
        socket.close();
    }
}
package com.xxx.net.demo01;

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("receive.jpg");
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        // 5.通知客户端,接收完毕 socket.shutdownInput();
        OutputStream os = socket.getOutputStream();
        os.write("服务器接收完毕,可以断开连接".getBytes());
        // 6.关闭资源
        os.close();
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}
  1. 初识 Tomcat
7.UDP
  1. 发送消息
package com.xxx.net.demo03;

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

/**
 * UDP:发送端
 * 不需要连接服务器
 */
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;
        // 参数:数据、数据起始位、数据结束位、IP地址、端口号
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),
                0, msg.getBytes().length, localhost, port);
        // 3.发送包
        socket.send(packet);
        // 4.关闭流
        socket.close();
    }
}
package com.xxx.net.demo03;

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

/**
 * UDP:接收端
 * 需要等待发送端发送数据
 */
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()));
        // 3.关闭连接
        socket.close();
    }
}
  1. 循环发送消息
package com.xxx.net.chat;

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

/**
 * UDP:发送端
 */
public class UdpSenderDemo01 {
    public static void main(String[] args) throws Exception {
        // 1.建立一个 Socket
        DatagramSocket socket = new DatagramSocket(8888);
        // 2.建个包
        // 准备数据:控制台读取 System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            // data 不可读,需转换成字节
            String data = reader.readLine();
            // data 转为字节
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas, 0,
                    datas.length, new InetSocketAddress("localhost", 6666));
            // 3.发送包
            socket.send(packet);
            if (data.equals("bye")) {
                break;
            }
        }
        // 4.关闭
        reader.close();
        socket.close();
    }
}
package com.xxx.net.chat;

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

/**
 * UDP:接收端
 */
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);
            //  data 得到数组
            byte[] data = packet.getData();
            // 注意:这里也可以打印出接收数据,但会多出很多空格,因为打印了数组 1024B 的字节
            // String receiveData = new String(data);
            // packet.getLength() 得到实际数据长度,而非数组总长度 byte[1024]
            String receiveData = new String(data, 0, packet.getLength());
            System.out.println(receiveData);
            // 断开连接 bye
            if (receiveData.equals("bye")) {
                break;
            }
        }
        // 关闭资源
        socket.close();
    }
}

在线咨询:两个人都可以是发送方,也可以是接收方(配合多线程)

package com.xxx.net.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 TalkSend implements Runnable {
    DatagramSocket socket = null;
    BufferedReader reader = null;
    private int fromPort;
    private String toIP;
    private int toPort;

    public TalkSend(int fromPort, String toIP, int toPort) {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;
        try {
            socket = new DatagramSocket(fromPort);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0,
                        datas.length, new InetSocketAddress(this.toIP, this.toPort));
                socket.send(packet);
                if (data.equals("bye")) {
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 4.关闭
        socket.close();
    }
}
package com.xxx.net.chat;

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

/**
 * 接收端线程
 */
public class TalkReceive implements Runnable {
    DatagramSocket socket = null;
    private int port;
    private String msgFrom;

    public TalkReceive(int port, String msgFrom) {
        this.port = port;
        this.msgFrom = msgFrom;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                // 准备接收包
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container, 0, container.length);
                // 阻塞式接收包
                socket.receive(packet);
                // data 得到实际数据的数组
                byte[] data = packet.getData();
                // 注意:packet.getLength() 得到数组内实际数据的长度,而非数组的总长度 byte[1024]
                String receiveData = new String(data, 0, packet.getLength());
                System.out.println(msgFrom + ":" + receiveData);
                // 断开连接 bye
                if (receiveData.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 关闭资源
        socket.close();
    }
}
package com.xxx.net.chat;

/**
 * 老师端
 */
public class TalkTeacher {
    public static void main(String[] args) {
        // 开启两个线程
        new Thread(new TalkSend(5555, "localhost", 8888)).start();
        new Thread(new TalkReceive(9999, "学生")).start();
    }
}
package com.xxx.net.chat;

/**
 * 学生端
 */
public class TalkStudent {
    public static void main(String[] args) {
        // 开启两个线程
        new Thread(new TalkSend(7777, "localhost", 9999)).start();
        new Thread(new TalkReceive(8888, "老师")).start();
    }
}
8.URL
协议:// IP地址:端口号/项目名/资源
package com.xxx.net.demo04;

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

/**
 * URL
 */
public class URLDemo01 {
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/hello/index.jsp?" +
                "username=liu&password=123");
        System.out.println(url.getProtocol());  // 协议
        System.out.println(url.getHost());  // 主机IP
        System.out.println(url.getPort());  // 端口
        System.out.println(url.getPath());  // 路径
        System.out.println(url.getFile());  // 文件名
        System.out.println(url.getQuery()); // 参数
    }
}
package com.xxx.net.demo04;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 下载网页上的文件
 */
public class URLDown {
    public static void main(String[] args) throws Exception {
        // 1.下载地址
        URL url = new URL("http://localhost:8080/hello/test.txt");
        // 2.连接到这个资源 Http
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fs = new FileOutputStream("11.txt");
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            // 写出这个数据
            fs.write(buffer, 0, len);
        }
        fs.close();
        inputStream.close();
        // 断开连接
        urlConnection.disconnect();
    }
}
上一篇下一篇

猜你喜欢

热点阅读