Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Cancel Reading Over A Socket

Thread view: 
Jason Cavett - 21 May 2007 16:23 GMT
I am attempting to cancel a running process that is reading data
coming in via a socket from an external (non-Java) application.  The
cancel *does* work, except I always get an exception.  Here is the
code in question...

   private void process() throws ProcessException {
       socketWriter.println("START PROCESSING");

       try {
           File outFile = new File(workingFolder, "output.txt");
           FileWriter fw = new FileWriter(outFile, false);
           BufferedWriter bw = new BufferedWriter(fw);

           String line = socketReader.readLine();
           while
(line.indexOf(NativeProcessDistributed.COMPLETED_RUN) == -1) {
               bw.write(line);
               bw.newLine();
               line = socketReader.readLine();
           }

           bw.flush();
       } catch (IOException e) {
           throw new
ProcessException(NativeProcess.CANT_WRITE_OUTPUT);
       }
   }

Basically, I start the processing (via START PROCESSING written to the
application) and then watch the input for a string that means the run
has completed.  Until then, I write out all output to an output file
(so someone can examine the output if anything goes wrong with the
processing).

I want the user to cancel the run at any time in the GUI via a cancel
button.  This is the method that's fired when the cancel button is
pressed

   public void cancel() {
       try {
           // write a suicide file/flag to force the processing to
halt
           File suicide = new File(runFolder + SUICIDE_FILE);
           suicide.createNewFile();

           this.cleanup();

           status = CANCELLED;
           stateChanged();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

The cleanup() method is as follows:

   private void cleanup() {
       try {
           socketReader.close();
           socketWriter.close();
           clientSocket.close();
           nativeProcess.destroy();
       } catch (IOException e) {
       }
   }

What's happening (as far as I can tell) is that I'm closing the
clientSocket while the "while" loop in the process() method is still
looping, thus causing the exception to be thrown.  I am not sure how
to sync the two methods up so that I can cancel the process AND the
while loop in the distribute method stops (the socketReader.readLine()
is what is throwing the actual exception, BTW).  It may be a threading
issue, but I'm having difficulty figuring out what the issue actually
is.

Thanks for any help with this frustrating problem.
Martin Gregorie - 21 May 2007 18:27 GMT
> I am attempting to cancel a running process that is reading data
> coming in via a socket from an external (non-Java) application.  The
[quoted text clipped - 72 lines]
>
> Thanks for any help with this frustrating problem.

Why not simply stop the loop and let the program clean up as normal
(closing files and connections)? The Cancel button's action listener
should be able to set the necessary watchdog boolean.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Jason Cavett - 21 May 2007 20:15 GMT
On May 21, 1:27 pm, Martin Gregorie <mar...@see.sig.for.address>
wrote:
> > I am attempting to cancel a running process that is reading data
> > coming in via a socket from an external (non-Java) application.  The
[quoted text clipped - 83 lines]
>
> - Show quoted text -

I tried that (and tried it by synchronizing the boolean variable) but
the loop didn't seem to recognize the flag.

I did figure out an alternative method - shutdownInput() and
shutdownOutput() on the socket.  That way, the value will be read in
as null allowing me to check for a "null" value in the while loop for
the variable line.  I don't know if this is the best method, but it
seems to work.
Knute Johnson - 22 May 2007 04:50 GMT
> I am attempting to cancel a running process that is reading data
> coming in via a socket from an external (non-Java) application.  The
[quoted text clipped - 72 lines]
>
> Thanks for any help with this frustrating problem.

If the stream or socket is closed an IOException is thrown.  For a
normal stop (when you've read the stop flag) I would close the stream
after the loop.  To force a termination, close the socket, that will
also close all streams and throw a SocketException.

try {
    while (true) {
        String str = reader.readLine();
        if ("end message".equals(str))
            break;  // you are done
        else if (str == null)
            break;  // end of stream
        // process str here
    }
    reader.close();  // normal exit, close stream
    // close your file i/o too
} catch (IOException ioe) {
    // print error message if needed
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.