Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Socket/Servlets Question - Please HELP! (http://www.velocityreviews.com/forums/t125016-socket-servlets-question-please-help.html)

Keren 07-30-2003 03:34 PM

Socket/Servlets Question - Please HELP!
 
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();
}
}
}

Chris Smith 07-30-2003 04:07 PM

Re: Socket/Servlets Question - Please HELP!
 
Keren wrote:
> 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


All times are GMT. The time now is 05:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.