A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 四叶草~ 中级黑马   /  2015-7-9 23:27  /  750 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

前几天写了个小程序,,模拟一个聊天室,,但是只开了两个线程。

/**
* 多线程聊天室
*/
public class ChatRoom {
        public static void main(String[] args) {
                SendThread send = new SendThread(null,1125);
                ReceiveThread receive = new ReceiveThread(1125);
                Thread t1 = new Thread(send);
                Thread t2 = new Thread(receive);
                t1.start();
                t2.start();
        }
}
--------------------------------------------------------------------------------------------
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
* 接受数据
*/
public class ReceiveThread implements Runnable {
        private int port;

        public ReceiveThread(int port) {
                this.port = port;
        }

        @Override
        public void run() {
                DatagramSocket ds = null;
                try {
                        ds = new DatagramSocket(port);
                        while (true) {
                                byte[] bys = new byte[1024];
                                DatagramPacket dp = new DatagramPacket(bys, bys.length);
                                ds.receive(dp);
                                String hostName = dp.getAddress().getHostName();
                                String s = new String(dp.getData(), 0, dp.getLength());
                                System.out.println("from: " + hostName + "\t" + "data: " + s);
                        }
                       
                } catch (SocketException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
}
-----------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
* 发送数据线程
*
*/
public class SendThread implements Runnable {
        /**
         * 接收端的主机ip号
         */
        private String ip;
        /**
         * 端口号
         */
        private int port;

        public SendThread(String ip, int port) {
                this.ip = ip;
                this.port = port;
        }

        @Override
        public void run() {
                DatagramSocket ds = null;
                DatagramPacket dp = null;
                // IP地址对象
                InetAddress address = null;
                try {
                        // 初始化IP地址对象
                        if (ip == null) {
                                address = InetAddress.getLocalHost();
                        } else {
                                address = InetAddress.getByName(ip);
                        }
                        ds = new DatagramSocket();
                        BufferedReader br = new BufferedReader(new InputStreamReader(
                                        System.in));
                        System.out.println("请输入聊天内容:");
                        while (true) {
                                String line;
                                line = br.readLine();
                                if (line.equals("over")) {
                                        System.out.println("聊天结束。");
                                        break;
                                }
                                // DatagramPacket(byte[] buf, int length, InetAddress address,
                                // int port)
                                dp = new DatagramPacket(line.getBytes(), line.getBytes().length,
                                                address, port);
                                ds.send(dp);
                        }
                        br.close();
                } catch (SocketException | UnknownHostException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } finally {
                        ds.close();
                }
        }
}




评分

参与人数 4黑马币 +36 收起 理由
牛奋 + 10 很给力!
wx_J25HxI5W + 6 很给力!
小朱 + 10 赞一个!
宸宸 + 10 赞一个!

查看全部评分

7 个回复

倒序浏览
大神,带我飞啊,头都看晕了1
回复 使用道具 举报
妹子有才 啊
回复 使用道具 举报
大神,带我飞,模拟聊天室,好高端啊,能加我一起聊吗?
回复 使用道具 举报
技术大咖  哪里有 ?   
回复 使用道具 举报
么看懂写的什么,
回复 使用道具 举报
点个赞!
回复 使用道具 举报
nanfp 中级黑马 2015-7-10 08:48:34
8#
赞一个!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马