基于Socket的局域网聊天室

  • Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TestSockClient {
    public static void main(String[] args) {
        DataOutputStream dos = null;
        Socket socket = null;
        Scanner scan = null;
        String  sendStr;
        try {
            socket = new Socket("localhost", 8888);
            final DataInputStream dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream( socket.getOutputStream());
            scan = new Scanner(System.in);
          //匿名内部类
            new Thread(){
                String receiveStr;
                public void run() {
                    try {
                        while ((receiveStr = dis.readUTF()) != null) {
                            System.out.println(receiveStr);
                        }
                    } catch (Exception e) {
                        System.out.println("客户端关闭!");
                    }finally{
                        try{
                            dis.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            do {
                sendStr = scan.nextLine();
                dos.writeUTF(sendStr);
            } while (!sendStr.equals("88"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {//最后一个要写Exception
            e.printStackTrace();
        }
        finally {
            try {
                dos.close();
                socket.close();
                scan.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class TestSockServer {
    static List<ClientService> cslist = new ArrayList<>();
    public static void main(String[] args) {
        ServerSocket server = null;
        Socket socket = null;

        try{
            server = new ServerSocket(8888);
            System.out.println("服务器已开启");
            while(true){
                socket = server.accept();
                ClientService cs = new TestSockServer().new ClientService(socket);
                cslist.add(cs);
                cs.start();
            }
        } catch(IOException e){
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                socket.close();
                server.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    class ClientService extends Thread{
        Socket socket;
        public ClientService(Socket socket) {
            this.socket = socket;
            this.setName(socket.getPort()+"");
        }
        @Override
        public void run() {
            DataOutputStream dos = null;
            DataInputStream dis = null;
            try{
                dos = new DataOutputStream(socket.getOutputStream());
                dis = new DataInputStream(socket.getInputStream());
                String receiveStr ;
                while((receiveStr = dis.readUTF())!= null) {
                    if(receiveStr.substring(0,2).equals("##")){
                        this.setName(receiveStr.substring(2));
                    }
                    System.out.println(this.getName()+"发送:"+receiveStr);
                    //分发信息
                    for(ClientService cs : cslist){
                        new DataOutputStream(cs.socket.getOutputStream()).writeUTF(this.getName()+"客户端发送:"+receiveStr);
                    }
                }
            }catch(EOFException e) {
                System.out.println(this.getName()+"客户端结束服务");
                //删除退出的客户端
                cslist.remove(this);
            }catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try{
                    dos.close();
                    dis.close();
                    socket.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
  • Result
    • Client1
      image.png
    • Client2
      image.png
    • Server
      image.png
posted @ 2021-01-19 19:24  0x3fffffff  阅读(177)  评论(0编辑  收藏  举报