UDP,循环单向聊天编程--Java

在这里插入图片描述

代码实现

package udpProgram;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/**
 * @author 
 * @date 2021/5/19-10:37
 **/
public class UDPDemo1 {
    @Test
    public void client() throws IOException {
        //1.create a sender to send dataPacket
        DatagramSocket s1 = new DatagramSocket();

        //2.pack want send message into a dataPacket
        String dp = "good morning!";

        //3.you must know receiver address and port before sending dataPacket
        InetAddress addr = InetAddress.getByName("localhost");
        int port = 8888;

        //4.pack dataPacket
        DatagramPacket pa = new DatagramPacket(dp.getBytes(StandardCharsets.UTF_8), 0, dp.getBytes(StandardCharsets.UTF_8).length, addr, port);

        //5.send
        s1.send(pa);

        s1.close();
    }

    @Test
    public void sever() throws IOException {
        DatagramSocket s1 = new DatagramSocket(8888);
        byte[] byte1 = new byte[1024];
        DatagramPacket packet = new DatagramPacket(byte1, 0, byte1.length);
        s1.receive(packet);
        System.out.println(new String(packet.getData()));
        s1.close();
    }
}

循环单向聊天

package udpProgram;

import org.junit.jupiter.api.Test;

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.nio.charset.StandardCharsets;

/**
 * @author 
 * @date 2021/5/19-11:33
 **/
public class UDPChatting {
    @Test
    public void user1() throws IOException {
        DatagramSocket socket1 = new DatagramSocket(6666);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String data = reader.readLine();
            DatagramPacket dataPacket = new DatagramPacket(data.getBytes(StandardCharsets.UTF_8), 0, data.getBytes(StandardCharsets.UTF_8).length, new InetSocketAddress("localhost", 8888));
            socket1.send(dataPacket);
            if (data.equals("bye")) {
                break;
            }
        }
        socket1.close();
    }

    @Test
    public void user2() throws IOException {
        DatagramSocket socket1 = new DatagramSocket(8888);
        while (true) {
            byte[] bytes = new byte[1024];
            DatagramPacket dataPacket = new DatagramPacket(bytes, 0, bytes.length);
            socket1.receive(dataPacket);
            String message = new String(dataPacket.getData());
            System.out.println(message);
            // message late maybe has space, so we should delete space, then compare
            String messTrim = message.trim();
            if (messTrim.equals("bye")) {
                break;
            }
        }
        socket1.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MC6058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值