0

I have this code on Server side :

Server Side:

 ServerSocket listenTransferSocket = new ServerSocket(6000);
          Socket connectionTransferSocket = listenTransferSocket.accept();

     DataOutputStream outTransferToClient =
         new DataOutputStream(connectionTransferSocket.getOutputStream());
        {
    .......................   (Some code)
    .......................
           }
    outTransferToClient.write(fileInBytes,0,numOfBytes);
           System.out.println("File send");
       **// outTransferToClient.close();**

        BufferedReader inFromClientR =
                 new BufferedReader(new InputStreamReader(connectionTransferSocket.getInputStream()));

Client Side:

Socket fileTransferSocket = new Socket("localhost",6000);
     DataInputStream in = new DataInputStream(new BufferedInputStream(
                                                 fileTransferSocket.getInputStream()));

OutputStream out = new FileOutputStream(new File("./TransferedFiles/"+fileName));

byte[] by = new byte[numOfBytes];

while ((read = in.read(by, 0, numOfBytes)) != -1) {

       out.write(by,0,read); 
   }

  DataOutputStream outToServerR = 
       new DataOutputStream(fileTransferSocket.getOutputStream()); 
     System.out.println("checkC");
        outToServerR.writeBytes("Transfer completed \n");

and i get the following exception when i try to open the BufferedReader if i close this: outTransferToClient.close();

Exception in thread "main" java.net.SocketException: Socket is closed
        at java.net.Socket.getInputStream(Socket.java:788)
        at Server.main(Server.java:92)

if i dont the while loop on Client Side never stops.. any help????

nobody
  • 19,814
  • 17
  • 56
  • 77
Nicolaos
  • 1
  • 2
  • Note: If you're using Java SE 7, consider to use the new Automatic Resource Management feature to close your streams. – Puce Apr 04 '13 at 08:40
  • possible duplicate of [DataInputStream never stops](http://stackoverflow.com/questions/15807999/datainputstream-never-stops) – Peter O. Apr 04 '13 at 15:45

2 Answers2

1

DataOutputStream extends FilterOutputStream which has the close() method

From docs

Closes this output stream and releases any system resources associated with the stream.
The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

Aside, its always a nice practice to have methods like close() in finally block

rajesh
  • 3,247
  • 5
  • 31
  • 56
0

Yes, closing a DataOutputStream closes the underlying OutputStream as well. The Javadoc for DataOutputStream#close() states:

The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

Plus, the Javadoc for Socket states that when you close a Sockets inputStream or outputStream, it will also close the associated socket.

So you cannot reuse the socket after having closed the DataOutputStream that wrapped either of its streams.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • First of all thank you both.. the thing is that if i dont close this DataOutputStream then when i recieve (using inFromClientR.readine()) the programm loops forever..(and in client side everything are fine. i dont forget '\n') – Nicolaos Apr 04 '13 at 08:31
  • Maybe you should add that piece of code to your question (or start a new question, not sure which is best). Right now, it's hard to answer because we cannot see what your code does. – mthmulders Apr 04 '13 at 08:39