0

I'm trying to build a client/server application that sends files but I'm having an issue with larger files. I'm using a BufferedInputStream to read information from a file and OutputStream to write to the socket. I have a loop that reads 1 KB from the file and then sends it, which works fine for the first 25 loops then crashes with a socket write error. Any ideas? Here's the code.

Client

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPClient
{
public static void main(String[] args)
{
    /*Variables*/
    int serverPort = 8899;
    String ip = "localhost";
    File myFile = new File("GeneratedFile.txt"); //fileToBeSent.txt

    System.out.println(myFile.length());

    try
    {
        /*Connect to Server*/
        Socket sock = new Socket(ip, serverPort);
        System.out.println("Connection Made");

        /*Create Streams*/
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream clientOutput = sock.getOutputStream();

        /*This is the old code for transfer*/
        /*Create Byte Array
        byte[] myByteArray = new byte[(int) myFile.length()];  //was 1024

        /*Send File
        bis.read(myByteArray, 0, 1024);
        clientOutput.write(myByteArray, 0, 1024);
        clientOutput.flush();
        */

        for(long i = 0; i <= myFile.length(); i += 1024)
        {
            byte[] myByteArray = new byte[1024];
            bis.read(myByteArray, 0, 1024);
            clientOutput.write(myByteArray, 0, 1024);
            clientOutput.flush();
            System.out.println("i is: " + i);
        }
        System.out.println("File Written");
        sock.close();
    } catch (IOException ex)
    {
        Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("You can't do that!");
    }
    System.out.println("Finished");
}
}

Server

import java.io.*;
import java.net.*;

public class RequestHandler
{   
public void handleRequest()
{
    try
    {
        ServerSocket welcomeSocket = new ServerSocket(8899);

        while(true)
        {
            Socket socket = welcomeSocket.accept();
            System.out.println("Socket Open");

            /* Create byte array */
            byte[] mybytearray = new byte[1024 * 512];

            /* Create streams */
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream("newFile.txt",true);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            /*Write to file*/ 
            int bytesRead = is.read(mybytearray, 0, mybytearray.length);
            bos.write(mybytearray, 0, bytesRead);

            /*Close Stream and Socket*/
            bos.close();
            socket.close();
        }
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void main(String args[])
{
    RequestHandler rq = new RequestHandler();
    rq.handleRequest();
    System.out.println("Here");
}
}
3
  • 2
    What is the exact error? What is the stack trace? Commented Nov 29, 2012 at 19:27
  • A Stack Trace would be nice, but also try wrapping your 'clientOutput' OutputStream in a BufferedOutputStream. Commented Nov 29, 2012 at 20:01
  • Does the crash happen on the client- or server-side? How large is the file you are transferring? (my assumption is, that your buffer is too large for the last part of the file) Commented Nov 29, 2012 at 20:46

1 Answer 1

1

Your copying technique is incorrect. This is how to copy streams in Java:

byte[] buffer = new byte[8192]; // or whatever you like, but declare it outside the loop
int count;
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}
out.flush();
// then in a finally block ...
out.close();
in.close();

You need this at both ends. You cannot assume that any given read will fill the buffer, so you must loop until EOS. Note that you don't flush inside the loop.

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

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.