File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes transferring files using sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "transferring files using sockets" Watch "transferring files using sockets" New topic
Author

transferring files using sockets

joe weakers
Ranch Hand

Joined: May 31, 2004
Posts: 38
Hi there. I am trying to transfer xml files via sockets from one machine (acting as server) to another (acting as client) which works perfectly for files under a certain size. However when I attempt to increase the size of the XML file I keep encountering various errors. At the moment my code is configured to parse thru a file and add each character to a StringBuffer and then transmit that StringBuffer to the client via the socket connection. This all works fine in theory until the file that is being transferred reaches a certain size. My code is as follows....ugly I know but it does sort of work!

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
FileInputStream file = new FileInputStream(file1);
int c = 0;
try
{
while ((c = file.read()) != -1)
{
buffer.append( (char) c);
}
out.println(buffer.toString()); //this sends data to the client
}
catch(Exception e){
e.printStackTrace();
}

Can somebody please advise me on a better and more suitable approach to sending files via sockets. Thanks a lot, Joe
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

Doing anything character by character is just plain slow. Java Platform Performance has a chapter on IO performance which will point you in the right direction for efficiency. However, your code above looks like it should work. What does the other end of the socket look like?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
joe weakers
Ranch Hand

Joined: May 31, 2004
Posts: 38
Hiya Joe. Cheers for the response. I am going to turn this query into a more general post. Is there a limit on the size of a datastream (in bytes) that you can send via a socket connection. I am kind of new to all this socket business so you will have to excuse my ignorance. I am under the impression after surfing the net that it is possible to send streams of up to 64kb at one go. However my application will hopefully send files of maybe 1mb or even more than that from a server to a client. Therefore, I presume that I will have to send 64kb segments, one by one, until the entire file has been transferred over the socket connection and then reassemble the segments at the client into a single file. If I am sending several segments, is it necessary to open and close connections for each 64kb transfer or can I maintain a single connection for the entire transfer? Thanks for your time and patience, Joe
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

Originally posted by joe weakers:
Is there a limit on the size of a datastream (in bytes) that you can send via a socket connection.


Absolutely not. The data does get broken into packets to be transmitted across the network, and there's a 64k limit on the size of an IP packet, but the management of packets is obscured from us by the API.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transferring files using sockets
 
Similar Threads
Problem with Encoding SQL
problem with DataInputStream.readInt() method
Image transfer through sockets
Unable to invoke servlet from client (simple java class)
HTML/Post java.lang.classcastexception