Can't get sockets to work

Tom Tromey tromey@cygnus.com
Tue May 25 10:13:00 GMT 1999


>>>>> "Lincoln" == Lincoln Spiteri <lincoln.spiteri@st.com> writes:

Lincoln> I really need a good debugger, will the latest GDB work with
Lincoln> DDD?

I don't know, but you could try it.

Lincoln> For now I guess I will initialise the PrintStream with the
Lincoln> auto flush flag set true.

I didn't look at the code closely enough before.  You wrote this:

   os.print( "Echo: " + request + "\r\n");
   conn.close(); 

It's probably a bad idea to close the file underlying the
PrintStream.  Instead you should close the PrintStream.

Of course, that won't solve your problem.  You still have to flush.  I
recommend writing:

   os.flush();
   os.close();

Lincoln> I would really like to understand what the difference
Lincoln> bettween the flush and print are.  It seems flush will call a
Lincoln> write whilst print will call writeChars in PrintStream where
Lincoln> things get done with an encoding converter, does the
Lincoln> converter do the write itself?

PrintStream uses a buffer internally.

The buffer is only flushed at certain points: when it is full,
(sometimes) when a newline is printed, or when flush() is called.

This is documented in the Java Class Libraries book.

I recommend not worrying about the writeChars method.  That is an
internal implementation detail.

Tom


More information about the Java mailing list