I am trying to build a simple desktop application where the user enters the url in a text field and clicks on the submit button to send the GET request for that page.I am trying to open a socket to connect to the host in the submit button mouse clicek event handeler but the GUI gets stuck. I have to close it from task manager.
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......
}
please help me out…..
You’ll almost certainly want to create a separate thread for the Socket code, and most likely using the excellent Socket Selector family of classes:
Using a Selector to Manage Non-Blocking Sockets