java http服务器实现和JMEter测试

eagi6jfj  于 2023-01-20  发布在  Java
关注(0)|答案(1)|浏览(127)

控制到达put,get,post请求但是我不能从服务器得到回复,http://localhost:8080抛出无效ip错误并且jmeter测试用例显示错误状态
我已按如下方式配置了一台服务器:公共类HttpServer {

private static final int MAX_THREADS = 64;

private static final Map<Integer, AtomicInteger> threadCounts = new ConcurrentHashMap<>();

private static final ExecutorService threadPool = Executors.newFixedThreadPool(MAX_THREADS);


public static void main(String[] args) {


    ArrayList<Integer> portNodes;

    ArrayList<Integer> timeout;


    XMLFileHandler xmlFileHandler = new XMLFileHandler();

    xmlFileHandler.readFromXML("config.xml");

    portNodes = xmlFileHandler.getPortNodes();

    timeout = xmlFileHandler.getTimeout();


    for (int i = 0; i < portNodes.size(); i++) {

        int port = portNodes.get(i);

        int timeouts = timeout.get(i);

                  if (!threadCounts.containsKey(port)) {

                             threadCounts.put(port, new AtomicInteger(0));

                  }         

                  try {

                             ServerSocket serverSocket = new ServerSocket(port);

                             serverSocket.setSoTimeout(timeouts);

                             threadPool.submit(() -> {

                                        while (true) {

                                                   try {

                                                              Socket socket = serverSocket.accept();

                                                              OutputStream os = socket.getOutputStream();

                                                              os.write("welcome to server".getBytes());

                                                              System.out.println("connected successfully" + port);

                                                              threadCounts.get(port).incrementAndGet();

                                                              threadPool.submit(new RequestHandler(socket));

                                                   }

                                                   catch (SocketTimeoutException e) {

                                                              System.out.println("Timeout occurred on port: " + port);

                                                   }

                                                   catch (IOException e) {

                                                              e.printStackTrace();

                                                   }

                                        }

                             });

                             }

                  catch (BindException e) {

                System.out.println("Port " + port + " is already in use. Please choose a different port.");

            }catch(IOException e) {

                  System.out.println("erro"+port);

            }

    }

}

私有静态类RequestHandler实现Runnable {

private final Socket socket;


public RequestHandler(Socket socket) {

    this.socket = socket;

}


public void run() {

    try {

       System.out.println("in request handler");

        // Handle HTTP request

        InputStream input = socket.getInputStream();

        OutputStream output = socket.getOutputStream();


        // Parse the request

        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        String[] requestLine = reader.readLine().split(" ");

        String method = requestLine[0];

        String url = requestLine[1];

        String httpVersion = requestLine[2];


        // Handle GET request

                        if (method.equals("GET")) {

            //TODO: Implement handling of GET request

        }

        // Handle POST request

        else if (method.equals("POST")) {

            //TODO: Implement handling of POST request

        }

        // Handle PUT request

        else if (method.equals("PUT")) {

            //TODO: Implement handling of PUT request

        }

        else {

            // Send error message for unsupported method

            output.write("HTTP/1.1 400 Bad Request\r\n\r\n".getBytes());

        }


        // Close socket and release resources

        input.close();

        output.close();

        socket.close();

        threadCounts.get(socket.getPort()).decrementAndGet();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

}
}

zkure5ic

zkure5ic1#

您的“http服务器”没有实现HTTP protocol,因此您将无法使用HTTP请求采样器进行加载。
考虑迁移到TCP采样器或HTTP Raw Request采样器(可使用JMeter Plugins Manager安装)

相关问题