java-网络通信--socket实现多人聊天(基于命令行)

java-网络通信--socket实现多人聊天(基于命令行)_第1张图片

 

 

先编写最简答的服务器

思路

1编写一个实现Runnable接口的静态内部类 ServerC,便于区分每个客户端
   1.1 获取客户端数据函数   public String remsg()
   1.2 转发消息给所有客户端(自己除外) public void allsend(String data) 
   1.3  释放资源  public  void release()
    1.4编写接口重写方法 public void run(),调用收消息和发消息方法。

2server主函数监听客户端的请求
    每次的客户端请求都将对应的 ServerC 保存到一个集合当中
    最后开启一个线程单独对应
package chat;



import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CopyOnWriteArrayList;


public class ServerChat {
    private static  CopyOnWriteArrayList all = new CopyOnWriteArrayList<>();

    public static void main(String[] args) throws IOException {
        System.out.println("##################Server##############");
        ServerSocket server = new ServerSocket(8888);
        while (true){
            Socket socket = server.accept();
            System.out.println("进入");
            ServerC serverC = new ServerC(socket);
            all.add(serverC);
            //使用多线程分离各个客户端
            new Thread(serverC).start();
        }
    }

    static class ServerC implements Runnable{
        private  Socket socket;
        private DataOutputStream dos;
        private DataInputStream dis;

        public ServerC(Socket socket) {
            this.socket = socket;
            try {
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                release();
            }
        }

        /**
         * 获取客户端data
         * @return
         */
        public String remsg(){
            //接受消息
            String data = null;
            try {
                data = dis.readUTF();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return data;

        }
        //消息转发给所有人函数
        public void transaction(String data){
            try{
                dos.writeUTF(data);
                dos.flush();
            }catch (Exception e){
                release();
            }
        }

        //群发给客户端
        public void allsend(String data) {
            for (ServerC serverC : all) {
                if (this == serverC) {
                    continue;
                } else {
                    serverC.transaction(data);
                }


            }
        }

        //释放资源函数
        public  void release(){
            try {
                dos.close();
                dis.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void run() {
            String data = remsg();
            allsend(data);
            }
        }


}

 

 

 

编写客户端的收消息类:

package chat;

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

public class ClientResevice implements Runnable{
    private Socket socket;
    private DataInputStream dis;

    public ClientResevice(Socket socket) {
        this.socket = socket;
        try {
            this.dis =  new DataInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            release();
        }
    }

    @Override
    public void run() {
        while (true){
            //接受消息
            String msg = receive();
            if(!msg.isEmpty()){
                System.out.println(msg);
            }
        }
    }

    /**
     * 接受消息函数
     * @return返回消息
     */
    public String receive(){
        String msg = null;
        try {
            msg = dis.readUTF();
        } catch (IOException e) {
            e.printStackTrace();
            release();
        }
        return msg;
    }

    //释放资源函数
    public  void release(){
        try {
            dis.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

客户端发消息类:

package chat;

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


/**
 * 创建发送消息线程类,发送和接受分开
 */
public class ClientSend implements Runnable{
    private BufferedReader console;
    private DataOutputStream dos;
    private Socket socket;

    /**
     * 建立发送消息线程类的构造函数
     * @param socket
     */
    public ClientSend( Socket socket){
         this.socket = socket;
         this.console =    new BufferedReader(new InputStreamReader(System.in));
        try {
            this.dos = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            this.release();
        }
    }

    /**
     * 实现Runnable里面的run方法
     */
    @Override
    public void run() {
        while (true){
            send();
        }
    }

    /**
     *获取命令函输入函数
     * @return
     */
    public String conin(){
        String msg = null;
        try {
            msg = console.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return msg;
    }


    /**
     * 发送函数
     */
    public void send(){
        String msg = conin();
        if(!msg.isEmpty()){
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    //释放资源函数
    public  void release(){
        try {
            dos.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

客户端代码,主要怎么使用发收消息类:

package chat;

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

public class FirstClient {

    public static void main(String[] args) throws IOException {
        System.out.println("##############client################");
        Socket socket = new Socket("127.0.0.1",8888);
//      启动发送消息线程
        new Thread(new ClientSend(socket)).start();
//      启动接受线程
        new Thread(new ClientResevice(socket)).start();
        }
    }

 

你可能感兴趣的:(java-网络通信--socket实现多人聊天(基于命令行))