Re: java - problem on code to send file

  • From: Sérgio Neves <mailpgrupos@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 18 Apr 2008 17:46:37 +0100

Hi,
Yes, it works well, thanks!
So, it's normal that the loop on the server side executes more times than the 
loop on the client side, depending on the bytes that the server is able to read 
(I thought the problem was on this, because I thought they had to execute the 
same number of times).
I think you are saying is related to tcp congestion and these low-level things, 
so I have forgotten this, although I have already had a network course.
But.. Is it a thing we have to know?
Imagine one person that has never had a network course and whishes to work with 
sockets. On Sun's documentation, I don't find what you said (maybe   I haven't 
searched well, lol).

Thanks again

Best regards

Sérgio Neves
  ----- Original Message ----- 
  From: public.niran 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Friday, April 18, 2008 2:47 PM
  Subject: Re: java - problem on code to send file


    Although you are reading data on the server in pieces of 1024 bytes, it is 
not necessary that each time there is 1024 bytes in the buffer to read. so the 
read function can only read the available bytes which may be less than 1024 
bytes.
    so you should not asume that 1024 bytes has been read and write it to the 
file, instead you should only write the number of bytes that are actualy 
received.
    Change
                f.write(buf, 0, MAX);
    to
                f.write(buf, 0, RedBytes);
    Also test the code with a text file rather than a zip. so that you can know 
how much is getting send etc by just looking the output.
    I am not a java programmer, so let me know if it solves your problem.
    Niran
    ----- Original Message ----- 
    From: Sérgio Neves 
    To: programmingblind@xxxxxxxxxxxxx 
    Sent: Friday, April 18, 2008 6:22 PM
    Subject: java - problem on code to send file


    Hi,
    I need your help to solve this problem:
    I've made a short application (one client and one server) which the unique 
function is to send a file from the client to the server, but the file is not 
sent correctly.
    I send the file in pieces of 1024 bytes.
    The server creates a socket to accept connections and after creates a 
thread to handle the client, receiving the file size and after receives the 
file content.
    The client creates a socket to connect to the server, and after sends the 
size of the file and then the file content.
    Server and client finish their execution when the file is sent.
    I've created two directories: c:\testclient (which owns the file to send to 
the server) and c:\testserver (into which the file is copied) (I don't test if 
the directories exist or things like that to not complicate the code).
    What happens is that the loop which receives the file content (on the 
server side) is executing more times than the loop on the client side (the 
client loop always executes the correct number of iterations). I know this 
because I've put a variable named "i" in both loops. And the stranger thing is 
that the suplementary number of iterations on the server side is not always the 
same. For example, in one execution it executes 3 more times, in another 
execution it executes 4 more times.. I don't understand this.
    Could anyone help with this, please? You can say if I wasn't clear enough.
    The code follows

    Best regards
    Sérgio Neves

    // client
    import java.io.DataInputStream;
    import java.io.EOFException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Inet4Address;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class Client {
    private static final int MAX = 1024;
      static Socket s;
      static ObjectInputStream ois;
      static ObjectOutputStream oos;

      public static void main (String[] args) throws UnknownHostException,
      IOException, ClassNotFoundException {
        try {
          s = new Socket(Inet4Address.getByName("localhost"), 44444);
          System.out.println("socket created");
          ois = new ObjectInputStream(s.getInputStream());
          oos = new ObjectOutputStream(s.getOutputStream());
          System.out.println("get streams");
            File file = new File("c:/testclient/eclipse-SDK-3.2.2-win32.zip");
            long size = file.length();
            oos.writeObject(size);
            DataInputStream f = new DataInputStream(new FileInputStream(file));
            long sentRemaining = size;
            byte[] buf = new byte[MAX];
            int i = 0;
            do{
              int writtenBytes; 
              if (sentRemaining < MAX){
                writtenBytes = f.read(buf, 0, (int)sentRemaining);
                oos.write(buf, 0, (int)sentRemaining);
                oos.flush();
              }else{
                writtenBytes= f.read( buf, 0, MAX);
                oos.write(buf, 0, MAX);
                oos.flush();
              }
              sentRemaining -= writtenBytes;
              i++;
            }while(sentRemaining > 0);
            System.out.println("terminated");
            System.out.println("the loop executed " + i + " times");
        } catch (EOFException eofException) {
          System.out.println("end connection");
        } catch (IOException ioException) {
          System.out.println("error");
        } finally {
          ois.close();
          oos.close();
          s.close();
        }
      }
    }

    // server

    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class Server implements Runnable {
      private static final int MAX = 1024;
      static ServerSocket s;

      private Socket sb;

      public static void main (String[] args) {
        try{
          s = new ServerSocket(44444);
          //while (true) {
          Socket sb;
          sb = s.accept();
          System.out.println("Connection accepted");
          Server ser = new Server(sb);
          new Thread(ser).start();

          //}//close o while
        }//close try

        catch(IOException ex){
          System.out.println();
        }
      }//close main

      public void run () {
        try{
          ObjectOutputStream oos = new ObjectOutputStream(sb.getOutputStream());
          ObjectInputStream ois = new ObjectInputStream(sb.getInputStream()); 
          try{
            DataOutputStream f = new DataOutputStream(new 
FileOutputStream("c:/testserver/eclipse-SDK-3.2.2-win32.zip", true));
            long receivedRemaining = (Long)ois.readObject();
            byte[] buf = new byte[MAX];
            int i = 0;
            do{
              int redBytes;
              //System.out.println("itera");
              if (receivedRemaining < MAX){
                redBytes = (Integer)ois.read(buf, 0, (int)receivedRemaining);
                f.write(buf, 0, (int)receivedRemaining);
                //f.flush();
              } else{
                redBytes = (Integer)ois.read(buf, 0, MAX);
                f.write(buf, 0, MAX);
                //f.flush();
              }
              //System.out.println("red " + redBytes);
              receivedRemaining -= redBytes;
              //System.out.println("received remaining " + receivedRemaining);
              i++;
            }while(receivedRemaining > 0);
            System.out.println("the loop executed " + i + " times");
            f.close();
          }catch(ClassNotFoundException ex){

          } catch(IOException ex){
            System.out.println("error");
          } finally{
            oos.close();
            ois.close();
            sb.close();
          }
        }
        catch(IOException ex){

        }
      }

      public Server (Socket sock) throws IOException{
        sb = sock;
      }
    }

Other related posts: