0

Please see code below:

s = new Socket(nextHopIP, 3434);

            fileIn = new FileInputStream(fileName);
            fileOut = s.getOutputStream();
            buffer = new byte[bufferSize];
            pw = new PrintWriter(s.getOutputStream());
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));

            pw.println(fromIP);
            pw.println(toIP);
            pw.println(fileName.split("\\\\")[fileName.split("\\\\").length - 1]);
            pw.flush();

            //Send file
            fileTransferTime = System.currentTimeMillis();
            while((readFile = fileIn.read(buffer)) != -1) {
                fileOut.write(buffer, 0, readFile);
            }
            pw.println("");
            pw.flush();
            fileIn.close();
            fileOut.close();
            br.readLine(); //error here
            fileTransferTime = System.currentTimeMillis() - fileTransferTime;
            System.out.println("File transfer time: " + fileTransferTime);
            pw.close();
            br.close();
            s.close();

By the time it reaches br.readLine(), socket closed exception is raised. I've only closed the outputstream so far. Does closing the outputstream also closes the inputstream in this case?

Thanks!

Karl Jamoralin
  • 1,240
  • 1
  • 14
  • 27
  • 1
    Thank you everyone for your replies. It seems that closing the OutputStream will implicitly close the socket associated with it along. To solve my error, I've replaced fileOut.close() with s.shutdownOutput(). – Karl Jamoralin Aug 28 '10 at 18:57

5 Answers5

2

From the API docs of java.net.Socket.getOutputStream

Closing the returned OutputStream will close the associated socket.

br is closed because it decorates the input stream associated with the socket. Strange, but true. It is possible to half-close a socket.

Also note that resource handling should be done as:

Resource resource = acquire();
try {
    Decorator decorated = decorate(resource);
    use(decorated);
    decorated.flush();
} finally {
    resource.release();
}

(Possibly using the Execute Around idiom.)

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

When you close the FileInputStream two lines before the error, wouldn't that also close the BufferedReader since it now has no stream from which to read? I'm kind of guessing here, not really a Java guy, but it would make sense.

David
  • 208,112
  • 36
  • 198
  • 279
  • No - because the buffered reader `br` sits on top of the socket `s` whereas the file input stream, `fileIn`, does not. – oxbow_lakes Aug 28 '10 at 18:06
0

As far as I know, the sockets should handle the management of its streams. So when you close the socket it closes the underlaying streams.

If you want to shutdown a particular stream, you should use shutdownInput() instead of closing the input socket; the same exists for the output.

Kru
  • 4,195
  • 24
  • 31
0

It should be easy to test (solve?) it, just move the closing of the OutputStream to the end, where you are closing the InputStream of the Socket.

Or just check the java doc for getOutputStream():

Closing the returned OutputStream will close the associated socket.

and closing the Socket will also close the InputStream (close).

Closing this socket will also close the socket's InputStream and OutputStream.

user85421
  • 28,957
  • 10
  • 64
  • 87
-2

The behaviour of sockets is highly platform dependent, so you can never be sure that the available documentation or methods will work as you expect.

I would think that it is highly probable that closing an OutputStream causes the underlying socket to get closed. I had a similar problem with sockets not behaving as expected which I've asked previously.

This answer indicates that closing the output stream of a socket does indeed close the underlying socket

Community
  • 1
  • 1
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • The behaviour of sockets is very much as per the API. I'm aware of a few platform dependencies but most people will never encounter them, and they certainly don't account for this behaviour which is precisely as per the documentation on all platforms. – user207421 Aug 29 '10 at 01:51
  • Most people probably don't do much socket programming; so, yes, I suppose you are right about that. But saying they work as in the API except for a few cases just backs up my point, doesn't it? – oxbow_lakes Aug 29 '10 at 11:33