java网络编程-TCP-多人群聊究极版-创新互联

客户端:
多个客户可以正常收发信息,因为可以同时发送和接受信息,不是发送完信息后等待
返回信息,所以要加入多线程

创新互联服务项目包括五指山网站建设、五指山网站制作、五指山网页制作以及五指山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,五指山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到五指山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
public class Client {

public static void main(String[]args) throws UnknownHostException, IOException
{
    System.out.println("客户端启动中...");
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    System.out.println("请输入用户名:");
    String name=br.readLine();
    Socket client =new Socket("localhost",9999);

    new Thread(new Send(client,name)).start();//在线程还没启动之前,通过构造方法发送名称,先于信息的发送,
    //因为接收信息时,名称已经发送并被读取到变量中

    new Thread(new Receive(client)).start();

}
}

作为释放资源工具类

public class utils {

public static void close(Closeable... target )
{
    for(Closeable it:target)
    {
            try {
                if(null!=it)
                {
                it.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }

    }       
}
}

客户端的发送端封装了:
发送消息
从键盘读取消息
run()
释放资源

public class Send implements Runnable{

private BufferedReader br;
private DataOutputStream dos;
private Socket client;
private boolean flag=true;
private String name;

public Send(Socket client,String name)
{
    this.client=client;
    this.name=name;
    br=new BufferedReader(new InputStreamReader(System.in));
    try {
        dos=new DataOutputStream(client.getOutputStream());
        //发送名称
        send(name);
    } catch (IOException e) {

        this.release();
    }
}
public void run()
{
    while(flag)
    {
        String msg=getstr();
        send(msg);

    }
}
private void release()
{
    this.flag=false;
    utils.close(dos,client);
}
//从控制台获取消息
private String getstr()
{
    try {
        String msg=br.readLine();
        return msg;
    } catch (IOException e) {

        e.printStackTrace();
    }
    return null;
}
private void send(String msg)
{
    try {
        dos.writeUTF(msg);
        dos.flush();

    } catch (IOException e) {
    release();
    }
}
}

客户端的接收端封装了:
接收消息
run()
释放资源

public class Receive implements Runnable {
private DataInputStream dis;
private Socket client;
private boolean flag=true;

public Receive(Socket client)
{
    this.client=client;
    try {
        dis=new DataInputStream(client.getInputStream());
    } catch (IOException e) {
        release();
    }
}

private String receive()
{
    String msg;
    try {
        msg = dis.readUTF();
        return msg;
    } catch (IOException e) {
        release();
    }
    return null;
}
public void run()
{
    while(flag)
    {
        String msg=receive();
        System.out.println(msg);
    }

}
private void release()
{
    this.flag=false;
    utils.close(dis,client);
}
}

在线聊天室
服务器:

public class Chat {
private static CopyOnWriteArrayList list=new CopyOnWriteArrayList<>();//使用并发容器,适合多线程
public static void main(String[]args) throws IOException
{
    System.out.println("服务器启动中...");

    ServerSocket server=new ServerSocket(9999);

    while(true)
    {
        Socket client =server.accept();
        System.out.println("一个客户端建立了连接");
        channel c=new channel(client);
        list.add(c);  //容器管理所有成员
        new Thread(c).start();

    }

}

static class channel implements Runnable{
    private DataInputStream dis;
    private DataOutputStream dos;
    private BufferedReader br;
    private Socket client;
    private boolean flag;
    private String name;

    public channel(Socket client)
    {
        this.client=client;
        try {
            dis=new DataInputStream(client.getInputStream());
            dos=new DataOutputStream(client.getOutputStream());
            flag=true;

            //获取名称
            this.name=receive();
            //欢迎到来
            this.send("欢迎来到聊天室");
            this.sendothers(this.name+"来到了聊天室",true);

        } catch (IOException e) {
            release();          }
    }
    //接收消息
    private String receive() 
    {
        try {
            String msg=dis.readUTF();
            return msg;
        } catch (IOException e) {
            release();
        }
        return null;

    }
    //发送给消息
    private void send(String msg)
    {
        try {
            dos.writeUTF(msg);
            dos.flush();
        } catch (IOException e) {
            release();
        }

    }
    //群聊:获取自己的消息,发给其他人和私聊功能
    private void sendothers(String msg,boolean issys)
    {
        boolean issecrete =msg.startsWith("@");
        if(issecrete)
        {
            int index=msg.indexOf(":");
            String target=msg.substring(1,index);
            msg=msg.substring(index+1);

            for(channel it:list)
            {
                if(it.name.equals(target)) //查找目标
                {
                    it.send(this.name+"悄悄地对你说:"+msg); //私聊消息
                    break;
                }
            }
        }else
        {
            for(channel it:list)
            {
                if(it==this)
                {
                    continue;
                }
                if(!issys)
                {
                    it.send(this.name+":"+msg);
                }else
                {
                    it.send(msg);
                }
            }
        }
    }
    private void release()
    {
        this.flag=false;
        utils.close(dis,dos,client); //写一个工具类,使用Closeable...可变参数    
        //退出时,从容器里面移掉自身
        list.remove(this);
        sendothers("离开了聊天室",false);
    }
    public void run()
    {
        while(flag)
        {
            String msg=receive();
            sendothers(msg,false);
        }
        release();

    }

}
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


新闻名称:java网络编程-TCP-多人群聊究极版-创新互联
文章地址:http://ybzwz.com/article/ceipej.html