TIMER Settings to connect to a server
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ajay_luthria
Posted On:   Tuesday, March 12, 2002 03:00 AM

Is there any variable in the URL method which can be set to a particular value so that when we make a URL connection,the system tries to connect to the URL for a specific number of seconds as set in this variable and if it doesnt connect it returns back?? Let say you want to connect to a URL within 15 seconds, if the system does not connect,ot returns 'Connection Timed out.Try Again' I have been advised to use a 'threads' to control the URL verifier for for 15 seconds? Attached is my java code public class HttpHandler { private static String sURL="localhost"; static String sMessage="Hello Server..Client   More>>

Is there any variable in the URL method which can be set to a particular
value so that when we make a URL connection,the system tries to connect to the
URL for a specific number of seconds as set in this variable and if it doesnt
connect it returns back??



Let say you want to connect to a URL within 15 seconds,
if the system does not connect,ot returns 'Connection Timed out.Try Again'


I have been advised to use a 'threads' to control the URL verifier for for 15 seconds?



Attached is my java code


public class HttpHandler
{

private static String sURL="localhost";

static String sMessage="Hello Server..Client sending Data";


public static void main(String[] args)
{

sendData(sMessage);

}


public static void sendData(String sMess)
{


HttpURLConnection hpCon=null;


String response=null;


try {


URL url;


String uri = "http://" + sURL + ":8080/servlet/threads.Recieve_Http_Data";



try {


url = new URL(uri);


hpCon=null;


Thread.sleep(10000);



hpCon = (HttpURLConnection)url.openConnection();


}catch(InterruptedException e)



{System.out.println("Unavailable " + e);}

System.out.println("ctr is : " + ctr);

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

//Response from the Server
DataInputStream dis = new DataInputStream(hpCon.getInputStream());

try {
response = dis.readUTF();
System.out.println("SERVER RESPONSE : " + response);
}finally
{ dos.close();
dis.close();
}

}catch(IOException e)
{
System.out.println("Error in Client " + e);
}

} // End of Method Encrypt.


} // End of Class HttpHandler



As I am not that familiar with threading,can some one please write/send in a short code for a URL verifier for 15seconds

   <<Less

Re: TIMER Settings to connect to a server

Posted By:   Vladislav_Kamensky  
Posted On:   Tuesday, March 12, 2002 11:48 PM

Hi, Buddy. I see you tried to follow my advise :-) But you code is no use. Probably you are not known with Threads.
I'll provide you with correct code.

public class HttpHandler {

InputStream input = null;

public static void main(String[] args){
new HttpHandler().sendData();
}

public void sendData(String sMess) {
URLConnection con = null;

String response=null;
try {
URL url = new URL(...);
con =(HttpURLConnection)
url.openConnection();
input = con.getInputStream();
//start timer
new Timer().start();

//some read operation form input..

//if reading was succefully completed
input.close();
input=null;
}catch(Exception e){
//connection was closed by timer or
// I/O exception has occured
}

private class Timer() extends Thread{
public void run(){
Thread.sleep(1000);
if(input!=null){
try{
input.close();
}catch(Exception ignored){
}
}
}

}
}


I did not compile it. But i hope you will understand my main idea.
About | Sitemap | Contact