不登高山,不知天之高也。不临深渊,不知地之厚也。

 Java中的封装类(Socket),在实现这个功能的时候,需要对Java中的Socket套接字的一些用法熟悉,服务器与客户端之间主要通过的Java中的IO流通信。需要理解IO流的流出,流入问题。

 接下来,之间看代码了,在客户端加入了多线程操作。自己定义了一多线程的工厂。

 服务器

public class Server1 extends Socket {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(5000);//开启服务器,端口为5000.个人服务器建议4位数,防止占用其他端口
        System.out.println("服务器已启动,等待连接");
        //自己创建一个线程工厂,以下是各种参数
        // corePoolSize 核心线程数
        // maxPoolSize 最大线程数 核心线程数+救急线程数<=最大线程数
        // keepAliveTime 保持时间  如果一个线程闲暇的时间超过了保持时间,那就把它回收,但不会少于核心线程数
        // timeUnit 时间单位
        // BlockingQueue 阻塞队列 当任务数超过核心线程数后,就把任务放入阻塞队列排队运行 (有界,无界)
//        ExecutorService service = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS,
//                new ArrayBlockingQueue<>(10));
        ExecutorService service = new ThreadPoolExecutor(10, 10, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());
        while (true){
            Socket accept = serverSocket.accept();//等待客户端的连接
            System.out.println("已连接……");
            service.submit(()->{//lambda 表达式
            // 把io相关的操作放在线程内执行,让每个线程处理一个io操作,避免io阻塞
                try {
                    getStream(accept);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
    private static void getStream(Socket accept) throws IOException {
        InputStream in = accept.getInputStream();//得到客户端的字节流
        OutputStream out = accept.getOutputStream();//要给客户的字节流
        while (true){

            byte[] bytes = new byte[1024];
            int len = in.read(bytes);
            if(len == -1){

                break;
            }

            String ins = new String(bytes, 0, len, "utf-8");
            System.out.println(ins);
            out.write(("服务器回答"+ins).getBytes("utf-8"));//返回给客户端

        }
    }
}

 接下来,看客户端的代码,客户端实现的是,可以在客户端写入数据,实时的显示在服务器端。

多个客户对应一个客户端。

客户端

 

public class Client extends Socket {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 5000);//连接本地服务器
        InputStream in = socket.getInputStream();//得到服务器传过来的字节流
        OutputStream out = socket.getOutputStream();//要给服务器传的字节流

        out.write("Helli".getBytes());//给服务器一个Hello
        new Thread(new Runnable() {
            @Override
            public void run() {
                Scanner scanner = new Scanner(System.in);
                if (scanner.hasNextLine()){//判断是否有下一行
                    String line = scanner.nextLine();//有就继续输出给服务端
                    try {
                        out.write(line.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        while (true){

            byte[] bytes = new byte[1024];
            int len = in.read(bytes);
            if(len == -1){
                break;
            }
            //读取服务器的字节流
            String result = new String(bytes,0,len,"utf-8");
            //返回给服务器
            out.write(("Hello"+result).getBytes("utf-8"));
        }
    }
}