954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to send file via socket

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

sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

Attachments FileServer.java (0.86KB) FileClient.java (1.12KB)
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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 = file.read(data)) != -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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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


server side:

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


client side:

C:\Users\Sathya\test>java FileClient
Connecting...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at FileClient.main(FileClient.java:16)
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

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

sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

hi
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?

john321
Newbie Poster
2 posts since Sep 2011
Reputation Points: 4
Solved Threads: 1
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: