TIMER Setting on Client,USING threads.PLEASE vIEW
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ajay_luthria
Posted On:   Wednesday, March 13, 2002 07:03 AM

Hello, Attached is my Java code Objective : 1.Connect to a Server 2.Wait for a response from the server for 10 seconds 3.If response recvd within time frame,display response else return back with 'Connection TimedOut' I have written the code with a person's guidance. PLEASE CAN ANY ONE TELL ME IF THIS WILL Work as per the Objective AS THIS IS VERY URGENT 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;    More>>

Hello,

Attached is my Java code

Objective :

1.Connect to a Server

2.Wait for a response from the server for 10 seconds

3.If response recvd within time frame,display response else return back with 'Connection TimedOut'

I have written the code with a person's guidance.

PLEASE CAN ANY ONE TELL ME IF THIS WILL Work as per the Objective AS THIS IS VERY URGENT

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 to read everthing within 10 seconds.
//System.out.println("Invoking Timer");
//new Timer();

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 < 10 ; i++)
{
Thread.sleep(1000);
}

// If inputStream is Null
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
About | Sitemap | Contact