Hello,

I am writng a simple networking program with a single server and multiple clients. I would like to send and receive objects between the clients and the server and hence I use the ObjectOutputStream/writeObject(Object) and ObjectInputStream/readObject() combination.

First, my server program:

import java.net.*; 
import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.ArrayList; 

public class ChatServer implements Runnable { 
    private Socket socket = null; 
    private ServerSocket server = null; 
    private Thread thread = null; 
    private int ID = 0; 
    private ArrayList<ObjectOutputStream> list = new ArrayList<ObjectOutputStream>(); 
    private ChatServerThread t;

    public ChatServer(int port) { 
        try { 
            System.out.println("Binding to port " + port + ", please wait ..."); 
            server = new ServerSocket(port); 
            System.out.println("Server started: " + server); 
            System.out.println("Waiting for a client ..."); 
        } 

        catch (IOException ioe) { 
            System.out.println(ioe); 
        } 

        thread = new Thread(this); 
        thread.start(); 
    } 

    public void run() 
    { 
        while (true) { 
            try { 
                socket = server.accept(); 
                ID++; 
                System.out.println("Client:" + ID + " Accepted " + socket); 
                t= new ChatServerThread(socket, ID); 
                t.start(); 

            } 

            catch (IOException ioe) { 
                //System.out.println(ioe); 
            } 
        } 
    } 

    public static void main(String args[]) { 
        ChatServer server = new ChatServer(2000); 
    } 

    private class ChatServerThread extends Thread implements Serializable { 
        private Socket socket = null; 
        private ObjectOutputStream objectoutputstream = null; 
        private Thread t; 
        private int id; 
        private String line; 
        private ObjectInputStream objectinputstream = null; 

        public ChatServerThread(Socket s, int ID) { 
            super("Server"); 
            socket = s; 
            id = ID; 

            try { 

                System.out.println ("Start " + id);
                objectinputstream= new ObjectInputStream(socket.getInputStream()); 
                System.out.println ("End " + id);

            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                //e.printStackTrace(); 
            } 

            try { 
                objectoutputstream = new ObjectOutputStream(socket.getOutputStream()); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                //e.printStackTrace(); 
            } 
            list.add(objectoutputstream); 
            //writer.println(id); 
            System.out.println ("Inside Thread#" + id + " 's Constructor in Server");
            t = new Thread(this); 
            t.start(); 
        } 



        public void run() { 

            while (true) 
            { 
                try 
                { 
                    Object obj = objectinputstream.readObject(); 
                    if (obj instanceof String)
                    {
                        objectoutputstream.writeObject("Server Echo " + obj);
                        objectoutputstream.reset();
                    }
                } 
                catch (ClassNotFoundException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } catch (IOException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } 
                System.out.println("Connected."); 

            } 

        } 

    } 
} 

The issue I am having is the following:

The Server is started and the first client connects fine with the server. When the server passes the socket and ID to the ChatServerThread objects's constructor, the server seems to get stuck on the following line fragment within it:

      try { 

                System.out.println ("Start " + id);
                objectinputstream= new ObjectInputStream(socket.getInputStream()); 
                System.out.println ("End " + id);

            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 

At this point in the program, the console for the Server shows the following:

Binding to port 2000, please wait ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=2000]
Waiting for a client ...
Client:1 Accepted Socket[addr=/127.0.0.1,port=56989,localport=2000]
Start 1

So, since I do not see the "End 1", the line in the ChatServer thread that is causing an issue is :

 objectinputstream= new ObjectInputStream(socket.getInputStream()); 

Because of this error, the server does not accept any more client requests as this line is unresolved.

I would be grateful for any help.

Thank you!

Recommended Answers

All 6 Replies

Nobody here is going to be stupid enough to waste any time trying to debug a program that discards and ignores relevant Exceptions.
Given the problem you have described, your commenting out line 75 is a candidate for the DaniWeb all-time greatest mistakes hall of fame.

I know there is a second version of the code later in your post that is different - but how is anyone suposed to deal with two versions of the code like that?

Anyway - the error happens in the Thread that deals with individual conections, so there's no a-priori reason why that should stop the main thread from accepting further connections. The answer may be somewhere in the missing stack traces.

Haha - that was entertaining. Thanks for the reply.

:)

But seriously.. what was the cause of the problem?

No clue actually. There are no run time exceptions and the stack traces produce nothing. It seems to simply stall on the line as mentioned above.
Should I be using something other than ObjectInputStream/ObjectOutputStream to read/write objects through sockets? I could use PrintWriter.writeObject(Object), but I am not aware of any other Object other than ObjectInputStream that can read/write via sockets.

Thanks!

Sockets give you raw InputStream and OutputStream on which you can then layer any other higher-level stream eg PrintWriter. However, Object streams work perfectly well over sockets, and have the easiest code. I have used them countless times without problem.
This is very puzzling. Can you try splitting the line into two and see which part is hanging? Also catch any Excecption, including uncheckedEg

try {
    System.out.println ("Start " + id);
    InputStream is = socket.getInputStream();
    System.out.println (is);
    objectinputstream= new ObjectInputStream(is);
    System.out.println ("End " + id);
} catch (Exception e) {
    e.printStackTrace();
} 

Hey James,

Finally..found the error. It was very silly -- Both Client and Server were trying to read at the same time from each other. Once I got the sequencing right, it worked! Thank you again for your patience and humor!

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.