Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

transferring files using sockets

 
joe weakers
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
joe weakers
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic