开发者社区> 问答> 正文

关于java并发编程

蛮大人123 2016-06-04 22:12:28 1029

谁能给个完整的 、有关java 并发编程的例子?多个客户端向服务器传输数据

Java
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:27:21
    //类receiveServer
    import java.io.*;
    import java.util.*;
    import java.net.*;
    
    public class recieveServer {
        final int RECEIVE_PORT = 9090; // 该服务器的端口号
    
        // receiveServer的构造器
        public recieveServer() {
            ServerSocket rServer = null; // ServerSocket的实例
            Socket request = null; // 用户请求的套接字
            Thread receiveThread = null;
            try {
                rServer = new ServerSocket(RECEIVE_PORT); // 初始化ServerSocket
                System.out.println("Welcome to the server!");
                System.out.println(new Date());
                System.out.println("The server is ready!");
                System.out.println("Port: " + RECEIVE_PORT);
                while (true) { // 等待用户请求
                    request = rServer.accept(); // 接收客户机连接请求
                    receiveThread = new serverThread(request); // 生成serverThread的实例
                    receiveThread.start();// 启动serverThread线程
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    
        public static void main(String args[]) {
            new recieveServer();
        } // end of main
    
    } // end of class
    
    //类serverThread
    class serverThread extends Thread {
    
        Socket clientRequest; // 用户连接的通信套接字
        BufferedReader input; // 输入流
        PrintWriter output; // 输出流
    
        public serverThread(Socket s) { // serverThread的构造器
            this.clientRequest = s; // 接收receiveServer传来的套接字
            InputStreamReader reader;
            OutputStreamWriter writer;
            try { // 初始化输入、输出流
                reader = new InputStreamReader(clientRequest.getInputStream());
                writer = new OutputStreamWriter(clientRequest.getOutputStream());
                input = new BufferedReader(reader);
                output = new PrintWriter(writer, true);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            output.println("Welcome to the server!");
            // 客户机连接欢迎词
            output.println("Now is:" + new java.util.Date() + " " + "Port:"
                    + clientRequest.getLocalPort());
            output.println("What can I do for you?");
        }
    
        public void run() { // 线程的执行方法
            String command = null; // 用户指令
            String str = null;
            boolean done = false;
    
            while (!done) {
                try {
                    str = input.readLine(); // 接收客户机指令
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
                command = str.trim().toUpperCase();
                if (str == null || command.equals("QUIT")) // 命令quit结束本次连接
                    done = true;
                else if (command.equals("HELP")) { // 命令help查询本服务器可接受的命令
                    output.println("query");
                    output.println("quit");
                    output.println("help");
                } else if (command.startsWith("QUERY")) { // 命令query
                    output.println("OK to query something!");
                }
                // else if …….. //在此可加入服务器的其他指令
                else if (!command.startsWith("HELP") && !command.startsWith("QUIT")
                        && !command.startsWith("QUERY")) {
                    output.println("Command not Found!Please refer to the HELP!");
                }
            }// end of while
            try {
                clientRequest.close(); // 关闭套接字
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            command = null;
        }// end of run
    }
    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题