Socket/Servlets Question - Please HELP!

Discussion in 'Java' started by Keren, Jul 30, 2003.

  1. Keren

    Keren Guest

    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();
    }
    }
    }
     
    Keren, Jul 30, 2003
    #1
    1. Advertising

  2. Keren

    Chris Smith Guest

    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
     
    Chris Smith, Jul 30, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    595
    Big Brian
    Oct 14, 2003
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,858
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    974
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    553
    Laszlo Nagy
    Feb 1, 2009
  5. Steve Holden
    Replies:
    0
    Views:
    669
    Steve Holden
    Feb 1, 2009
Loading...

Share This Page