Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree1Likes

Thread: Sending multiple files to client

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: Sending multiple files to client

    Some more debugging ideas: Look at the available() bytes before and after reads.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    I spent hours trying to find what's wrong and I came up with the conclusion that print writer and buffered stream cannot be used together. I made an even smaller program which only involves sending two strings; one using a printwriter and one using a buffered stream. Even with such a simple program the results are the same; the bytes sent from buffered stream cannot be read from the client if the client calls readLine() before. (the client hungs waiting for the bytes).

    This is the piece of code for sending data from server:

    Java Code:
    PrintWriter sender = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedOutputStream toClient = new BufferedOutputStream(clientSocket.getOutputStream());
    sender.println("PrintWriter");          
    byte[] bytes = new byte[]{'B','u','f','f','e','r','e','d'};
    toClient.write(bytes);
    toClient.flush();
    And the piece of code for receiving data from client:


    Java Code:
    BufferedReader receiver = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
    InputStream fromServer = serverSocket.getInputStream();
    byte[] bytes = new byte[8];
    System.out.println(receiver.readLine()); 
    fromServer.read(bytes, 0, bytes.length);
          for (int i = 0; i < bytes.length; i++) {
                System.out.print((char)bytes[i]);
           }
    The output is "PrintWriter". If you remove 'sender.println("PrintWriter")' and 'System.out.println(receiver.readLine())' the result will be "Buffered". If readLine() is called before the data sent from the buffered stream are read, the only way to read those data is using readLine()/read() again.
    Last edited by po105; 10-07-2012 at 10:49 PM.

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: Sending multiple files to client

    I think that the BufferedReader class reads and buffers more that just what was written by the println() method. Getting a new InputStream after the BufferedReader class has read data will miss what has been buffered.

    Try writing your own "readLine()" method that reads bytes from the InputStream through the end of line characters. Without the buffering the data could be left in the stream and be able to be read by the receiveFile() method.
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    That is what I've just tried and it works perfectly. I want to thank you very much for your help, I don't know how I would come to this conclusion without your help. I appreciate it.

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: Sending multiple files to client

    Your welcome. I might have learned a little also.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. sending byte from server to C client
    By rnvrnv in forum Networking
    Replies: 11
    Last Post: 07-02-2010, 03:20 PM
  3. Sending array of data to client
    By k80sg in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 07:48 AM
  4. sending to client
    By rob in forum New To Java
    Replies: 1
    Last Post: 02-22-2009, 11:07 AM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 05:12 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
  •