UDP实现在线聊天功能

发送端

//发送
public class UDPChat01 {
    public static void main(String[] args) throws Exception {
        //开启端口
        DatagramSocket socket = new DatagramSocket(8888);

        while (true){
            //准备数据:创建控制台
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
            socket.send(packet);
            //退出
            if (data.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}

接收端

//接受
public class UDPChat02 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);

        while (true){
            //准备接受包裹
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
            socket.receive(packet);

            //判断结束
            byte[] data = packet.getData();
            String receiveData = new String(data, 0, data.length);
            System.out.println(receiveData);
            if (receiveData.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}

将方法封装

//发送端
public class TalkSender implements Runnable {
    DatagramSocket socket = null;
    BufferedReader reader = null;
    int fromPort;
    String toIp;
    int toPort;

    public TalkSender(String toIp, int fromPort, int toPort) {
        this.fromPort = fromPort;
        this.toIp = toIp;
        this.toPort = toPort;
        try {
            socket = new DatagramSocket(fromPort);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            String data = null;
            try {
                data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(toIp, toPort));
                socket.send(packet);
                //退出
                if (data.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}
public class TalkReceiver implements Runnable {
    DatagramSocket socket = null;
    int port;
    String msgFrom;

    public TalkReceiver(int port, String msgFrom) {
        this.port = port;
        this.msgFrom = msgFrom;
        try {
            socket = new DatagramSocket(port);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            //准备接受包裹
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
            try {
                socket.receive(packet);
                //判断结束
                byte[] data = packet.getData();
                String receiveData = new String(data, 0, data.length);
                System.out.println(msgFrom + receiveData);
                if (receiveData.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}
public class Teacher {
    public static void main(String[] args) {
        new Thread(new TalkSender("127.0.0.1",6666,8888)).start();
        new Thread(new TalkReceiver(7777,"学生:")).start();
    }
}
public class Student {
    public static void main(String[] args) {
        System.out.println("------------咨询课程------------");
        new Thread(new TalkSender("localhost",5555,7777)).start();
        new Thread(new TalkReceiver(8888,"老师:")).start();
    }
}
posted @ 2020-03-30 16:30  shimmernight  阅读(213)  评论(0编辑  收藏  举报