Results 1 to 10 of 10
  1. #1
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default java.io.IOException: Stream not marked

    I am creating a Server / Client environment. When I start the server (with Class HelloClient that creates and passes the socket to ConnectionThread - which itself waits for commands from a client and processes these indefinitely until it receives the string "Exit") it seems to run fine.

    When I start the client it seems to start up okay and opens the GUI that I created with no apparent issues.

    However the first time I try to pass a command from client to server (all on local host right now) i get the following error:


    compile-single:
    run-single:
    ...Server starting up
    Server is connected to Client
    ...Streams set up
    Trouble with a connection java.io.IOException: Stream not marked

    I don't know what an "unmarked stream" is or how I would mark one.

    The run method of HelloServer is written as follows:

    Java Code:
        public void run()
        {
            try
            {
                // create a ServerSocket object to listen on the port
                this.serverSocket = new ServerSocket(PORT_NUMBER);
                while (true)
                {
                    //Wait for a connection request
                    this.socket = this.serverSocket.accept();
                    System.out.println("Server is connected to Client");
    
                    //Start connection thread to handle connection. 
                    this.connection = new Thread(new ConnectionThread(this.socket,
                            this.recruit));
                    this.connection.start();
                }
    
            }
            catch (Exception exp)
            {
                System.out.println("Trouble with connection " + exp);
    
            }
        }
    ConnectionThreads run() method looks like this:

    Java Code:
        public void run()
        {
            try
            {
                openStreams();
                toClient.println(MESSAGE_TO_CLIENT);
    
                processClientRequests();
    
                closeStreams();
    
                this.socket.close();
            }
            catch (Exception exp)
            {
                System.out.println("Trouble with a connection " + exp);
            }
        }
    And the openStreams() method is written as follows:

    Java Code:
        /**
         * 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");
        }
    The Client command that is the cause point of this error is:

    Java Code:
     
    
        /**
         * Add a recruiter item to the collection of available recruiters. 
         */
        public void addRecruiter()
        {
            if (recruiterFistNameTextField.getText().isEmpty() == false
                    && recruiterLastNameTextField.getText().isEmpty() == false)
            {
    
                String firstName = this.recruiterFistNameTextField.getText();
                String lastName = this.recruiterLastNameTextField.getText();
    
                toServer.println("addRecruiter");
                toServer.println(firstName);
                toServer.println(lastName);
    
                this.recruiterFistNameTextField.setText("");
                this.recruiterLastNameTextField.setText("");
                this.populateRecruiterList();
            }
        }
    and the portion of processClientRequests() in ConnectionThread that receives this is:

    Java Code:
                if (commandFromClient.equals("addRecruiter"))
                {
                    String fNname = fromClient.readLine();
                    String lNname = fromClient.readLine();
                    recruit.addRecruiter(fNname, lNname);
                    fromClient.reset();
                }
    Any ideas what it is that I am doing wrong?


    Joel

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: java.io.IOException: Stream not marked

    Can you post the full text of the error message?
    What statement is executing when the error occurs?

    Add a call to printStackTrace() to all the catch blocks so you get the full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: java.io.IOException: Stream not marked

    Thanks.

    First of all the Try{} Catch() blocks were catching Exception exp - and the print out was simply

    System.out.println("Error with connection" + exp);

    I adjusted this and so that specific exception are caught (IO, ClassNotFound and ParseException) and added getMessage as well as printSTackTrace

    My GUI Opens and runs without any seeming hitch. I can add a data item to one of the collections of the core system (two strings arguments that are passed to the core system on the server side and become an Object) but when I try to delete these then I get the following:


    init:
    Deleting: C:\Users\Joel\Documents\...\Q3Server\build\built-jar.properties
    deps-jar:
    Updating property file: C:\Users\Joel\Documents\...\Q3Server\build\built-jar.properties
    Deleting: C:\Users\Joel\Documents\...\Q3Server\null170814749 7
    Compiling 1 source file to C:\Users\Joel\Documents\...\Q3Server\build\classes
    compile-single:
    run-single:
    ...Server starting up
    Server is connected to Client
    ...Streams set up
    IO trouble with a connection in ConnectionThread run() Stream not marked
    java.io.IOException: Stream not marked
    at java.io.BufferedReader.reset(BufferedReader.java:4 85)
    at server.ConnectionThread.processClientRequests(Conn ectionThread.java:287)
    at server.ConnectionThread.run(ConnectionThread.java: 50)
    at java.lang.Thread.run(Thread.java:619)


    Line 287 of ConnectionThread is:

    Java Code:
     
    
                if (commandFromClient.equals("deleteRecruiter"))
                {
                    String name = fromClient.readLine();
                    recruit.deleteRecruiter(name);
                    fromClient.reset();                         // LINE 287
                }
    I don't know where to find line 485 of the BufferedReader.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: java.io.IOException: Stream not marked

    What class is the fromClient variable?
    Stream not marked
    Have you read the API doc for the reset() method? Are you using it correctly?
    Where do you call mark()?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: java.io.IOException: Stream not marked

    fromClient is declared as:

    this.fromClient = new BufferedReader(new InputStreamReader(is));


    I did have a look at the Java API concerning the reset method. I did not totally understand it but my duplication was that if mark() had not been called on a stream then the stream would return to its "original" (empty) state.

    What I am trying to accomplish is that I have used calls to PrintWriter and Buffered reader streams numerous times and wanted a way to ensure that neither the HelloServer nor HelloClient would be at some point "confused" or delayed (and thus execute old commands) and that each call included a reset to clear away anything previously.

    Obviously I am wrong as if I get rid of this reset() method call I get rid of the errors and things start to function again.

    Is this something that I should not be worried about (using reset() to clear out old information)? I know that ObjectOutputStream and ObjectInputStream operate on HashMaps and reset is used there to clear the hashmap. Is something similar not the case with PrintWriter and BufferedReader?

    Joel
    Last edited by joelBeaton; 03-30-2012 at 08:25 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: java.io.IOException: Stream not marked

    using reset() to clear out old information)
    Is that what the API doc says?
    Can you explain what "clear out old" means? You have read some of the file's data. Now what do you want to do?
    ObjectOutputStream and ObjectInputStream operate on HashMaps
    Where is that documented?
    not the case
    Yes, it is not the case.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: java.io.IOException: Stream not marked

    Thanks.

    The datum on the ObjectOutputStream operating on a hashtable (sorry - said hashmap previously, had to look again) can be found at:

    Advanced Socket Programming

    Specifically quoted:

    Object Serialization Pitfall
    When working with object serialization it is important to keep in mind that the ObjectOutputStream maintains a hashtable mapping the objects written into the stream to a handle. When an object is written to the stream for the first time, its contents will be copied to the stream. Subsequent writes, however, result in a handle to the object being written to the stream. This may lead to a couple of problems:

    If an object is written to the stream then modified and written a second time, the modifications will not be noticed when the stream is deserialized. Again, the reason is that subsequent writes results in the handle being written but the modified object is not copied into the stream. To solve this problem, call the ObjectOutputStream.reset method that discards the memory of having sent an object so subsequent writes copy the object into the stream.
    An OutOfMemoryError may be thrown after writing a large number of objects into the ObjectOutputStream. The reason for this is that the hashtable maintains references to objects that might otherwise be unreachable by an application. This problem can be solved simply by calling the ObjectOutputStream.reset method to reset the object/handle table to its initial state. After this call, all previously written objects will be eligible for garbage collection.
    The reset method resets the stream state to be the same as if it had just been constructed. This method may not be called while objects are being serialized. Inappropriate invocations of this method result in an IOException.


    I understand what you are saying that this is not the case with PrintWriter / BufferedReader.

    What do I mean by "clear out old"

    Some data has been passed from Client to Server or Server to Client and been acted upon (a command called, information saved, whatever). What I was aiming to achieve now is making sure there is nothing on the Client or Server that will store that information any further so that it cannot be "re-used" in error and that it is, in fact, discarded. Is there a need to do this? Or not at all and I am being a little paranoid?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: java.io.IOException: Stream not marked

    If you have read from the input stream, what was read will not be read again, unless you mark and reset.

    Thanks for the link.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: java.io.IOException: Stream not marked

    Thanks for your help Norm.

  10. #10
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: java.io.IOException: Stream not marked

    API docs solve a huge quantity fast and can lead to better questions.

    java.io.InputStream or java.io.BufferedReader

    void mark(int readAheadLimit) Marks the present position in the stream.
    Marks the present position in the stream. Subsequent calls to reset()
    will attempt to reposition the stream to this point.
    Not all character-input streams support the mark() operation.

    boolean markSupported() whether mark supported in this i.o. class

Similar Threads

  1. Replies: 0
    Last Post: 01-21-2011, 12:43 PM
  2. java.io.IOException: Premature EOF
    By CrazyVag in forum New To Java
    Replies: 5
    Last Post: 01-26-2010, 10:55 PM
  3. Replies: 1
    Last Post: 11-16-2009, 07:12 AM
  4. Problems with getTables marked
    By albyblueska in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 07:20 PM
  5. Replies: 0
    Last Post: 07-08-2009, 08:10 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
  •