Results 1 to 2 of 2
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Rep Power

    Default ObjectInputStream does not initialize

    Hi all. I have a client that is supposed to communicate with the server using objects. However, my ObjectInputStream fails to initialize, and blocks. I included the class below, but, unfortunately, it does not compile. I'll try to make a SSCCE, but it could be difficult.

    Java Code:
    import javax.swing.*;
    //The next two classes are my own
    import messages.Message;
    import messages.StringMods;
    public class Client implements MessageListener
    /*Non-Standard Interface in the same package as this class*/
    	private PrintWriter pw;
    	private ObjectOutputStream outObjects;
    	private ObjectInputStream serverObjects;
    	private Socket s;
    	private ClientFrame cf = new ClientFrame();
    	private String uName;
    	public Client(InetAddress ip) throws ConnectionErrorException, IOException{
    		JTextField name = new JTextField(16);
    		int u = JOptionPane.showConfirmDialog(null, name, "Enter Username", JOptionPane.OK_CANCEL_OPTION);
    		if(u == JOptionPane.OK_OPTION){
    			uName = name.getText();
    		else System.exit(0);
    		try {
    			s = new Socket(ip,2);
    		} catch (IOException e) {
    			throw new ConnectionErrorException("Connection to host at " + ip.getHostAddress() + " failed");
    		System.out.println("Initiating I/O");
    		pw = new PrintWriter(s.getOutputStream(), true);
    		System.out.println("Initiating objectInStream");//Prints
    		/*The code below (that has been commented out) worked
    		 * fine. However, the ObjectInputStream does not initialize
    		//System.out.println("s.getInputStream() returned")
    		[color=red][b]serverObjects = new ObjectInputStream(s.getInputStream());//blocks[/b][/color]
    		System.out.println("Initiating objectOutStream");//does not print
    		outObjects = new ObjectOutputStream(s.getOutputStream());
    		System.out.println("I/O Initiated");
    		new ServerMessageListener().start();
    		System.out.println("Started listener");
    		System.out.println("Setting Visibility");
    	private class ServerMessageListener extends Thread{
    		public void run(){
    				Object o = null;
    				try {
    					 try {
    						o = serverObjects.readObject();
    					} catch (ClassNotFoundException e) {}
    				} catch (IOException e) {
    					System.err.println("Message failed to transfer properly");
    				if(s == null){
    				String s = null;
    				if(o.getClass() == Message.class){
    					Message m = (Message) o;
    					s = m.getPrintString();
    				else if(o.getClass() == String.class){
    					s = (String) o;
    				else s = o.toString();
    	public class ClientException extends Exception{
    		private static final long serialVersionUID = 1L;
    		private String err;
    		public ClientException(String m){
    		public String toString(){
    			return err;
    	public class ConnectionErrorException extends ClientException{
    		public ConnectionErrorException(String m) {
    		private static final long serialVersionUID = 1L;
    	@Override //In the self-created interface MessageListener
    	public void handleMessageString(String m) {
    			try {
    			} catch (IOException e) {
    		try {
    			outObjects.writeObject(new Message(uName, m));
    		} catch (IOException e) {
    			cf.appendText(StringMods.createIsolatedMessage("ERROR: could not send message:\n" + m));
    	public static void main(String[] args){
    		try {
    			new Client(InetAddress.getLocalHost());
    		} catch (ConnectionErrorException e) {
    		} catch (UnknownHostException e) {
    		} catch (IOException e) {
    Last edited by Singing Boyo; 06-03-2009 at 04:01 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    From the API docs for the ObjectInputStream constructor you're using
    Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
    IOW, works as designed.

    Once the server initialises it's ObjectOutputStream the process will continue.

Similar Threads

  1. Reading from ObjectInputStream
    By deepthought015 in forum Networking
    Replies: 8
    Last Post: 04-28-2009, 06:57 PM
  2. Stops when opening ObjectInputStream
    By Norberhuis in forum Networking
    Replies: 4
    Last Post: 01-09-2009, 05:19 PM
  3. Int does not initialize, will this work?
    By starchildren3317 in forum New To Java
    Replies: 2
    Last Post: 07-09-2008, 11:42 PM
  4. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:47 PM
  5. I do not know how to initialize the two variables
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts