zoukankan      html  css  js  c++  java
  • 基于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
    ljm要加油
  • 相关阅读:
    web前端图片上传
    二级联动
    前端框架
    获取URL域名
    监听横屏竖屏
    下载中间件、爬虫中间件
    起始url的调度原理
    自定义代理IP
    爬虫深度控制
    手动处理cookie(实现一个点赞爬虫)
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14299559.html
Copyright © 2011-2022 走看看