Search code examples
javaioexceptionobjectoutputstreamobjectinputstream

Streams - ObjectOutputStream gives NULL


I have two apps that work in conjunction with one another. One is a "server" type app that does not have any GUI interface and handles queries to a database and processes requests from a client. The other is a "client" that is primarily a GUI and is for users to interact with database information in a structured manner.

ISSUE / TROUBLE / HELP NEEDED WITH

The problem that I am having is that I can send one Object (a String[]) to the server successfully and with no problems. Client app sends it, Server app receives it an processes it successfully.

If I try and send a second String[], the the client compiles the array and thinks it gets sent, but the server never receives is (gets only null) and produces a IOException.

This is even with Arrays that contain the exact same number of positions and the exact same text in the exact same format and positions.

The error produced by the printStackTrace() is:

Java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0 (ObjectInputStream.java:1367)
at java.io.ObjectInputStream.readObject (ObjectInputStream.java:369)
at server.ConnectionThread.processClientRequests(ConnectionThread:204)
at server.ConnectionThread.processClientRequests(ConnectionThread:50)
at javalang.Thread.run(Thread.java:722) 

The code at line 204 that is the point where the ObjectStream is being read from:

String[] addArray = (String[]) ois.readObject();

ois is an ObjectInputStream and is initialized as follows:

private ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

CLIENT CODE

The client code used to send these objects TO the server app is:

ObjectToServer.writeObject(String[] var);
ObjectToServer.flush();
ObjectToServer.reset();

COMMENTS

What does NOT make sense to me is that this exact same code format is used to successfully send a number of String[] over the objectOutputStream from the SERVER to the CLIENT app without ever sending a "null"

I have Google searched this and all to absolute no avail.

Someone please help if you can!!

                             ADDITIONAL CODE

// CONNECTION THREAD IS ON SERVER APP, SETS UP STREAMS AND WAITS FOR MESSAGES FROM CLIENT
// HANDLES COMMUNICATION FROM CLIENT AND REST OF SERVER

public class ConnectionThread implements Runnable
{

    private Socket socket;
    private SystemCore core;
    //Streams for connections
    private InputStream is;
    private OutputStream os;
    //Writers and readers for communication of Strings
    private PrintWriter toClient;
    private BufferedReader fromClient;
    // Writers and readers for sending and receiving Objects between server and client.
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;
    //Protocol
    static final String CLIENT_QUITTING = "Exit";

    public ConnectionThread(Socket s, SystemCore aSysCore)
    {
        socket = s;

        // State of the SystemCore as taken from HelloServer
        core = aSysCore;
    }

    public void run()
    {
        try
        {
            openStreams();
            toClient.println(MESSAGE_TO_CLIENT);
            processClientRequests();
            closeStreams();
            this.socket.close();
        }
        catch (OptionalDataException ode )
        {
            System.out.println("OptionalDataException: ");
            System.out.println("length is: " + ode.length);
        }
        catch (IOException ioe)
        {
            System.out.println("IO trouble with a connection in ConnectionThread run() " + ioe.getMessage());
            ioe.printStackTrace();
        }
       catch (ClassNotFoundException cnf)
        {
            System.out.println("Class trouble with a connection in ConnectionThread run() " + cnf.getMessage());
            cnf.printStackTrace();
        }
        catch(ParseException pe)
        {
            System.out.println("Parse trouble with a connection in ConnectionThread run() " + pe.getMessage());
            pe.printStackTrace();
        } 
    }

    /**
     * Opens streams between the server and the client.
     *
     * @throws IOException
     */
    private void openStreams() throws IOException
    {
        final boolean AUTO_FLUSH = true;
        this.is = this.socket.getInputStream();
        this.fromClient = new BufferedReader(new InputStreamReader(is));
        this.os = this.socket.getOutputStream();
        this.toClient = new PrintWriter(os, AUTO_FLUSH);

        //Object streams.
        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());

        System.out.println("...Streams set up");
    }


    /**
     * Private method that accepts arguments from a client and executes the related
     * commands in the systemcore as long as the command passed from the client
     * is not CLIENT_QUITTING.
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void processClientRequests() throws IOException, ClassNotFoundException, ParseException
    {
        String commandFromClient;
        commandFromClient = fromClient.readLine();
        while (!(commandFromClient.equals(CLIENT_QUITTING)))
        {
            if (commandFromClient.equals("addProjectPrepare"))
            {
                String[] addArray = (String[]) ois.readObject();
                core.addProjectPrepare(addArray);
            }
            if (commandFromClient.equals("editProjectPrepareDetails"))
            {
                String[] editArray = (String[]) ois.readObject();
                recruit.editProjectPrepareDetails(editArray);               
            }
        }
           commandFromClient = fromClient.readLine();
    }


**// CLIENT SIDE (User GUI) CODE THAT SENDS STRING[] TO THE SERVER**




public void saveAction()
    {
        // TEST TO SEE IF THE DATE ENTERED IS CORRECT FORMAT, IF NOT NO SAVE OCCURRS

    boolean parsedOk = false;

    if (this.arrivalDateTextField.getText().isEmpty() == false)
    {
        try
        {
            // Check if date is correct format. Nothing will be done with 
            // the testDate object

            MyDate testDate = new MyDate(
                    this.arrivalDateTextField.getText());

            //Allow write to server to occur.
            parsedOk = true;
            //If date is okay, send form data to server.
        }
        catch (ParseException pe)
        {
            this.arrivalDateTextField.setText(""); // Set text field to blank
            int messageIcon = javax.swing.JOptionPane.ERROR_MESSAGE;
            JOptionPane.showMessageDialog(this, "Invalid date",
                    "Warning", messageIcon);
        }
    }
    else
    {
        parsedOk = true; // No date entered so allow blank. 
    }
    if (parsedOk == true)
    {
        // WRITE DATA TO SERVER OCCURS HERE: 

        try
        {

            **//getPersonDetails() returns a String[]**

            ManageClientConnections.toServer.println("addNewData");                 
       ManageClientConnections.objectToServer.writeObject(this.getPersonDetails());
            ManageClientConnections.objectToServer.flush();
            ManageClientConnections.objectToServer.reset();
        }
        catch (IOException ioe)
        {
            System.out.println(
                    "While writing new person to server, there was an error: " + ioe.getMessage());
        }

        // And dispose of the GUI, inside the parseok if clause
        this.dispose();
    }
}

Solution

  • You can't create multiple input/output streams over the same socket input/output streams. that doesn't work. you need to pick one type of stream and stick with it. Since you need to send structured data, you should use only the Object streams and ditch the Print streams. if you need to send different types of messages from client to server, then you should consider using a wrapping Serializable object type (e.g. Message) which can contain different types of messages.