UDP(user datagram protocol: 用户数据报文协议)网络编程:
	java中所有跟网络相关对象,都是存放在java.net包

使用UDP发送数据

package com.openlab.day26.net;

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

public class TestDUP {
    public static void main(String[] args) {

        // 创建一个报文协议的socket
        DatagramSocket datagramSocket = null;
        try {
            // 创建的没有指定端口和ip地址
//            datagramSocket = new DatagramSocket();
            // 指定端口
            datagramSocket = new DatagramSocket(8080);
            // 同时指定ip地址和端口
//            datagramSocket = new DatagramSocket(new InetSocketAddress("127.0.0.1", 8888));
            // 也可以在创建完成后执行
//            datagramSocket.bind(new InetSocketAddress("127.0.0.1", 8888));
            String msg = "刘建宏是个大帅哥";
            byte[] bytes = msg.getBytes();
            // 指定编码
//            byte[] bytes = msg.getBytes("gbk");
            // 用户数据报协议,传输的数据都是报文数据
            DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("192.168.31.23", 8888));
            datagramSocket.send(datagramPacket);
            System.out.println("数据发送成功……");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (datagramSocket != null) {
                datagramSocket.close();
            }
        }

    }
}

使用UDP接数据

package com.openlab.day26.net;

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

public class TestUDP02 {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            // 如果ip不指定,默认获取当前系统ip
            datagramSocket = new DatagramSocket(8888);
            byte[] buf = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(buf, 0, buf.length);
            System.out.println("服务器等待接受数据……");
            // 接受报文数据
            datagramSocket.receive(datagramPacket);
            // 完成接受之后

            byte[] data = datagramPacket.getData();
            String msg = new String(data, 0, data.length);

            System.out.println("接收到"+ datagramPacket.getAddress().getHostAddress() +":"+ datagramPacket.getPort() +"信息,信息是:"+ msg);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (datagramSocket != null)
                datagramSocket.close();
        }
    }
}

使用UDP循环发送数据

package com.openlab.day26.net;

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

public class TestUDP03 {
    private static final String DESPATH = "192.168.31.23";
    private static final Integer DESPORT = 9999;

    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(8888);
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.print("请输入你好发送的数据:");
                String msg = scanner.nextLine();
                byte[] buf = msg.getBytes();
                DatagramPacket dp = new DatagramPacket(buf, 0, buf.length, new InetSocketAddress(DESPATH, DESPORT));

                socket.send(dp);
                System.out.println("发送成功……");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }
}

使用UDP循环接收数据

package com.openlab.day26.net;

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

public class TestUDP04 {
    private static final String DESPATH = "192.168.31.23";
    private static final Integer DESPORT = 8888;

    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(9999);
            while (true) {
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacket(buf, 0, buf.length);
                System.out.println("等待接受数据中……");

                socket.receive(dp);
                byte[] data = dp.getData();
                String msg = new String(data, 0, data.length);
                System.out.println("接收到"+ dp.getAddress().getHostAddress() +":"+ dp.getPort() +"信息,信息是:"+ msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }
}

使用UDP多线程实现数据的发送和接收

相当于是一个控制台版的聊天小程序,将该程序放在两台电脑上,将IP地址分别改为对方的IP地址,两台电脑就可以开始聊天了。

package com.openlab.day26.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;

public class TestUDP05 {
    public static void main(String[] args) {
        DatagramSocket datagramSocket=null;
        try {
            datagramSocket=new DatagramSocket(8889);
            DataSend ds=new DataSend(datagramSocket);
            DataRecive dr=new DataRecive(datagramSocket);
            new Thread(ds).start();
            new Thread(dr).start();
        } catch (SocketException e) {
            e.printStackTrace();
        }

    }
}
class DataSend implements Runnable{
    DatagramSocket datagramSocket=null;
    private final static String DESPATH="192.168.43.145";
    private final static int DESPORT=8889;
    public DataSend(DatagramSocket datagramSocket) {
        this.datagramSocket = datagramSocket;
    }
    @Override
    public void run() {
        Scanner sc=new Scanner(System.in);
        try {
            while (true){
                System.out.print(">");
                String msg=sc.nextLine()+"\n";
                if(msg.trim().equalsIgnoreCase("exit")){
                    break;
                }
                byte[] buf=msg.getBytes();
                DatagramPacket datagramPacket=new DatagramPacket(buf,0,buf.length,
                        new InetSocketAddress(DESPATH,DESPORT));
                datagramSocket.send(datagramPacket);
                System.out.println("发送成功!!!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(datagramSocket!=null){
                datagramSocket.close();
            }
        }
    }
}
class DataRecive implements Runnable{
    DatagramSocket datagramSocket=null;

    public DataRecive(DatagramSocket datagramSocket) {
        this.datagramSocket = datagramSocket;
    }
    //实现数据的接收到
    @Override
    public void run() {
        try {
            while (true){
                byte[] buf=new byte[1024];
                DatagramPacket datagramPacket=new DatagramPacket(buf,0,buf.length);
                datagramSocket.receive(datagramPacket);
                byte[] data = datagramPacket.getData();
                String msg=new String(data,0,data.length);
//                System.out.println(datagramPacket.getAddress().getHostName()+":"+msg);
                System.out.print("接收到"+datagramPacket.getAddress().getHostAddress()+
                        ":"+datagramPacket.getPort()+"的信息,信息是:"+msg.trim()+"\n"+">");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(datagramSocket!=null){
                datagramSocket.close();
            }
        }
    }
}