klemens
klemens

Reputation: 413

Java and send file through socket

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.

Upvotes: 1

Views: 7260

Answers (4)

Andreas Veithen
Andreas Veithen

Reputation: 9154

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.

Upvotes: 2

Tudor
Tudor

Reputation: 62439

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.

Upvotes: 3

Andrew Thompson
Andrew Thompson

Reputation: 168815

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.

Upvotes: 3

Franz Wong
Franz Wong

Reputation: 1114

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.

Upvotes: 3

Related Questions