dcsimg
Please See this regarding Client TIMEOUT
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ajay_luthria
Posted On:   Thursday, March 14, 2002 08:56 AM

Hello Alex, Please ignore this if this has been read. My Objective is: Connect to a URL (from a Java Client). Get a response back from the Servlet.Keep trying for 30 seconds. If a response is not recieved within 30 seconds,return 'Response Timed Out' else display the response received. I have written the Java class with threads with some guidance from the JGURU members. Please could you tell me if the code attached will attain my objective. An answer is really appreciated ajayluthria@hotmail.com public class HttpHandler { private static String sURL="localhost&qu   More>>

Hello Alex,


Please ignore this if this has been read.

My Objective is:


Connect to a URL (from a Java Client).
Get a response back from the Servlet.Keep trying for 30 seconds.
If a response is not recieved within 30 seconds,return 'Response Timed Out'
else display the response received.


I have written the Java class with threads with some guidance from the JGURU members.


Please could you tell me if the code attached will attain my objective.


An answer is really appreciated


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

// 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";

System.out.println(new Date());

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

}

} // End of Method sendData

} // End of Class HttpHandler

   <<Less
About | Sitemap | Contact