Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question

Options
  • 09-02-2002 7:46pm
    #1
    Registered Users Posts: 1,176 ✭✭✭


    I am writing a client server application which exchanges objects over a
    socket connection. A new thread is started for each client connection
    and
    ObjectInputStream.readObject is used to receive the object. Once this
    object is processed the server must check from more messages until it
    receives a disconnect message. I do this using the following code:


    while(disconnect!=true)
    {
    try {
    SphinxMessage msg = (SphinxMessage)ois.readObject();
    this.processNormalMessage(msg);
    }catch(IOException e3) { /*do nothing*/ }
    catch (ClassNotFoundException cnfe2){
    System.out.println(" readobject cnfe");
    cnfe2.printStackTrace();
    }
    }

    How can i make ObjectInputStream.readObject() block until it receives
    a message because at the moment it is continously looping calling
    readObject and catching the exception.
    Any help or suggestions would be appreciated.
    David.


Comments

  • Registered Users Posts: 1,176 ✭✭✭podgeen


    anyone?


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    This is some quick code I whipped up to show that readObject does block. I dont know how your code is not blocking. Run this example to verify that it does block

    SERVER

    import java.net.*;
    import java.io.*;

    public class Server {

    public Server() {
    }
    public static void main(String[] args) throws Exception
    {
    ServerSocket ss = new ServerSocket(12345);
    Socket s = ss.accept();
    ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());

    out.writeObject(new String("FIRST"));
    Thread.sleep(10000);
    out.writeObject(new String("SECOND"));
    Thread.sleep(10000);
    out.writeObject(new String("CLOSE"));
    Thread.sleep(10000);

    out.close();
    s.close();
    ss.close();
    }
    }

    CLIENT

    import java.net.*;
    import java.io.*;


    public class Client {

    public Client() {
    }
    public static void main(String[] args) throws Exception
    {
    Socket ss = new Socket("localhost",12345);
    ObjectInputStream in = new ObjectInputStream(ss.getInputStream());
    while(true)
    {
    System.out.println(".");
    Object o = in.readObject();
    //As soon as the above line is called execution is blocked in the
    //current thread. As the server is pausing for 10 seconds, if the //code was not blocking, you would see loads of . and .. being //print out
    System.out.println("..");
    System.out.println(o.getClass().getName());
    String s = (String) o;
    System.out.println(s);
    if(s.equalsIgnoreCase("CLOSE"))
    break;
    }
    in.close();
    ss.close();
    }
    }

    L8er,
    Lynchie


  • Registered Users Posts: 1,176 ✭✭✭podgeen


    i havent tried your code but i got it blocking yesterday, there was a problem with the client i was using to test the server and it was messing up the input stream. Thanks for your help.


Advertisement