1

I write a client-server application which will be sending an .xml file from the client to the server. I have a problem with sending large data. I notice that the server can get at most 1460 bytes. When I send a file with more than 1460 bytes the server gets only first 1460 bytes and nothng more. In effect I get uncompleted file. Here is my code:

client send:

public void sendToServer(File file) throws Exception
{
    OutputStream output = sk.getOutputStream();     

    FileInputStream fileInputStream = new FileInputStream(file);
    byte[] buffer = new byte[1024*1024];
    int bytesRead = 0;

    while((bytesRead = fileInputStream.read(buffer))>0)
    {
        output.write(buffer,0,bytesRead);
    }

    fileInputStream.close();
}

server get:

public File getFile(String name) throws Exception
{
    File file=null;

    InputStream input = sk.getInputStream();

    file = new File("C://protokolPliki/" + name);
    FileOutputStream out = new FileOutputStream(file);

    byte[] buffer = new byte[1024*1024];

    int bytesReceived = 0;

    while((bytesReceived = input.read(buffer))>0) {
        out.write(buffer,0,bytesReceived);
        System.out.println(bytesReceived);
        break;
    }

    return file;
}

Do anyone know what is wrong with this code? Thanks for any help.

EDIT:

Nothing help :(. I google about that and I think its may connected with TCP MSS with is equal 1460 bytes.

1
  • Exactly what type of OutputStream does sk.getOutputStream(); return? Copy/paste the result of System.out.println("OS is: " + output);. Commented Nov 20, 2011 at 12:02

4 Answers 4

3

Make sure you call flush() on the streams.


A passerby asks: isn't close() enough?

You linked to the docs for Writer, and the info. on the close() method states..

Closes the stream, flushing it first. ..

So you are partly right, OTOH, the OP is clearly using an OutputStream and the docs for close() state:

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

The close method of OutputStream does nothing.

(Emphasis mine.)

So to sum up. No, calling close() on a plain OutputStream will have no effect, and might as well be removed by the compiler.

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

13 Comments

1+, and thanks. I'm no expert on Streams, but shouldn't he also be using buffered streams where possible?
@HovercraftFullOfEels "I'm no expert on Streams," (whispers) Neither am I, and.. "but shouldn't he also be using buffered streams where possible?" ..I'll leave that to those who are. :)
well, isn't close() enough?
See: download.oracle.com/javase/1,5,0/docs/api/java/io/… ("Close the stream, flushing it first.")
From the JavaDoc of OutputStream: The flush method of OutputStream does nothing.
|
3

Although not relate to your question, the API document said FileInputStream.read returns -1 for end of file. You should use >=0 for the while loop.

Comments

3

The MTU (Maximum Transmission Unit) for Ethernet is around 1500 bytes. Consider sending the file in chunks (i.e. one line at a time or 1024 bytes at a time).

See if using 1024 instead of 1024 * 1024 for the byte buffer solves your problem.

Comments

2

In the code executed on the server side, there is a break instruction in the while loop. Therefore the code in the loop will only get executed once. Remove the break instruction and the code should work just fine.

Comments

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.