资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java网络编程-TCP-多人聊天(面向对象封装)

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

站在用户的角度思考问题,与客户深入沟通,找到息县网站设计与息县网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、申请域名、网页空间、企业邮箱。业务覆盖息县地区。

 public class Client {

public static void main(String[]args) throws UnknownHostException, IOException
{
    System.out.println("客户端启动中...");

    Socket client =new Socket("localhost",9999);

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

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

}
}

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

 public class Send implements Runnable{

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

public Send(Socket client)
{
    this.client=client;
    br=new BufferedReader(new InputStreamReader(System.in));
    try {
        dos=new DataOutputStream(client.getOutputStream());
    } catch (IOException e) {

        this.release();
    }
}
public void run()
{
    while(flag)
    {
        System.out.println("请输入消息:");
        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 utils {

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

                e.printStackTrace();
            }

    }       
}
}

在线聊天室
服务器

 public class Chat {

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("一个客户端建立了连接");
        new Thread(new channel(client)).start();

    }

}

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

    public channel(Socket client)
    {
        this.client=client;
        try {
            dis=new DataInputStream(client.getInputStream());
            dos=new DataOutputStream(client.getOutputStream());
            flag=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 release()
    {
        this.flag=false;
        utils.close(dis,dos,client); //写一个工具类,使用Closeable...可变参数    
    }
    public void run()
    {
        while(flag)
        {
            String msg=receive();
            send(msg);
        }
        release();

    }

}
}

当前标题:java网络编程-TCP-多人聊天(面向对象封装)
路径分享:http://cdkjz.cn/article/gohsei.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220