Socket/Servlets Question - Please HELP!

K

Keren

Hi Guys,
I have a problem regarding Sockets.
What I am trying to do is : I have an applet that avoids security
issues by using servlets. The servlet opens a new socket to the server
at listens to any
new data and sends it to the applet.
the applet reads the data and manipulate it.
The problem is that the applet always read the same thing. it means
that when I read it from the socket the data stays there.

I am attaching my code, I really hope someone could help me!!!
Thanks Alot!!!!!!!!!!!!!
Keren


The Servlet :


public class RT
extends HttpServlet
{
Socket socket;
BufferedReader in;
PrintWriter out;

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
String body = null;
String strdata = null;
PrintWriter out2Applet = null;
String Result = null;
int i = 0;

response.setContentType( "text/plain" );
body = "uid=" + request.getParameter( "uid" ) +
"&sessionid=" + request.getParameter( "sessionid" ) +
"&webip=" + request.getParameter("webip");
strdata = "POST / HTTP/1.1\nHost:
XXX.XXX.XXX.XXX:80\nUser-Agent:
MLKHUB Version .1\nContent-Length: " +
body.length() + "\n" + "Content-Type: application/x-www-form-
urlencoded\n" + "Cache-Control: no-cache\n\n" + body + "\n" ;
socket = new Socket( "XXX.XXX.XXX.XXX", 80 );
in = new BufferedReader( (Reader) new InputStreamReader(
socket.getInputStream() ) );
out = new PrintWriter( socket.getOutputStream(), true );
out2Applet = ((ServletResponse) response).getWriter();
out.write( strdata );
out.flush();
out2Applet.println( strdata );
out2Applet.println();
out2Applet.println( " results..." );
Result = "";
for( i = 0; i < 10; ++i )
{
String inputLine = in.readLine();
Result = Result.concat( inputLine );
}
out2Applet.write( Result );
out2Applet.flush();
out2Applet.close();
}
}


The Reading code :


public class RTConnector
extends Thread
{
Session m_session;
String m_path;
public RTConnector(Session session,String path)
{
m_session = session;
m_path = path;
}

public void run()
{
try
{
URL url = new URL(m_path + "RT?uid="
+ m_session.getUI().getUid() +
"&sessionid=" +
m_session.getUI().getSessionid() +
"&webip=" +
m_session.getUI().getRTIP());
URLConnection con = url.openConnection();
con.setRequestProperty("Content-Type", "text/xml");
BufferedReader in = new BufferedReader(new
InputStreamReader(url.
openStream()));
String inputLine, Result = "";

while (true)
{
while ( (inputLine = in.readLine()) != null)
Result = Result + inputLine;
System.out.println(Result);
in.mark(20);
sleep(1000);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
 
C

Chris Smith

Keren said:
Hi Guys,
I have a problem regarding Sockets.
What I am trying to do is : I have an applet that avoids security
issues by using servlets. The servlet opens a new socket to the server
at listens to any
new data and sends it to the applet.
the applet reads the data and manipulate it.
The problem is that the applet always read the same thing. it means
that when I read it from the socket the data stays there.

Umm... not sure what you mean by "data stays there". Can you explain
more specifically what happens? What data does it always get?

One thing I notice is that you haven't removed the headers from the HTTP
response before trying to use it. Perhaps you're seeing the first 10
lines of headers, and they happen to always be the same?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top