0

My JAVA application sends a command to server (command=filename.ini). When the server receives this command it sends filename.ini contents through Socket. The first problem I had was receiving only partial contents of the file. That happened when in the code I used while(in.available()!=0){//write bytes} because in.available() does not know how big/long the content of the file is. If I use while((numBytesRead = dis.read(buffer)) != -1){//write bytes} the loop will never terminate since the Socket connection remains always open. My question is how else can I terminate the loop once every byte has been received? Please help me I have tried everything. I understand where the mistake is but I don't know how to fix it.

The following is the part of the code I have at the moment:

public class TCPClient {

    protected Socket s = null;
    public DataInputStream in = null;

    public TCPClient(InetAddress ipa, int port) {

        Socket s1 = null;

        try { //Open the socket.
            s1 = new Socket(ipa.getHostAddress(), port);
        } catch (IOException ex) {
            System.out.println("Error opening socket!");
            return;
        }

        s = s1;

        try { //Create an input stream.
            in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
        } catch (Exception ex) {
            System.out.println("Error creating input stream!");
        }
    }

    public synchronized byte[] receive() {

        byte[] buffer = new byte[0];

        ByteArrayOutputStream getBytes = new ByteArrayOutputStream();

        try {
            while (in.available() == 0) {
            }  //Wait for data.
        } catch (IOException ex) {
        }

        try {

            int numBytesRead;
            buffer = new byte[1024];

            while ((numBytesRead = dis.read(buffer, 0, 1024)) != -1) { //LOOP NEVER ENDS HERE BECAUSE CONNECTION IS ALWAYS OPEN
                getBytes.write(buffer, 0, numBytesRead);
            }


        } catch (IOException ex) {
        }

        return (getBytes.toByteArray());
    }
}
2
  • Those empty catch blocks are going to cause you trouble. Commented Nov 24, 2011 at 14:40
  • 1
    This: while(in.available() == 0){} is also going to cause you trouble. Just delete it, from read() specification: This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. Commented Nov 24, 2011 at 14:43

1 Answer 1

2

You need to define a micro protocol to say the receiver how long is the file, or just close the connection on the server after finishing sending the file. First method is preferred, since it is a little bit more robust. On the client you should have a timeout too in order to avoid to wait forever in case of network problems.

Clarification for micro protocol: before sending the file itself send a 32 (or 64 if needed) bit integer containing the file length. The client should read that integer and then start retrieving the file.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you all so much for your answers.
is there a sample anywhere you could point me to regarding the micro protocol for receiving file length? Thank you
Depending on the task, you might want to use some existing RPC standard, for example REST which have java implementation called jersey. Unfortunately using such tools will increase the complexity of code quite a bit.
Paolo if I didn't want to use such a tool, is there anywhere an example that shows how to get the file size (length) prior to receiving the file?
Just google for it :) google.fr/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.