Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1818

how to fix: java.net.SocketTimeoutException: Connect timed out [duplicate]

$
0
0

i have server and client use https.what happend with my code, i try to connect from client to server. but it fail. this is my code, when i use cmd, i can ping from client to server. IP-Client: 192.168.100.248, IP-Server:192.168.100.95. port: 8443.

Client:

 private static void disableSSLVerification() throws Exception {    TrustManager[] trustAllCerts = new TrustManager[]{        new X509TrustManager() {            public X509Certificate[] getAcceptedIssuers() {                return null;            }            public void checkClientTrusted(X509Certificate[] certs, String authType) {            }            public void checkServerTrusted(X509Certificate[] certs, String authType) {            }        }    };    SSLContext sc = SSLContext.getInstance("TLS");    sc.init(null, trustAllCerts, new java.security.SecureRandom());    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());}private static void sendClientIp() {try {    disableSSLVerification();    // Lấy địa chỉ IP của client    String clientIp = InetAddress.getLocalHost().getHostAddress();    System.out.println("Client IP: " + clientIp);    // Tạo kết nối đến server    URL url = new URL("https://192.168.00.95:8443/clientip");    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();    connection.setRequestMethod("POST"); // Sử dụng phương thức POST để gửi dữ liệu    connection.setDoOutput(true);    connection.setHostnameVerifier((hostname, session) -> true);    connection.setConnectTimeout(10000); // 5 giây timeout kết nối    connection.setReadTimeout(10000);// Chấp nhận tất cả chứng chỉ SSL    // Gửi địa chỉ IP đến server    try (OutputStream os = connection.getOutputStream()) {        os.write(clientIp.getBytes());        os.flush();    }    // Nhận phản hồi từ server    if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {        System.out.println("Địa chỉ IP đãđược gửi thành công.");    } else {        System.out.println("Lỗi: " + connection.getResponseCode());    }} catch (Exception e) {    e.printStackTrace();}

server:

static class ClientIpHandler implements HttpHandler {    public void handle(HttpExchange exchange) {        try {            if ("POST".equalsIgnoreCase(exchange.getRequestMethod())) {                // Đọc địa chỉ IP từ yêu cầu                InputStream is = exchange.getRequestBody();                String clientIp = new BufferedReader(new InputStreamReader(is))                                    .lines().collect(Collectors.joining("\n"));                System.out.println("Nhận địa chỉ IP từ client: " + clientIp);                // Bạn có thể lưu trữ hoặc xử lýđịa chỉ IP này ởđây                // ...                MenuItem clientIpItem = new MenuItem(clientIp);                clientIpItem.addActionListener(e -> {                if (!clientIpItem.getLabel().equals(""+clientIp+"(Đang chạy)")){                    clientIpItem.setLabel(""+clientIp+"(Đang chạy)");                    startClient(); // Gọi phương thức để khởi động client                } else {                    clientIpItem.setLabel(clientIp);                    stopClient();// Đổi tên lại thành client IP                }                });                clientIpItems.put(clientIp, clientIpItem); // Lưu item vào map                popupMenu.add(clientIpItem); // Thêm item vào men                // Phản hồi cho client                String response = "Client ID received: " + clientIp;                exchange.sendResponseHeaders(200, response.length());                OutputStream responseBody = exchange.getResponseBody();                responseBody.write(response.getBytes());                responseBody.flush();            } else {                String errorMessage = "Phương thức không được hỗ trợ.";                exchange.sendResponseHeaders(405, errorMessage.length());                OutputStream responseBody = exchange.getResponseBody();                responseBody.write(errorMessage.getBytes());                responseBody.flush();            }        } catch (Exception e) {            e.printStackTrace();        } finally {            exchange.close();        }    }}

i use:telnet 192.168.100.95 8443It work, but this code is not. pls help me


Viewing all articles
Browse latest Browse all 1818

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>