[Java网络编程] UDP协议实现简单一对一聊天

UDP_Send 类用来发送消息

package NET;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class UDP_Send implements Runnable {
    private String toIP;
    private int toPort;
    private DatagramSocket sender;
    private BufferedReader reader;

    public UDP_Send(int fromPort, String toIP, int toPort) throws SocketException {
        this.toIP = toIP;
        this.toPort = toPort;
        this.sender = new DatagramSocket(fromPort);
        this.reader = new BufferedReader(new InputStreamReader(System.in));
    }

    @Override
    public void run() {
        while (true) {
            String msg;
            try {
                msg = reader.readLine();
                byte[] data = msg.getBytes();
                DatagramPacket packet = new DatagramPacket(data, data.length,
                        new InetSocketAddress(toIP, toPort));
                sender.send(packet);

                if (msg.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        sender.close();
    }
}

UDP_Receive 类用来接收消息

package NET;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Arrays;

public class UDP_Receive implements Runnable {
    private DatagramSocket recipient;

    UDP_Receive(int fromPort) throws SocketException {
        recipient = new DatagramSocket(fromPort);
    }

    @Override
    public void run() {
        while (true) {
            byte[] container = new byte[1<<16];
            DatagramPacket packet = new DatagramPacket(container, container.length);
            try {
                recipient.receive(packet);
                byte[] data = packet.getData();
                String msg = new String(data, 0, data.length);
                System.out.println("对方:" + msg);
                if (msg.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        recipient.close();
    }
}

由本机发送并由本机接收,模拟两人一对一聊天。其中,用户一(UDP_Request)通过端口 11111 发送消息给用户二(UDP_Response)监听的端口 22222,用户二通过端口 33333 发送消息给用户一监听的端口 44444。将消息显示在控制台上,以实现简单通信。

package NET;

import java.net.SocketException;

public class UDP_Request {
    public static void main(String[] args) throws SocketException {
        new Thread(new UDP_Send(11111, "localhost", 22222)).start();
        new Thread(new UDP_Receive(44444)).start();
    }
}
package NET;

import java.net.SocketException;

public class UDP_Response {
    public static void main(String[] args) throws SocketException {
        new Thread(new UDP_Receive(22222)).start();
        new Thread(new UDP_Send(33333, "localhost", 44444)).start();
    }
}

猜你喜欢

转载自blog.csdn.net/HNUCSEE_LJK/article/details/104237426