Java学习-UDP编程

【简介】

UDP:以数据为中心 非面向连接 不安全 数据可能丢失 效率高
类 DatagramSocket和DatagramPacket
1.客户端
1)创建客户端 DatagramSocket类 + 指定发送端口
2)准备数据 字节数组
3)打包DatagramPacket + 服务器地址 及 端口
4)发送
5)释放资源
2.服务器端
1)创建服务器 DatagramSocket类 + 指定接收端口
2)准备接收容器 字节数组 封装DatagramPacket
3)封装成包
4)接收数据
5)分析
6)释放资源

【传入字符串】

客户端

package com.anqi.UDP;

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

public class MyClient {
    public static void main(String[] args) throws IOException {
        //1.创建客户端 + 端口
        DatagramSocket client = new DatagramSocket(6666);
        //2.准备数据
        String msg = "编程";
        byte[] data = msg.getBytes();
        //3.打包 发送的地点以及端口
        DatagramPacket packet = new DatagramPacket(data,data.length,new InetSocketAddress("localhost",8888));
        //4.发送
        client.send(packet);
        //5.释放
        client.close();
    }
}

服务器端

package com.anqi.UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class MyServer {
    public static void main(String[] args) throws IOException {
        //1.创建服务器端 + 端口
        DatagramSocket server = new DatagramSocket(8888);
        //2.准备接收容器
        byte[] container = new byte[1024];
        //3.封装成包
        //DatagramPacket(byte[] buf, int length) 
        DatagramPacket packet = new DatagramPacket(container,container.length);
        //4.接收数据
        server.receive(packet);
        //5.分析数据
        byte[] data = packet.getData();
        int len = packet.getLength();
        System.out.println(new String(data,0,len));
        //5.释放
        server.close();
    }
}

【传入double】

客户端

package com.anqi.UDP;

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

public class Client {
    public static void main(String[] args) throws IOException {
        //1.创建客户端 + 端口
        DatagramSocket client = new DatagramSocket(6666);
        //2.准备数据
        double num = 89.12;
        byte[] data = convert(num);
        //3.打包 发送的地点以及端口
        DatagramPacket packet = new DatagramPacket(data,data.length,new InetSocketAddress("localhost",9888));
        //4.发送
        client.send(packet);
        //5.释放
        client.close();
    }
    public static byte[] convert(double num) throws IOException{
        byte[] data = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeDouble(num);
        dos.flush();
        //获取数据
        data = bos.toByteArray();
        dos.close();
        return data;
    }
}

服务器端

package com.anqi.UDP;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Server {
    public static void main(String[] args) throws IOException {
        //1.创建服务器端 + 端口
        DatagramSocket server = new DatagramSocket(9888);
        //2.准备接收容器
        byte[] container = new byte[1024];
        //3.封装成包
        //DatagramPacket(byte[] buf, int length) 
        DatagramPacket packet = new DatagramPacket(container,container.length);
        //4.接收数据
        server.receive(packet);
        //5.分析数据
        double data  = convert(packet.getData());
        System.out.println(data);
        //5.释放
        server.close();
    }
    public static double convert(byte[] data) throws IOException{
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
        double num = dis.readDouble();
        dis.close();
        return num;
    }
}

你可能感兴趣的:(Java源码探索)