java.net.SocketException 连接超时错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:40:08  来源:igfitidea点击:

java.net.SocketException Connection timed out error

javasockets

提问by jgg

I am getting below error when I am trying to connect to a TCP server. My programs tries to open around 300-400 connections using diffferent threads and this is happening during 250th thread. Each thread uses its own connection to send and receive data.

当我尝试连接到 TCP 服务器时出现以下错误。我的程序尝试使用不同的线程打开大约 300-400 个连接,这发生在第 250 个线程期间。每个线程使用自己的连接来发送和接收数据。

java.net.SocketException: Connection timed out:could be due to invalid address
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:385)

Here is the code I have that a thread uses to get socket:

这是我有一个线程用来获取套接字的代码:

socket = new Socket(my_hostName, my_port);

Is there any default limit on number of connections that a TCP server can have at one time? If not how to solve this type of problems?

TCP 服务器一次可以拥有的连接数是否有任何默认限制?如果不是如何解决这类问题?

回答by Chochos

You could be getting a connection timeout if the server has a ServerSocket bound to the port you are connecting to, but is not accepting the connection.

如果服务器将 ServerSocket 绑定到您要连接的端口,但不接受连接,则可能会出现连接超时。

If it always happens with the 250th connection, maybe the server is set up to only accept 250 connections. Someone has to disconnect so you can connect. Or you can increase the timeout; instead of creating the socket like that, create the socket with the empty constructor and then use the connect() method:

如果第 250 个连接总是发生这种情况,则可能服务器设置为仅接受 250 个连接。必须有人断开连接才能连接。或者你可以增加超时时间;不是像那样创建套接字,而是使用空构造函数创建套接字,然后使用 connect() 方法:

Socket s = new Socket(); s.connect(new InetSocketAddress(my_hostName, my_port), 90000);

Socket s = new Socket(); s.connect(new InetSocketAddress(my_hostName, my_port), 90000);

Default connection timeout is 30 seconds; the code above waits 90 seconds to connect, then throws the exception if the connection cannot be established.

默认连接超时为 30 秒;上面的代码等待 90 秒连接,然后如果无法建立连接则抛出异常。

You could also set a lower connection timeout and do something else when you catch that exception...

您还可以设置较低的连接超时并在捕获该异常时执行其他操作...

回答by user207421

Why all the connections? Is this a test program? In which case be aware that opening large numbers of connections from a single client stresses the client in ways that aren't exercised by real systems with large numbers of different client hosts, so test results from that kind of client aren't all that valid. You could be running out of client ports, or some other client resource.

为什么所有的连接?这是一个测试程序吗?在这种情况下,请注意从单个客户端打开大量连接会给客户端带来压力,而真实系统并没有使用大量不同的客户端主机,因此这种客户端的测试结果并不是那么有效. 您可能会耗尽客户端端口或其他一些客户端资源。

If it isn't a test program, same question. Why all the connections? You'd be better off running a connection pool and reusing a much smaller number of connections serially. The network only has so much bandwidth after all; dividing it by 400 isn't very useful.

如果它不是测试程序,同样的问题。为什么所有的连接?你最好运行一个连接池并连续重用更少的连接。毕竟网络只有这么多带宽;除以 400 不是很有用。