java Socket编程(udp篇)

概述

关于网络的基础知识,请自行google,这里通过socket(udp)来实现一个简单的对话。

Udp协议的Socket

服务端五步走:
1.初始化DatagramSocket,指定端口号
2.创建用于接收消息的DatagramPacket,指定接收数据大小
3.接收客户端消息
4.创建用于发送消息的DatagramPacket
5.向客户端发送消息

package udp;

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

public class Server {
    private static DatagramSocket mSocket;

    public static void main(String[] args) {
        try {
            // 1.初始化DatagramSocket,指定端口号
            mSocket = new DatagramSocket(8888);
            // 2.创建用于接收消息的DatagramPacket,指定接收数据大小
            
            // 3.接收客户端消息
            System.out.println("***************服务器开始监听消息***************");
            while (true) {
                byte[] receiveData = new byte[1024];
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                mSocket.receive(receivePacket);// 在接收到信息之前,一直保持阻塞状态
                System.out.println("客户端说:" + new String(receiveData));
                HandleThread handleThread = new HandleThread(receivePacket);
                handleThread.setPriority(4);
                handleThread.start();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mSocket.close();// 关闭资源
        }
    }

    static class HandleThread extends Thread {
        private DatagramPacket mPacket;
        private DatagramSocket mSocket;

        public HandleThread(DatagramPacket packet) {
            super();
            mPacket = packet;
        }

        @Override
        public void run() {
            try {
                // 4.创建用于发送消息的DatagramPacket
                byte[] sendData = "hello,我是服务端".getBytes();
                SocketAddress remoteAddress = mPacket.getSocketAddress();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, remoteAddress);
                // 5.向客户端发送消息
                mSocket = new DatagramSocket();
                mSocket.send(sendPacket);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (mSocket != null) {
                    mSocket.close();// 关闭资源
                }
            }
        }
    }
}

客户端五步走:
1.初始化DatagramSocket
2.创建用于发送消息的DatagramPacket
3.向服务端发送消息
4.创建用于接收消息的DatagramPacket
5.接收服务端消息

package udp;

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

public class Client {

    private static DatagramSocket mSocket;

    public static void main(String[] args) {
        try {
            // 1.初始化DatagramSocket
            mSocket = new DatagramSocket();
            mSocket = new DatagramSocket();
            // 2.创建用于发送消息的DatagramPacket
            InetSocketAddress address = new InetSocketAddress("localhost", 8888);
            byte[] sendData = ("hello,我是客户端" + (int) (Math.random() * 100)).getBytes();// 随机数模拟不同的客户端
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address);
            // 3.向服务端发送消息
            mSocket.send(sendPacket);
            // 4.创建用于接收消息的DatagramPacket
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            // 5.接收服务端消息
            mSocket.receive(receivePacket);
            System.out.println("服务端说:" + new String(receiveData));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } finally {
            if (mSocket != null) {
                mSocket.close();
            }
        }
    }

}

测试一下

先启动服务端,再启动客户端(多次启动,模拟多用户访问服务器)
服务端打印:

java Socket编程(udp篇)_第1张图片
image.png

客户端打印:

java Socket编程(udp篇)_第2张图片
image.png

你可能感兴趣的:(java Socket编程(udp篇))