工作生活

UDP基本步骤

2019-07-03  本文已影响0人  kanaSki

分析:
1.不需要利用IO流实现数据传输(注意是数据传输)
2.每个数据发送单元被统一封装成数据包的方式,发送方将数据包发送到网络中,数据包在网络中寻找目的地。

DatagramSocket:用于发送或者接受数据包的套接字
DatagramPacket:数据包

package com;

import java.net.*;

/**
 * 发送端
 */
public class UdpClient {
    public static void main(String[] args) throws Exception {
        System.out.println("发送方启动中");
        // 1.使用DatagramSocket创建发送端(指定端口)
        DatagramSocket client = new DatagramSocket(8888);
        // 2.准备数据 转成字节数组
        String data = "datatobytes";
        byte[] bytes = data.getBytes();
        // 3.封装成DatagramPacket包裹,需要指定目的地(ip+端口)
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("127.0.0.1", 9999));
        // 4.发送包裹——send
        client.send(datagramPacket);
        // 5.释放资源
        client.close();
    }
}

package com;

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

/**
 * 接收端
 */
public class UdpServer {
    public static void main(String[] args) throws Exception {
        // 1.使用DatagramSocket创建接收端(指定端口)
        DatagramSocket server = new DatagramSocket(9999);
        // 2.准备容器 封装成DatagramPacket包裹
        byte[] container = new byte[1024 * 60];
        DatagramPacket packet = new DatagramPacket(container, 0, container.length);
        // 3.阻塞式接收包裹——receive
        server.receive(packet);
        // 4.分析数据——getData,getLength
        byte[] data = packet.getData();
        System.out.println(new String(data, 0, data.length));
        // 5.释放资源
        server.close();
    }
}

处理基本类型:

package com;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * 发送端
 */
public class UdpTypeClient {
    public static void main(String[] args) throws Exception {
        System.out.println("发送方启动中");
        // 1.使用DatagramSocket创建发送端(指定端口)
        DatagramSocket client = new DatagramSocket(8888);
        // 2.准备数据 转成字节数组
        ByteArrayOutputStream boas = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(boas);
        dos.writeInt(2);
        dos.flush();
        byte[] bytes = boas.toByteArray();
        dos.close();
        // 3.封装成DatagramPacket包裹,需要指定目的地(ip+端口)
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("127.0.0.1", 9999));
        // 4.发送包裹——send
        client.send(datagramPacket);
        // 5.释放资源
        client.close();
    }
}


package com;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * 接收端
 */
public class UdpTypeServer {
    public static void main(String[] args) throws Exception {
        // 1.使用DatagramSocket创建接收端(指定端口)
        DatagramSocket server = new DatagramSocket(9999);
        // 2.准备容器 封装成DatagramPacket包裹
        byte[] container = new byte[1024 * 60];
        DatagramPacket packet = new DatagramPacket(container, 0, container.length);
        // 3.阻塞式接收包裹——receive
        server.receive(packet);
        // 4.分析数据——getData,getLength
        byte[] data = packet.getData();
        ByteArrayInputStream bias = new ByteArrayInputStream(data);
        DataInputStream dataInputStream = new DataInputStream(bias);
        System.out.println(dataInputStream.readInt());
        dataInputStream.close();
        // 5.释放资源
        server.close();
    }
}

对象类型:

package com;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * 发送端
 */
public class UdpObjClient {
    public static void main(String[] args) throws Exception {
        System.out.println("发送方启动中");
        // 1.使用DatagramSocket创建发送端(指定端口)
        DatagramSocket client = new DatagramSocket(8888);
        // 2.准备数据 转成字节数组
        ByteArrayOutputStream boas = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(boas);
        oos.writeObject(new Person("katsuna", 21));
        oos.flush();
        byte[] bytes = boas.toByteArray();
        oos.close();
        // 3.封装成DatagramPacket包裹,需要指定目的地(ip+端口)
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("127.0.0.1", 9999));
        // 4.发送包裹——send
        client.send(datagramPacket);
        // 5.释放资源
        client.close();
    }
}

package com;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * 接收端
 */
public class UdpObjServer {
    public static void main(String[] args) throws Exception {
        // 1.使用DatagramSocket创建接收端(指定端口)
        DatagramSocket server = new DatagramSocket(9999);
        // 2.准备容器 封装成DatagramPacket包裹
        byte[] container = new byte[1024 * 60];
        DatagramPacket packet = new DatagramPacket(container, 0, container.length);
        // 3.阻塞式接收包裹——receive
        server.receive(packet);
        // 4.分析数据——getData,getLength
        byte[] data = packet.getData();
        ByteArrayInputStream bias = new ByteArrayInputStream(data);
        ObjectInputStream ois = new ObjectInputStream(bias);
        Object o = ois.readObject();
        if (o instanceof Person) {
            System.out.println(((Person) o).name);
        }
        ois.close();
        // 5.释放资源
        server.close();
    }
}

package com;

import java.io.Serializable;

public class Person implements Serializable {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

上一篇下一篇

猜你喜欢

热点阅读