How to solve: Java network communication error: Connection timeout
When performing Java network communication, you often encounter connection timeout errors. Connection timeout means that when establishing a network connection, the handshake process between the client and the server takes longer than the preset time limit. In network communication, connection timeout errors may be caused by multiple factors, such as network delay, slow server response, etc. This article will describe how to resolve connection timeout errors in Java network communications and provide some sample code.
import java.net.*; public class ConnectionTimeoutExample { public static void main(String[] args) { try { URL url = new URL("http://www.example.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置连接超时时间为5秒 conn.setConnectTimeout(5000); int responseCode = conn.getResponseCode(); System.out.println("Response Code: " + responseCode); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
In the above sample code, we set the connection timeout to 5 seconds through the setConnectTimeout()
method. If it takes more than 5 seconds to establish a connection, a connection timeout exception will be thrown.
import org.apache.http.HttpEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ConcurrentRequestsExample { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(10); for (int i = 0; i < 100; i++) { executorService.execute(new RequestTask()); } executorService.shutdown(); } static class RequestTask implements Runnable { public void run() { try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpEntity entity = httpclient.execute(new HttpGet("http://www.example.com")).getEntity(); String result = EntityUtils.toString(entity); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } } }
In the above sample code, we use a thread pool to control the number of concurrent connections to 10. By controlling the number of concurrent connections, the stability of network communication can be improved and the occurrence of connection timeout errors can be reduced.
Summary
In Java network communication, connection timeout errors are a common problem. This article explains how to resolve connection timeout errors in Java network communications and provides some sample code. By checking the network connection, adjusting the connection timeout and controlling the number of concurrent connections, you can effectively solve the problem of connection timeout and improve the stability of network communication. Hope this article helps to solve the connection timeout error.
The above is the detailed content of How to fix: Java Network Communication Error: Connection timed out. For more information, please follow other related articles on the PHP Chinese website!