Java Socket Programming-Transferring large sized files through socket

So far we have discussed the fundamentals of Socket programming . In a previous chapter we have discussed the file transfer using TCP. In that example we were converting the entire file object into byte array . And sending it through the socket.But that cannot be applied to large sized file.Because the heap of a JVM  cannot afford an object beyond its maximum capacity.So for transferring large sized files we need to use another approach. In this chapter we are discussing about  transferring large sized files through socket with suitable example.The file size does not have any limit in this case.So this application can be used to transfer movies of large or very large size.The java nio package has a SocketChannel component.We can use a  SocketChannel instance for  sending file. On the other side of connection , a ServerSocketChannel  accepts the connection.Read/Write operations  on a file is done with FileChannel instances

Transferring large sized files through socket


Specify a valid file as source file in .Specify a valid and existing output folder at the  while initializing the RandomAccessFile object.In the above code , we used  localHost  as address .We need to put suitable address there. Start the and then file would be sent from source to destination.  These applications can be used to transfer large sized files (like .mkv  , .mprg movies) from one machine to another.This application can be used to transfer files of  all formats.

5 thoughts on “Java Socket Programming-Transferring large sized files through socket

  1. Aaron Digulla says:

    There is a typo in private void sendMessage(String message): It always sends “request” instead of “message”.

  2. Above program works well if the data is not huge say 20 mb or so. But I tried to move a movie and even though the file size is correct on the client but there is some data corruption.

    How can we enhance the client server to check for data corruption?

  3. Thanks a lot for providing this code

  4. is there any mistake in both sender and receiver code in while loop…

  5. hey you guyz saved my day.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">