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