3

I'm trying to send large files using socket programming in java for a p2p file sharing application. This code is sending 200-300 mb files without any problems, but for large files around 1 gb it is giving the error:-

    java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
    at FileSender.main(FileSender.java:28)

I'm already sending file in small chunks as suggested in many answers I got. What should I do to send 1 gb files. I'm programming on Windows.

Here are my codes:

Server

public class FileSender {
    public static void main(String...s)
    {
        BufferedOutputStream bos;
        String file="D:\\filename.mp4";

        try {
            ServerSocket sock=new ServerSocket(12345);      
            while(true)
            {   
                System.out.println("waiting");
                Socket soc=sock.accept();
                bos= new BufferedOutputStream(soc.getOutputStream());
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                int n=-1;
                byte[] buffer = new byte[8192];
                while((n = bis.read(buffer))>-1) 
                { 
                    bos.write(buffer,0,n);
                    System.out.println("bytes="+n);
                    bos.flush();
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Reciever

public class FileReciev {
    public static void main(String...s)
    {
        try    
        {      
            Socket sock=new Socket("127.0.0.1",12345);
            File file=new File("D:\\newfilename.mp4");
            BufferedInputStream bis=new BufferedInputStream(sock.getInputStream());
            FileOutputStream fos = new FileOutputStream(file);
            int n;
            byte[] buffer = new byte[8192];
            System.out.println("Connected");
            while ((n = bis.read(buffer)) > -1) {
                System.out.println("bytes="+n);
                fos.write(buffer, 0, n);
                if(n<(8192)){
                    fos.close();
                    bis.close();
                    break;
                } 
                fos.flush();
            }

            System.out.println("recieved");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
4
  • Most probably the receiver is not in sync with the server i.e the receiver cannot read all the data within the time the server sends another packet. Commented May 23, 2013 at 17:44
  • You can implement a blocking mechanism such that the server will not send new data util the receiver has read all previously sent data. Commented May 23, 2013 at 17:49
  • @ExtremeCoders TCP already does exactly that. Commented May 23, 2013 at 23:51
  • Don't flush inside the loop. You're losing all the benefit of the buffered output stream by doing that. Commented May 23, 2013 at 23:52

1 Answer 1

2
if(n<(8192)){
    fos.close();
    bis.close();

What this means is if you ever get less than the number of bytes that you expected, you'll shut down your socket. There's no reason to do this.

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

4 Comments

then how to stop the connection without knowing the file size...??
You loop already terminates at end of stream. That's all you need. Just remove this block of code completely.
even after removing that block file is transferred completely but loop is not ending, its stuck.Its just waiting for another block to read. Receiver side prints "received" at the end.But its not printing "received".
i didnt closed the streams in sender side thats why receiver side was still waiting...!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.