java网络编程

java 网络编程

网络编程三要素

ip地址

​ 设置在网络中的地址,是唯一标识

端口

​ 应用程序在设置种的唯一标识

协议

​ 数据在网络种传输的规则,常见的协议有UDP和TCP协议

ip

ipv4

ipv6

IPv4 :4个字节 1100000000 .... 太难记忆了变成10进制 192.168.1.166

运用了 点分10进制

image-20201001230125439

IPv6:

点分16进制

image-20201001230227782

InetAddress ip

image-20201001231112837

端口

应用程序在设置种的唯一标识

协议

链接和通信的规则被称为网络通信协议

UDP

​ 用户数据报协议

​ UDP是面向 无连接 通信协议

​ 速度快,大小一次最多64K,数据不安全,容易丢失数据

TCP

​ 传输控制协议

​ Tcp 协议是面向链接的通信协议

​ 速度慢,没有大小限制,数据安全

UDP

UDP 三种传播方式

单播

一对一

组播

一对组

范围( 224.0.0.0 到 239.255.255.255)

广播

一对所有

广播地址:255.255.255.255

单播

client

public static void main(String[] args) throws IOException {
    //创建数据报对象
    DatagramSocket ds = new DatagramSocket();
    //封装发送数据
    byte[] bytes = "hello world".getBytes();
    int lenth = bytes.length;
    InetAddress inetAddress = InetAddress.getByName("localhost");
    int port = 10000;
    DatagramPacket dp = new DatagramPacket(bytes,lenth,inetAddress,port);
    //发送数据
    ds.send(dp);
    System.err.println("数据发送完成");
    //释放资源
    ds.close();
}

server

public static void main(String[] args) throws IOException {

    //创建接收报包
    DatagramSocket ds = new DatagramSocket(10000);
    //创建接收包
    byte[] bytes = new byte[1024];
    DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
    //接收数据包
    ds.receive(dp);
    //打印数据
    System.err.println(new String(bytes,0,dp.getLength()));
    //释放资源
    ds.close();

}

组播

client

public static void main(String[] args) throws IOException {
    //创建报包对象
    DatagramSocket ds = new DatagramSocket();
    //创建数据包
    InetAddress inetAddress = InetAddress.getByName("224.0.0.1");
    byte[] bytes = "hello 组播".getBytes();
    int length = bytes.length;
    int port = 10000;
    DatagramPacket dp = new DatagramPacket(bytes,length,inetAddress,port);
    //发送数据包
    ds.send(dp);
    //释放资源
    ds.close();;
}

server

public static void main(String[] args) throws IOException {

    //创建接收报包
    MulticastSocket ms = new MulticastSocket(10000);
    byte[] bytes = new byte[1024];
    //封装数据
    DatagramPacket dp = new DatagramPacket(bytes, 1024);
    // 把当前计算机  绑定倒组播地址
    ms.joinGroup(InetAddress.getByName("224.0.0.1"));
    //准备接收请求
    ms.receive(dp);
    System.err.println(new String(bytes,0,dp.getLength()));
    //释放资源
    ms.close();
}

广播

广播地址 255.255.255.255 唯一

client

public static void main(String[] args) throws IOException {
    //创建数据报对象
    DatagramSocket ds = new DatagramSocket();
    //封装发送数据
    byte[] bytes = "广播 hello world".getBytes();
    int lenth = bytes.length;
    InetAddress inetAddress = InetAddress.getByName("255.255.255.255");
    int port = 10000;
    DatagramPacket dp = new DatagramPacket(bytes,lenth,inetAddress,port);
    //发送数据
    ds.send(dp);
    System.err.println("数据发送完成");
    //释放资源
    ds.close();
}

server

public static void main(String[] args) throws IOException {

    //创建接收报包
    DatagramSocket ds = new DatagramSocket(10000);
    //创建接收包
    byte[] bytes = new byte[1024];
    DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
    //接收数据包
    ds.receive(dp);
    //打印数据
    System.err.println(new String(bytes,0,dp.getLength()));
    //释放资源
    ds.close();
}

TCP

原理:

TCP 通信是一种可靠的网络协议,它在通信的网络各建立一个Socket对象

通信之前要保证连接已建立

通过Socket产生IO流来进行网络通信

应用

client

public static void main(String[] args) throws IOException {

    //建立一个socket对象
    Socket socket = new Socket("127.0.0.1",10000);

    // 获取一个IO流开始写数据
    OutputStream os = socket.getOutputStream();
    os.write("hello worold 你好,我是tcp client ".getBytes());
    os.flush();

    //释放资源
    os.close();
    socket.close();

}

server

public static void main(String[] args) throws IOException {

    //创建服务socket对象
    ServerSocket ss = new ServerSocket(10000);
    //等待客户端连接
    Socket accept = ss.accept();
    //获得输入流
    InputStream is = accept.getInputStream();
    byte[] bytes = new byte[1024];
    int l = -1;
    while( (l = is.read(bytes)) != -1){
        System.err.println(new String(bytes,0,l));
    }
    //释放资源
    is.close();
    ss.close();

}

tcp 原理分析

image-20201002004346011

三次握手

​ 建立连接的时候 需要三次握手,证明安全性

image-20201002004434918

四次挥手

终止连接时,需要用到四次挥手

保证成功终止链接

image-20201002004942461

练习 TCP

image-20201002005428193

image-20201002010251829

image-20201002010313728

tcp 练习2

上传文件 成功给出反馈

image-20201002010643649

image-20201002010800008

image-20201002010822561

posted @ 2023-07-31 16:22  lanwf  阅读(18)  评论(0编辑  收藏  举报