Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I be sure that the server is sending me a response?

Status
Not open for further replies.

luthriaajay

Programmer
Jan 29, 2002
41
FR
How do I know whether that server outputstream is sending me a response or not?
I have opened the Client InputStream to recieve a response from a servlet,but how
can i be sure that i will receive a response from the servlet?
I cud be waiting for 15 seconds expecting a response but havent received one..

Is there any way to check whether the servlet is sending me a response?Any Method to

The reason I am asking is this.

I have written a Java Client that connects to a servlet.It has to wait for a
response from the servlet.It will wait for 5 seconds and if this doesnt recieve
a response,it will return back else it will display the response.

I have set a timer on my client for 5 seconds and a timer on the servlet for 15 seconds.
Essentially,when the client connects,the servlet response is held for 15 seconds
and the client tries for 5 seconds.
But the client is unable to exit without a response.The response comes back in 15 seconds.
The client shud have the message 'Connection Timed Out' after 5 seconds.
This means there is an error somewhere.
As the response takes 15 seconds,the client shudnt recieve one.

So,is there a way I can block the servlet response?

I am using threads and Inner classes for the timer purposes..
Please can any one help me?

ajay


Client code:
------------
<code>

public class HttpHandler {

private static String sURL=&quot;localhost&quot;;
static String sMessage=&quot;Hello Server..Client sending Data&quot;;

static DataInputStream dis = null;
static HttpURLConnection hpCon=null;

public static void main(String[] args)
{
sendData(sMessage);
}

public void TimerTest() {
NewThread nt = new NewThread();
}

public static void sendData(String sMess)
{
String response=null;
try{

// Invoke Timer
new HttpHandler.TimerTest();

URL url=null;
String uri = &quot; + sURL + &quot;:8080/servlet/threads.Recieve_Http_Data1&quot;;

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

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

} // End of Method sendData


// Inner Class

class NewThread extends Thread
{
String response;
int i=0;

NewThread()
{
start();
}

public void run()
{
try {
while(i < 5)
{
System.out.println(i);
Thread.sleep(1000);

try {
dis = new DataInputStream(hpCon.getInputStream());
response = dis.readUTF();

// If response recieved, break off else Loop back.
if(dis !=null)
{
System.out.println(&quot;SERVER RESPONSE : &quot; + response);
dis.close();
break;
}

}catch(IOException e){System.out.println(&quot;Here : &quot; + e);}

i++;

} // End of While.

}catch(InterruptedException e){}
}
}

}


The Servlet
-----------

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import java.io.*;
import java.sql.*;
import java.math.*;

public class Recieve_Http_Data1 extends HttpServlet {

private static final String CONTENT_TYPE = &quot;text/html&quot;;

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println(&quot;Server Ready to receive Message from application :&quot;);
System.out.println();

BufferedReader br=null;

// Data Read by the Servlet
String sMess=&quot;&quot;;
DataInputStream dis = new DataInputStream(request.getInputStream());
sMess = dis.readUTF();

System.out.println(&quot;Received from Client: &quot; + sMess);


// Send response back after 15 seconds Only.
try {
for(int i=0;i<15;i++)
{
System.out.println(i);
DataOutputStream dos = new DataOutputStream(response.getOutputStream());
String sResponse = &quot;Hello Client...This is server sending response&quot;;
dos.writeUTF(sResponse);
Thread.sleep(1000);
}

}catch(InterruptedException e){}

}


}




</code>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top