网络编程

1.1 概述

image-20210318143633537

image-20210318143800184

image-20210319100541426

1.2 网络通信的要素

image-20210319100937368

image-20210319101041468

image-20210319101157507

1.3 IP

image-20210319101547978

image-20210319102127223

–》代码实现IP

package javalearn.NetTest;

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

//03 IP编程
//测试IP
public class NetTest01 {
    public static void main(String[] args) {

        try {
            //查询本机地址
            InetAddress iNetAddress1 = InetAddress.getByName("127.0.0.1");//没有构造器,通过方法获取
            System.out.println(iNetAddress1);
            InetAddress iNetAddress2 = InetAddress.getByName("localhost");
            System.out.println(iNetAddress2);
            InetAddress iNetAddress3 = InetAddress.getLocalHost();
            System.out.println(iNetAddress3);

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

            //常用方法
            System.out.println(iNetAddress4.getAddress());
            System.out.println(iNetAddress4.getCanonicalHostName());//规范的名字
            System.out.println(iNetAddress4.getHostAddress());//IP
            System.out.println(iNetAddress4.getHostName());//域名,或者自己电脑的名字

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

image-20210319103649108

1.4 端口

image-20210319104113925

netstat -ano #查看所有端口

netstat -ano|findstr "5900" #查看指定端口

tasklist|findstr "8696" #查看指定端口的进程

ctrl+shift+ESC #打开任务管理器

代码实现如下:

package javalearn.NetTest;

import java.net.InetSocketAddress;

//端口
public class NetTest02 {
    public static void main(String[] args) {
        InetSocketAddress iNetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
        InetSocketAddress iNetSocketAddress2 = new InetSocketAddress("localhost", 8080);
        System.out.println(iNetSocketAddress);
        System.out.println(iNetSocketAddress2);

        System.out.println(iNetSocketAddress.getAddress());
        System.out.println(iNetSocketAddress.getHostName());//地址
        System.out.println(iNetSocketAddress.getPort());//端口
    }
}

image-20210319105203106

1.5 通信协议

image-20210319142417396

image-20210319142502922

image-20210319142628290

image-20210319142704919

image-20210319142820460

image-20210319142832855

1.6 TCP

发送消息

客户端

​ 1.连接服务器 Socket

​ 2.发送消息

package javalearn.NetTest;

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

//客户端
public class NetTest04 {
    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.发送消息
            os = socket.getOutputStream();
            os.write("你好!".getBytes());

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

服务器

​ 1.建立服务的端口 ServerSocket

​ 2.等待用户的连接 accept

​ 3.接收用户的消息

package javalearn.NetTest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务器端
public class NetTest03 {
    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();
                /**
                 //一般写法,若中文超过1024大小,就断掉了,输出后乱码
                 byte[] buffer = new byte[1024];
                 int len;
                 while ((len = is.read(buffer))!=-1){
                 String msg = new String(buffer,0,len);
                 System.out.println(msg);
                 }
                 */
                //管道流 不会断
                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 {
            //关闭资源
            //先开后关!
            try {
                if (baos!=null){
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null){
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (serverSocket!=null){
                    serverSocket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

文件上传

客户端

package javalearn.NetTest;

import sun.nio.cs.ext.ISO2022_CN;

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

public class NetTest05 {
    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("src/javalearn/NetTest/note.markdown"));

        //4.写出文件
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
        //通知服务器,我已经结束了
        socket.shutdownOutput();//我已经传输完了

        //确定服务器接收完毕,才能够断开连接
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[1024];
        int len2 = 0;
        while ((len2 = fis.read(buffer2))!=-1){
            baos.write(buffer2,0,len);
        }

        //5.关闭资源
        baos.close();
        inputStream.close();
        fis.close();
        os.close();
        socket.close();
    }
}

服务端

package javalearn.NetTest;

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

public class NetTest06 {
    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.markdown"));

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

        //通知客户端我接收完毕了
        OutputStream os = socket.getOutputStream();
        os.write("我接收完毕了,你可以断开了".getBytes());

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

    }
}

Tomcat

服务端

  • 自定义
  • Tomcat服务器 : Java后台开发

客户端

  • 自定义
  • 浏览器

1.7 UDP

发短信:不需要建立连接,需要知道对方的地址。

发送消息

package javalearn.NetTest;

//UDP传输,发送消息
//不需要连接服务器

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

public class NetTest07 {
    public static void main(String[] args) throws Exception {
        //1.建立一个Socket
        DatagramSocket socket = new DatagramSocket();
        //2.建个包

        //发送给谁
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        String msg ="你好啊,服务器!";
        //数据,数据的长度起始,要发给谁
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
        //3.发送包 IO流
        socket.send(packet);
        //4.关闭流
        socket.close();
    }
}

消息接收端

package javalearn.NetTest;

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

//UDP传输,接收消息
//还是要等待客户端的连接
public class NetTest08 {
    public static void main(String[] args) throws Exception {
        //开放端口
        DatagramSocket 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()));
        System.out.println(packet.getAddress());
        //关闭连接
        socket.close();
    }
}

咨询

发送端

package javalearn.NetTest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;

//UOP实现咨询
//发送方
public class NetTest09 {
    public static void main (String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);

        //准备数据,控制台读取System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true){
            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();
    }
}

接收端

package javalearn.NetTest;

import java.lang.String;

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

//UOP实现咨询
//接收方
public class NetTest10 {
    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);//阻塞式接收包裹

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

            System.out.println(receiveData);

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

在线咨询

两个人都可以是发送方,也可以是接收方

1.8 URL

https://www.zhihu.com/hot

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

DNS域名解析 域名-》IP

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

冷秋