gpt4 book ai didi

java - java.net.SocketTimeoutException : Connect timed out 的奇怪情况

转载 作者:行者123 更新时间:2023-12-01 15:45:07 26 4
gpt4 key购买 nike

你好,我一时无法理解。以下代码在一个简单的应用程序中完美实现:

     /**
*
* @author AKhusnutdinov
*/
public class JavaApplication12 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Date startDate = new Date();
String hostname = "ihtik.lib.ru";
int port = 80;

Socket socket = null;
BufferedReader reader = null;

try {
socket = new Socket();
socket.setSoTimeout(30000);
socket.connect(new InetSocketAddress(hostname, port),
30000);
String writer = "GET /2011.06.03_prislan.ihtiku/
HTTP/1.1\r\n"
+ "Host: " + hostname + "\r\n"
+ "Accept: */*\r\n"
+ "User-Agent: Java\r\n"
+ "\r\n";

socket.getOutputStream().write(writer.getBytes("UTF-8"));
socket.getOutputStream().flush();

reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
for (String line; (line = reader.readLine()) != null;)
{
// if (line.isEmpty()) {
// break; // Stop when headers are completed.
We're not interested in all the HTML.
// }
System.out.println(line);
}
} catch (Exception ex) {
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException logOrIgnore) {
}
}

if (socket != null) {
try {
socket.close();
} catch (IOException logOrIgnore) {
}
}
}

Date endDate = new Date();
System.out.println(endDate.getTime() -
startDate.getTime());
}
}

但是在 NetBeans(桌面应用程序 Java、Swing)中创建的 GUI 应用程序中,不会处理相同的代码,但会出现错误:

 run: java.net.SocketTimeoutException: Connect timed out
at
java.net.SocksSocketImpl.readSocksReply(SocksSocketImpl.java:125)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:579)
at
desktopapplication1.DesktopApplication1View.<init(DesktopApplication1View.java:46)
at
desktopapplication1.DesktopApplication1.startup(DesktopApplication1.java:19)
at
org.jdesktop.application.Application$1.run(Application.java:171)
at
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
ПОСТРОЕНИЕ УСПЕШНО ЗАВЕРШЕНО (общее время: 39 секунд)

GUI 应用程序部分代码:

 DesktopApplication1View extends FrameView {

public DesktopApplication1View(SingleFrameApplication app) {
super(app);

initComponents();

Date startDate = new Date();
String hostname = "ihtik.lib.ru";
int portt = 80;

Socket socket = null;
BufferedReader reader = null;
try {
socket = new Socket();
socket.setSoTimeout(30000);
socket.connect(new InetSocketAddress(hostname, portt),
30000);
String writer = "GET /2011.06.03_prislan.ihtiku/
HTTP/1.1\r\n"
+ "Host: " + hostname + "\r\n"
+ "Accept: */*\r\n"
+ "User-Agent: Java\r\n"
+ "\r\n";
socket.getOutputStream().write(writer.getBytes("UTF-8"));
socket.getOutputStream().flush();

reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
for (String line; (line = reader.readLine()) != null;) {
// if (line.isEmpty()) { // break;
// Stop when headers are completed. We're not interested in all the
HTML. // }
System.out.println(line);
}
reader.close();
socket.close();
} catch (Exception ex) {
ex.printStackTrace();
}

// status bar initialization - message timeout, idle icon and
busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout =
resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener()
{
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});

为什么会出错?

最佳答案

您正在 EventQueue 上运行代码。请参阅here了解更多相关信息。

代码需要移至单独的线程,只需执行 invokeLater() 仍将在事件线程上运行代码。

关于java - java.net.SocketTimeoutException : Connect timed out 的奇怪情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7202850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com