Posted By:
ajay_luthria
Posted On:
Thursday, March 14, 2002 06:34 AM
Hi there, I have been assigned a task which is: Connect to a Server within 30 seconds. If the server sends a response within 30 seconds,display response else the return 'Connection Timed Out' Attached is my code with the help of a few experts who have guided me Kindly advise if this is correct ajay ajayluthria@hotmail.com public class HttpHandler3 { private static String sURL="localhost"; static String sMessage="Hello Server..Client sending Data"; static HttpURLConnection hpCon=null; static DataInputStream dis =null; public static void main(String[] args)
More>>
Hi there,
I have been assigned a task which is:
Connect to a Server within 30 seconds.
If the server sends a response within 30 seconds,display response else the return 'Connection Timed Out'
Attached is my code with the help of a few experts who have guided me
Kindly advise if this is correct
ajay
ajayluthria@hotmail.com
public class HttpHandler3
{
private static String sURL="localhost";
static String sMessage="Hello Server..Client sending Data";
static HttpURLConnection hpCon=null;
static DataInputStream dis =null;
public static void main(String[] args)
{
sendData(sMessage);
}
// This method is uses http to transfer data normally to.
public static void sendData(String sMess)
{
String response=null;
try{
URL url=null;
String uri = "http://" + sURL + ":8080/servlet/threads.Recieve_Http_Data1";
url = new URL(uri);
hpCon=null;
hpCon = (HttpURLConnection)url.openConnection();
hpCon.setDoOutput(true);
hpCon.setDoInput(true);
DataOutputStream dos = new DataOutputStream(hpCon.getOutputStream());
dos.writeUTF(sMess);
System.out.println("Invoking Timer");
new Timer();
dis = new DataInputStream(hpCon.getInputStream());
try {
response = dis.readUTF();
System.out.println("SERVER RESPONDING : " + response);
}finally
{ dos.close();
dis.close();
}
}catch(IOException e)
{System.out.println("Error in Client : " + e);}
// Inner Class
class Timer extends Thread
{
Timer()
{
start(); // start the Thread.
}
public void run()
{
try {
for(int i=0; i
< 30 ; i++)
{
Thread.sleep(1000);
}
if(dis !=null)
{
try {
dis.close();
}catch(Exception e)
{ System.out.println("Exception caught here : " + e);}
}
}catch(InterruptedException e){
System.out.println("Exception Caught" + e);}
}
}
}
}
<<Less