luthriaajay
Programmer
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 = " + 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);}
}
}
}
}
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 = " + 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);}
}
}
}
}