Setting a TIMER for a Client.SOS!
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ajay_luthria
Posted On:   Thursday, March 14, 2002 06:24 AM

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. Kindly advise if this is correct ajay ajayluthria@hotmail.com public class HttpHandler { 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); }    More>>

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.

Kindly advise if this is correct

ajay

ajayluthria@hotmail.com

			
public class HttpHandler
{

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);
}

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);

// Transfer Data over http
DataOutputStream dos = new DataOutputStream(hpCon.getOutputStream());
dos.writeUTF(sMess);

// Response from the Server
// DataInputStream dis = null;
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()
{
System.out.println("Here I am");
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);}
}

}

} // End of Method sendData

} // End of Class HttpHandler




   <<Less
About | Sitemap | Contact