我正在尝试构建一个简单的桌面应用程序,用户在其中的文本字段中输入 url 并单击提交按钮以发送该页面的 GET 请求。我正在尝试打开一个套接字以连接到主机提交按钮鼠标点击事件处理程序,但 GUI 卡住了。我必须从任务管理器中关闭它。
private void SubmitButtonMouseClicked(java.awt.event.MouseEvent evt)
{
Socket s = null;
String website = URLTextBox.getText();
String host = "111.65.23.56"; //this could be any host
int port = 80;
try
{
System.out.println("I am opening socket");
s = new Socket(host,port); **// the code stuck here :(**
System.out.println("socket opened");
}
catch(IOException e){ }
// and the code follows......
}
请帮帮我......
最佳答案
您几乎肯定想为 Socket 代码创建一个单独的线程,并且很可能使用出色的 Socket Selector 系列类: Using a Selector to Manage Non-Blocking Sockets
关于java - 无法在事件处理程序中打开套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4603389/