views:

108

answers:

4

Hello everyone!

I am currently working on a homework assignment and I am thoroughly stuck. I am on the last question and I just can not figure out how to go about accomplishing the last task. Below is the tasks I had to complete:


  1. The client should save the file in the "client" subdirectory of the home directory.

  2. Test your program. Be sure it works with binary files, not just text files. Be sure it works when both programs are on the same machine as well as when they are separated over the network.


Thus far when I start the server it asks what port I want to use. Then I start the client and it asks what IP and port to use. The the server immediately sends a list of the files in the home directory "server" folder. I then respond with the client with the file number I wish to download. This is where I get stuck. I can't seem to find any information about how to do this. So as you can see in my code posted below, I am trying to use a FileInputReader to convert the file to an array of bytes. Then I am sending that to the client. I am then trying to FileOutputReader the recieved array of bytes to a file. But I can't seem to find the correct methods to do that, or even if I am doing that correctly.

CLIENT

        int i = 0;
        while(i < 1000){
        String modifiedSentence = inFromServer.readLine();
        System.out.println("From Server: " + modifiedSentence);
        i++;
        }

        while(j < 1000) {
        int byteString = inFromServer.read();
        ArrayList<byte[]> bytes = new ArrayList<byte[]>();
        bytes.add(byteString);
        }
        Integer byteInt = new Integer(byteString);
        FileOutputStream fo = new FileOutputStream(System.getProperty("user.home")+ "/client/text.txt");
        fo.write(byteInt.byteValue());

    }
}

SERVER

            byte[] bytes = new byte[1024];
            FileInputStream fi = new FileInputStream(file.toString() + fileArray[userChoiceInt]);
            fi.read(bytes, 0, 1024);
            outToClient.write(bytes, 0, 1024);

        }
    }
}

If anyone could offer any advice or the correct classes or methods to use I would appreciate it.

Thank you in advance.

A: 

You can take a look at this tutorial by Sun (Oracle). That should give you a basic understanding of sockets.

What I do seem to notice however, in the client side, you iterate a specific amount of times (1000) which is not a good idea since generally, you do not know the size of the file to be sent, the tutorial should show this and how to make the appropriate changes. Also, you keep creating the structures within the loop, so you loose any information that you have received, besides creating new datastructures each and every time, which is inefficient.

What you have to do is to move the initialization of the structures from outside the loop. Also, in the case of the modifiedSentence variable, you might want to change it from string to StringBuilder.

npinti
Wow, yea that is terrible. The fact that I am continually instantiating a new ArrayList each time really defeats the purpose. Thanks for pointing that out. As for the while(i < 1000), I was using while(true), but then it would never leave the loop. I also tried the method call on the socket.ready(); but that didn't seem to work. Thank you for your advice, I am looking at the tutorial you posted right now.
Dan Mikita
You should either read until the socket returns -1 (if you are reading bytes) or null (if you are reading strings)
npinti
A: 

If you can use an IO library for this, I would suggest Netty or Mina. There are some netty examples here: http://jboss.org/netty/documentation.html
Even if you cannot use a library, these may be helpful for learning how things are done.

Cbox
A: 

You probably should not ask how to do homework in the class on Web sites like this. It is not appropriate. Your server does look mostly good. Your program will only read files up to 1024 bytes though. You should look at java.io.File in more detail. There is a length method you can use to find the length of the file, so you know how much to send.

Andrew Kalafut
A: 

Without spoiling the whole thing here's some hints.

This can be easily accomplish by using Socket (Server & Client). Using byte[] for transfering the file(s) will ensure that your program will work with both ascii and binary file(s).

Another approach would be to use the build in Remote Method Invocation (RMI). I haven't transfered file using this approach but I'm sure it's feasible.

And in case you didn't know, getting the user home directory is accomplished with the following call: System.getProperty( "user.home" );

Nik