Java——多线程实现聊天室程序

A端

public class A {

    public static void main(String[] args) throws IOException {
        //子线程用来读取消息
        new Thread() {
            @Override
            public void run() {
                try {
                    DatagramSocket ds = new DatagramSocket(8888);
                    while (true) {
                        byte[] bytes = new byte[1024];
                        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                        System.out.println("A服务器已经开启,等待接收数据.....");
                        ds.receive(dp);
                        //取出数据
                        byte[] data = dp.getData();
                        int length = dp.getLength(); //取出数据的实际长度
                        //获取客户端IP
                        String ip = dp.getAddress().getHostAddress();
                        String s = new String(data, 0, length);
                        System.out.println(ip + "-给你发来消息:" + s);
                        if ("886".equals(s)) {
                            break;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();

        //主线程用来发消息
        sendMsg();
    }

    private static void sendMsg() throws IOException {
        DatagramSocket ds = new DatagramSocket();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("请输入要发送给B的消息");
            String msg = reader.readLine();
            byte[] bytes = msg.getBytes();
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.15.90"), 8888);
            ds.send(dp);
            //自定义结束标记
            if ("886".equals(msg)) {
                break;
            }

        }
        ds.close();
    }
}

B端

public class B {
    public static void main(String[] args) throws IOException {
        //子线程用来读取消息
        new Thread() {
            @Override
            public void run() {
                try {
                    DatagramSocket ds = new DatagramSocket(8888);
                    while (true) {
                        byte[] bytes = new byte[1024];
                        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                        System.out.println("B服务器已经开启,等待接收数据.....");
                        ds.receive(dp);
                        //取出数据
                        byte[] data = dp.getData();
                        int length = dp.getLength(); //取出数据的实际长度
                        //获取客户端IP
                        String ip = dp.getAddress().getHostAddress();
                        String s = new String(data, 0, length);
                        System.out.println(ip + "-给你发来消息:" + s);
                        if ("886".equals(s)) {
                            break;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        //主线程用来发消息
        sendMsg();
    }

    private static void sendMsg() throws IOException {
        ArrayList<String> iPs = IPUtils.getIPs();
        for (String ip : iPs) {
            System.out.println(ip);
        }
        DatagramSocket ds = new DatagramSocket();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Scanner sc= new Scanner(System.in);
         System.out.println("请输入对方的ip");
         String ip = sc.nextLine();
        while (true) {
            System.out.println("请输入要发送给A的消息");
            String msg = reader.readLine();
            byte[] bytes = msg.getBytes();
            System.out.println(ip);
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(ip), 8888);
            ds.send(dp);
            //自定义结束标记
            if ("886".equals(msg)) {
                break;
            }
        }
        ds.close();
    }
}

获取IP 

public class IPUtils {
    public static ArrayList<String> getIPs() throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("arp /a");
        InputStream in = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"GBK"));
        ArrayList<String> ips = new ArrayList<>();
        while (true){
            String s = reader.readLine();
            if(s==null){
                break;
            }
            if(s.contains("192.168.15")){
                String[] str = s.trim().split("\\s+");
                //System.out.println(str[0]);
                ips.add(str[0]);
            }
        }
        return ips;
    }
}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Geek Li

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

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

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

打赏作者

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

抵扣说明:

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

余额充值