Socket实现UDP编程

Socket实现UDP编程_第1张图片
image.png
Socket实现UDP编程_第2张图片
image.png

适用于一次的少量的连接

服务器

package com.company;

import java.io.IOException;
import java.net.*;

public class UDPServer {
    public static void main(String[] args) {
        try {
            //1、创建服务器端DatagramSocket
            DatagramSocket datagramSocket = new DatagramSocket(8080);

            while (true){
                //2、创建数据报,用于接收客户端发送的数据
                byte[] bytes=new byte[1024];//1k
                DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
                System.out.println("服务器端等待新的客户端发送数据");
                //3、此方法在获取到数据报之前,会处于阻塞状态
                datagramSocket.receive(datagramPacket);

                new UDPServerThread(datagramSocket,datagramPacket,bytes).run();
            }


//            datagramSocket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

多线程

package com.company;

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

public class UDPServerThread extends Thread {

    DatagramPacket datagramPacket;
    byte[] bytes;

    //和本线程相关的DatagramSocket
    DatagramSocket datagramSocket = null;


    public UDPServerThread(DatagramSocket datagramSocket,DatagramPacket datagramPacket,
                           byte[] bytes) {
        this.datagramSocket = datagramSocket;
        this.datagramPacket = datagramPacket;
        this.bytes = bytes;
    }

    @Override
    public void run() {

        try {
            //4、读取数据
            String info = new String(bytes, 0, datagramPacket.getLength(), "gbk");
            System.out.println("获取到客户端数据:" + info);

            //5、响应客户端数据,创建数据包
            InetAddress address = datagramPacket.getAddress();//客户端地址
            int port = datagramPacket.getPort();//客户端端口
            byte[] bytes1 = "欢迎您".getBytes("gbk");
            DatagramPacket packet = new DatagramPacket(bytes1, bytes1.length, address, port);
            datagramSocket.send(packet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端

package com.company;

import java.io.IOException;
import java.net.*;

public class UDPClient {

    public static void main(String[] args) {
        try {
            //1、定义服务器的地址、端口号、数据
            InetAddress inetAddress = InetAddress.getByName("localhost");
            int port = 8080;
            //2、创建DatagramSocket对象
            DatagramSocket socket = new DatagramSocket();

            //3、创建数据报
            byte[] bytes = "admin;456".getBytes("gbk");
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, inetAddress, port);

            //4、发送数据报
            socket.send(packet);

            //5、闯将获取的数据报
            byte[] bytes1 = new byte[1024];
            DatagramPacket packet1 = new DatagramPacket(bytes1, bytes1.length);
            socket.receive(packet1);

            String info = new String(bytes1, 0, packet1.getLength(), "gbk");
            System.out.print("收到服务端数据:" + info);


            //5、关闭资源
            socket.close();

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

你可能感兴趣的:(Socket实现UDP编程)