JAVA小Demo_手写聊天室_:)

这是一个基于TCP连接的手写聊天室。

​ 约定以@name:为私聊某人,sendToOther方法实现群聊。ChatUtils类为了释放资源。用到的基础知识:TCP Socket编程,多线程,IO等

项目所用到的类
服务器端ChatServer
package com.shun.chathome;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author: JamesLee
 * @Description:
 * @Date: Created in 18:00
 * @Modify By:
 */
public class Channel implements Runnable{
    private Socket client;
    private DataOutputStream dos;
    private DataInputStream dis;
    private boolean isRunning;
    private String name ;
    public Channel(Socket client){
        try {
            this.client=client;
            this.isRunning = true;
            this.dos = new DataOutputStream(client.getOutputStream());
            this.dis = new DataInputStream(client.getInputStream());
            this.name = dis.readUTF();
            this.send("欢迎你加入这个聊天群");
            this.sendToOthers(this.name+"加入聊天群..",true);
        } catch (IOException e) {
            release();
            System.out.println(" ---- Server--Init ----Error-");
        }
    }
    private void sendToOthers(String msg,boolean isSystem){
        if (!isSystem){
            char head = msg.charAt(0);
            if (head!='@') {
                for (Channel c : ChatServer.all) {
                    if (!c.equals(this)) {
                        c.send(this.name+"说:"+msg);
                    }
                }
            }else{
                int index = msg.indexOf(":");
                String name = msg.substring(1,index);
                for (Channel c : ChatServer.all) {
                    if (c!=this){
                        if (c.name.equals(name)){
                            sendToOthers(this.name+"偷偷对你说:"+msg,false);
                        }
                    }
                }
            }
        }else{
            for(Channel c : ChatServer.all){
                if (!c.equals(this)){
                    c.send("系统提示:"+msg);
                }
            }
        }
    }
    @Override
    public void run() {
        while (isRunning) {
            String msg = receive();
            sendToOthers(msg,false);
        }
    }

    public void send(String msg){
        if (msg.length()>0)
        try {
            dos.writeUTF(msg);
            dos.flush();
            System.out.println(msg);
        } catch (IOException e) {
            release();
            System.out.println(" ---- Server--Send--Msg----Error-");
        }
    }

    public String receive(){
        String msg = "";
        try {
            msg=dis.readUTF();
            System.out.println(this.name+":"+msg);
        } catch (IOException e) {
            release();
            System.out.println("---Server--Read--Msg--Error!---");
        }
        return msg;
    }

    public void release(){
        isRunning = false;
        ChatServer.all.remove(this);
        ChatUtils.close(dis,dos,client);
    }
}

客户端ChatClient
package com.shun.chathome;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author: JamesLee
 * @Description:
 * @Date: Created in 17:07
 * @Modify By:
 */
public class ChatClient {
    public static void main(String[] args) throws IOException {
        Socket client = new Socket("localhost",8888);
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入您的姓名:");
        String name = sc.nextLine();
        System.out.println("---------client-----");
        new Thread(new Send(client,name)).start();
        new Thread(new Receive(client)).start();
    }
}

工具类 ChatUtils
package com.shun.chathome;

import java.io.Closeable;
import java.io.IOException;

/**
 * @Author: JamesLee
 * @Description:
 * @Date: Created in 17:57 2020
 * @Modify By:
 */
public class ChatUtils {
    public static  void close(Closeable ... targets){
        for (Closeable target:targets){
            try {
                if (null!=target){
                    target.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

连接类Channel
package com.shun.chathome;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author: JamesLee
 * @Description:
 * @Date: Created in 18:00
 * @Modify By:
 */
public class Channel implements Runnable{
    private Socket client;
    private DataOutputStream dos;
    private DataInputStream dis;
    private boolean isRunning;
    private String name ;
    public Channel(Socket client){
        try {
            this.client=client;
            this.isRunning = true;
            this.dos = new DataOutputStream(client.getOutputStream());
            this.dis = new DataInputStream(client.getInputStream());
            this.name = dis.readUTF();
            this.send("欢迎你加入这个聊天群");
            this.sendToOthers(this.name+"加入聊天群..",true);
        } catch (IOException e) {
            release();
            System.out.println(" ---- Server--Init ----Error-");
        }
    }
    private void sendToOthers(String msg,boolean isSystem){
        if (!isSystem){
            char head = msg.charAt(0);
            if (head!='@') {
                for (Channel c : ChatServer.all) {
                    if (!c.equals(this)) {
                        c.send(this.name+"说:"+msg);
                    }
                }
            }else{
                int index = msg.indexOf(":");
                String name = msg.substring(1,index);
                for (Channel c : ChatServer.all) {
                    if (c!=this){
                        if (c.name.equals(name)){
                            c.send(c.name+"偷偷对你说:"+msg.substring(index+1));
                        }
                    }
                }
            }
        }else{
            for(Channel c : ChatServer.all){
                if (!c.equals(this)){
                    c.send("系统提示:"+msg);
                }
            }
        }
    }
    @Override
    public void run() {
        while (isRunning) {
            String msg = receive();
            sendToOthers(msg,false);
        }
    }

    public void send(String msg){
        if (msg.length()>0)
        try {
            dos.writeUTF(msg);
            dos.flush();
            System.out.println(msg);
        } catch (IOException e) {
            release();
            System.out.println(" ---- Server--Send--Msg----Error-");
        }
    }

    public String receive(){
        String msg = "";
        try {
            msg=dis.readUTF();
            System.out.println(this.name+":"+msg);
        } catch (IOException e) {
            release();
            System.out.println("---Server--Read--Msg--Error!---");
        }
        return msg;
    }

    public void release(){
        isRunning = false;
        ChatServer.all.remove(this);
        ChatUtils.close(dis,dos,client);
    }
}

客户端接收 Receive
package com.shun.chathome;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author: JamesLee
 * @Description:
 * @Date: Created in 18:17 2020
 * @Modify By:
 */
public class Receive implements Runnable{
    private boolean isRunning;
    private DataInputStream dis;
    private Socket client;
    public Receive(Socket client){
        try {
            this.client = client;
            this.dis  = new DataInputStream(client.getInputStream());
            this.isRunning = true;
        } catch (IOException e) {
            release();
            System.out.println("---客户端接收端初始化失败----");
        }
    }
    @Override
    public void run() {
        while (isRunning){
            receive();
        }
    }
    private void receive(){
        try {
            System.out.println(dis.readUTF());
        } catch (IOException e) {
            release();
            System.out.println("-------客户端接收错误-------");
        }
    }
    private void release(){
        isRunning = false;
        ChatUtils.close(dis,client);
    }
}

客户端发送Send
package com.shun.chathome;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @Author: JamesLee
 * @Description:
 * @Date: Created in 18:17
 * @Modify By:
 */
public class Send implements Runnable{
    private DataOutputStream dos;
    private BufferedReader console;
    private Socket client;
    boolean isRunning = true;
    public Send(Socket client,String name){
        try {
            this.client = client;
            dos = new DataOutputStream(client.getOutputStream());
            send(name);
        } catch (IOException e) {
            release();
            System.out.println("----客户端发送初始化出错---");
        }
    }
    public void send(String msg){
        try {
            dos.writeUTF(msg);
            dos.flush();
        } catch (IOException e) {
            release();
            System.out.println("---客户端发送出现错误---");
        }
    }
    @Override
    public void run() {
        while (isRunning){
            send(getMsgFromConsole());
        }
    }
    private String getMsgFromConsole(){
        console = new BufferedReader(new InputStreamReader(System.in));
        String msg  = "";
        try {
            msg = console.readLine();
        } catch (IOException e) {
            release();
            System.out.println("---发送端控制台读取出现错误---");
        }
        return msg;
    }
    private void release(){
        isRunning=false;
        ChatUtils.close(dos,client);
    }

}

运行结果
假装这里有图,图片挂了,结果的话很简单,如果你是初学者,你只需要在同一个包内定义这六个类,然后将代码复制进去,先运行ChatServer,再运行Client(可以运行多个),然后可以在控制台里输入输出啦。

结语:

​ 不积跬步无以至千里,不积小流无以成江海

​ 望与诸君共同进步

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您提供的配置文件,这是一个用于姿态估计的pose_demo的配置示例。该配置文件包括了处理器配置(processor_cfg)和命令行参数配置(argparse_cfg)。 处理器配置包括以下内容: - type:指定处理器类型为"processor.pose_demo.inference",这可能是一个自定义的处理器类型。 - gpus:指定使用的GPU数量为1。 - worker_per_gpu:指定每个GPU的worker数量为1。 - video_file:指定输入视频的路径为"resource/data_example/skateboarding.mp4"。 - save_dir:指定结果保存的目录路径为"work_dir/pose_demo"。 检测配置(detection_cfg)包括以下内容: - model_cfg:指定检测模型的配置文件路径为"configs/mmdet/cascade_rcnn_r50_fpn_1x.py"。 - checkpoint_file:指定检测模型的checkpoint路径为"mmskeleton://mmdet/cascade_rcnn_r50_fpn_20e"。 - bbox_thre:指定检测目标的边界框阈值为0.8。 估计配置(estimation_cfg)包括以下内容: - model_cfg:指定姿态估计模型的配置文件路径为"configs/pose_estimation/hrnet/pose_hrnet_w32_256x192_test.yaml"。 - checkpoint_file:指定姿态估计模型的checkpoint路径为"mmskeleton://pose_estimation/pose_hrnet_w32_256x192"。 - data_cfg:指定姿态估计模型的数据配置,包括图像尺寸、像素标准化值、图像均值和标准差以及后处理选项。 命令行参数配置(argparse_cfg)包括了一些命令行参数的绑定,用于从命令行传递参数给处理器配置。 例如,您可以使用以下命令行参数来运行pose_demo: ``` python pose_demo.py --gpus 1 --video resource/data_example/skateboarding.mp4 --worker_per_gpu 1 --skeleton_model configs/pose_estimation/hrnet/pose_hrnet_w32_256x192_test.yaml --skeleton_checkpoint mmskeleton://pose_estimation/pose_hrnet_w32_256x192 --detection_model configs/mmdet/cascade_rcnn_r50_fpn_1x.py --detection_checkpoint mmskeleton://mmdet/cascade_rcnn_r50_fpn_20e ``` 请注意,以上仅为示例,具体的使用方式和命令行参数可能会有所不同,具体取决于实际情况。 如果您有进一步的问题,请提供更多细节,我将尽力帮助您。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值