how to send mp3,avi(large) files via socket...small files with size less than 100kb is possible but how to send large file..

What is the problem you are having with the large files?
Does the server dislike receiving too much data?

The basic mechanism is the same for any size file. Is the data arriving too fast for the client? In that case use the same socket connection to send ready/not ready messages from the client to the server to keep things synchronised.

client side file will be created..but it will be in 0kb only ...but no error...prog is attached..


public class FileClient{
  public static void main (String [] args ) throws IOException {
    int filesize=6022386; // filesize temporary hardcoded

    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;
    // localhost for testing
    Socket sock = new Socket("sathya-PC",13267);

    // receive file
    byte [] mybytearray  = new byte [filesize];
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("abc.flv");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead =,0,mybytearray.length);
    current = bytesRead;

    // thanks to A. Cdiz for the bug fix
    do {
       bytesRead =
, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    bos.write(mybytearray, 0 , current);
    long end = System.currentTimeMillis();

public class FileServer {
  public static void main (String [] args ) throws IOException {
    // create socket
    ServerSocket servsock = new ServerSocket(13267);
    while (true) {

      Socket sock = servsock.accept();
      System.out.println("Accepted connection : " + sock);

      // sendfile
      File myFile = new File ("java_test.flv");
      byte [] mybytearray  = new byte [(int)myFile.length()];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);,0,mybytearray.length);
      OutputStream os = sock.getOutputStream();

Does the program work for some files and not for others?
Does the kind of file (text vs binary) make any difference?

I see you are reading the whole file into a buffer array before writing it. (Your use of a int for the file length limits you to 2GB anyway). It's more normal to have a sensible sized buffer and use copy blocks from the file to the output stream until the file is consumed, eg

byte[] data = new byte[4096]; // 4k buffer, could be much larger
int count;
while ((count = != -1) {
	outbound.write(data, 0, count);

Plus its a bad idea to hide exceptions - better to put your code in a try block, catch any/every exception, and call its printStackTrace() method. That way you can be sure of seeing all the details for any exception.

ya working...small size file ok...but how can send larger movie file 700mb(is it possible to send) shows exception...

server side:

C:\Users\Sathya>java FileServer
Accepted connection : Socket[addr=/,port=50781,localport=13267]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at FileServer.main(

client side:

C:\Users\Sathya\test>java FileClient
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at FileClient.main(

data can be send as a this possible(receiving side) to whether that stream is document or mp3, or picture type and can we possible to get original file name

Are you saving all of the file in memory?
Read some, write some, read some more, write some more, etc

.is this possible(receiving side) to know whether that stream is

You could send a header record with information about the data to follow.

ya working...small size file ok...but how can send larger movie file 700mb
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

That's exactly what I expected. See my previous post for the solution.

i wrote client server programs for file transfer.
it copies the sent file content on a file ,created on client side.
but additionaly it stored some garbage text at the end...which also increased the size of the file.
how can i get actual file content?

Make sure you only write what was read. The read method returns the number of bytes that were read.

This question has already been answered. Start a new discussion instead.