Hi,
 
I am writing a chat using Applet-Servlet communication. I have given the code below.
When I run this program, it works fine. Applet sends 10 objects in a for loop to the server without closing the stream & the servlet receives well.
When I comment the for loop & try to read the objects in the same way in a the class receiver in a separate thread, it gives following error.
 
javawebserver: Reading.....java.io.ObjectInputStream@1ca4b5
javawebserver: Receiver : java.io.EOFException: Expecting code
 
Is there anything which prevents the streams to be used in a separate thread? Please help!
 
Thanks in advance!
Rahul.
 
 
public class NewServlet extends GenericServlet
{
 ObjectOutputStream oos;
 ObjectInputStream ois;
 int j;
 receiver r;
 Thread rt;
public void service(ServletRequest req,ServletResponse res) throws ServletException, IOException
 {
  try
  {
   res.setContentType("application/octet-stream");
   System.out.println("In dopost new read"+numberOfCl++);
   ois=new ObjectInputStream(req.getInputStream());
   for(int i=0;i<10;i++)
   {
    s = (String)ois.readObject();
    System.out.println("Got it : "+s);
   }
   /*r = new receiver(ois);
   rt = new Thread(r);
   rt.start();*/
  }
  catch(Exception e)
  {
   System.out.println("Post exception : "+e);
  }
 
class receiver implements Runnable               
{
 ObjectInputStream oois;
 
 public receiver(ObjectInputStream input) {
  oois = input;
 }
 
 public void run()
 {
  try
  {
  for(int j=0;j<10;j++)
   {
    String s = (String)oois.readObject();
    System.out.println("Got it : "+s);
   }
   oois.close();
  }
  catch(Exception e)
  {
   System.out.println("Receiver : "+e);
  }
 }
 }
 
 
public class mon extends Applet implements ActionListener
{
 ObjectInputStream in;
 ObjectOutputStream out;
 URL url1;
 
 
 public void init()
 {
    try{ 
    url1 = new URL(getCodeBase(),"/servlet/NewServlet");
    sendcon = url1.openConnection();
    sendcon.setUseCaches (false);
    sendcon.setDoOutput(true);
    sendcon.setRequestProperty("Content-Type", "application/octet-stream");
 
    out = new ObjectOutputStream(sendcon.getOutputStream());
    System.out.println("10 : Outputstream : "+out);
    for(int i=0;i<10;i++)
    {
     out.writeObject("Applet Response sent : chayla");
     out.flush();
    }
    out.close();
   }
   catch(Exception e)
   {
    System.out.println("Exception : "+e);
    e.printStackTrace();
   }
 }
}

Reply via email to