1

I got working over socket file sender, it worked perfectly, but I couldn't send large files with it. Always got heap error. Then I changed the code of client, so it would send file in chunks. Now I can send big files, but there is new problem. Now I recieve small files empty and larger files for example videos can't be played. Here is the code of client that sends file:

public void send(File file) throws UnknownHostException, IOException {

    // Create socket
    hostIP = "localhost";
    socket = new Socket(hostIP, 22333);

    //Send file

    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);

    DataInputStream dis = new DataInputStream(bis);


    OutputStream os = socket.getOutputStream();

    //Sending size of file.
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeUTF(file.getName() + ":" + userName);

    byte[] arr = new byte[1024];
    try {
        int len = 0;
        while ((len = dis.read(arr)) != -1) {
            dos.write(arr, 0, len);

        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }



    dos.flush();

    socket.close();
}

and here is the server code:

void start() throws IOException {

        // Starts server on port.
        serverSocket = new ServerSocket(port);

        int bytesRead;

        while (true) {
            connection = serverSocket.accept();

            in = connection.getInputStream();

            clientData = new DataInputStream(in);

            String[] data = clientData.readUTF().split(":");
            String fileName = data[0];
            String userName = data[1];

            output = new FileOutputStream("C:/" + fileName);
            long size = clientData.readLong();
            byte[] buffer = new byte[1024];

            // Build new file
            while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
                output.write(buffer, 0, bytesRead);
                size -= bytesRead;
            }
            output.close();
        }
    }
Charles
  • 50,943
  • 13
  • 104
  • 142
Rohit Malish
  • 3,209
  • 12
  • 49
  • 67
  • http://stackoverflow.com/questions/5113914/large-file-transfer-with-sockets – Adam Matan Jul 12 '12 at 13:00
  • ***As an advice:*** Use `BufferedStream` as middle stream between the `FileStream` and the `DataStream` when sending and receiving through a socket, just for faster transition. – Eng.Fouad Jul 12 '12 at 13:05
  • Duplicate of own post [Can't send large files over socket in Java](http://stackoverflow.com/questions/11449398/cant-send-large-files-over-socket-in-java) – user207421 Jul 12 '12 at 13:08
  • 1
    Please stop reposting this question. This is the third iteration. You've had plenty of answers. If you don't understand them, say so, in the place where the answers are. – user207421 Jul 12 '12 at 13:09
  • Yes it's true I dont understand them, but I already got help from other person, but it created new problem so I posted it again, since its different problem now – Rohit Malish Jul 12 '12 at 13:18
  • @EJP: I directed him to open this new question because that one was about the heap memory problem. After some answers were posted he decided to try sending the file in chunks but had problems so I recommended him to open a different question since that one was unlikely to attract new readers and was technically answered. – Tudor Jul 12 '12 at 13:18
  • Well anyway guys thanks for help. I really appreciate it, but I guess this stuff isn't for me. Programming is hard, expecially when you suck at it :D If you try something for 2 months without success, it just isn't for you :D – Rohit Malish Jul 12 '12 at 13:32
  • @Tudor This is the third appearance of the same code, and both the problem and the answer are the same in all three cases. – user207421 Jul 12 '12 at 23:44

1 Answers1

3

You failed to write out the length of the file to the stream in the client:

long size = clientData.readLong();

So that call in the server is reading the first 8 bytes of the actual file and who knows what that quantity is. You don't have to read the length from the stream since you only wrote a single file. After reading the filename, and username (not very secure is it?) you can just read the stream until EOF. If you ever wanted to send multiple files over the same open socket then you'd need to know the length before reading the file.

Also your buffers for reading are way to small. You should be at a minimum of 8192 instead of 1024. And you'll want to put all .close() in a finally block to make sure your server and clients shutdown appropriately if there is an exception ever.

Tudor
  • 61,523
  • 12
  • 102
  • 142
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • Username is the name of the pc that file came from. Could you edit my code? Its more than 2 months that I work on this issue, everytime i get it to send large files it corrupts them, and when i fix that problem it stops sending large files. I'm really out of options – Rohit Malish Jul 12 '12 at 13:16
  • 13
    My hourly rate is $100/hr. – chubbsondubs Jul 12 '12 at 13:20