一个简单的群聊系统

一个简单的群聊系统,在同一个网段的群聊系统

客户端

————代码的具体实现
public class ClientRunnable implements Runnable {
    private String address = "***.***.***.***";//所在同一个网段的IP
    private int port = 8888;//端口号//客户端和服务器端的端口号必须一致

    public ClientRunnable() {

    }

    public ClientRunnable(String address, int port) {
        this.address = address;
        this.port = port;
    }

    @Override
    public void run() {// 要执行的程序
        // 客户端传送数据用DatagramSocket
        try (DatagramSocket s = new DatagramSocket();
                Scanner sc = new Scanner(System.in)) {
            byte[] buf = null;//定义一个发送数据的字节数组
            String message = null;
            DatagramPacket p = null;
            while (true) {
                message = sc.nextLine();
                if (message.equals("886")) {
                    break;
                }
                buf = message.getBytes();
                p = new DatagramPacket(buf, buf.length, InetAddress.getByName(address), port);
                //发送数据
                s.send(p);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

服务端

————代码的具体实现
public class ServerRunnable implements Runnable {

    public ServerRunnable() {
    }

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

    private int port = 8888;
    @Override
    public void run() {
        try (
                // 定义一个监视指定端口的DatagramSocket类
                DatagramSocket s = new DatagramSocket(port);)
        {

            // 定义一个容器,用来存储接受的数据
            byte[] bytes = new byte[1024 * 64];// udp最大的传送为64k
            DatagramPacket p = new DatagramPacket(bytes, bytes.length);
            while (true) {
                //接受数据
                s.receive(p);
                System.out.println(p.getAddress().getHostAddress()+":"+new String(bytes,0,p.getLength()));
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
自定义一个线程池,让他们处于同一个界面!
public class ChatRoom {
    public static void main(String[] args) {
        ServerRunnable s = new ServerRunnable();
        ExecutersThreadPool.execute(s);
        ClientRunnable c = new ClientRunnable();
        ExecutersThreadPool.execute(c);
    }
}

class ExecutersThreadPool {
    private static ExecutorService threadPool = Executors.newCachedThreadPool();

    public static void execute(Runnable task) {
        if (threadPool.isShutdown()) {
            threadPool = Executors.newCachedThreadPool();
            execute(task);
        }
        threadPool.execute(task);
    }

    public static void shutdown() {
        if (!threadPool.isShutdown()) {
            threadPool.shutdown();
        }
    }
}

//不足:没有做一个界面!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值