• Post Reply Bookmark Topic Watch Topic
  • New Topic

transfer file

 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i transfer file over socket, i.e, jpg or gif file and how do i know how many percent left when i receive the file.....
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Socket (or accept() from a ServerSocket), get the InputStream (or OutputStream), then proceed as normal.
The simplest way to implement a progress tracker would be to precede the file with a number indicating the length of the file; for instance, chop a long up in four bytes and write them to/read them from the stream. You can then compare the number of bytes received with the total number of bytes expected.
Obviously this only works if you are in control of both the sending and receiving end. Otherwise, you might try parsing gif/jpeg headers as they come in and use that, but that'd be rather more difficult.
- Peter
 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
Create a Socket (or accept() from a ServerSocket), get the InputStream (or OutputStream), then proceed as normal.
- Peter

the my application is a client to a server, made of delphi. will it be any different?... and how actually i read from the stream?... normally i used bufferedreader and printwriter to write and read from socket... is it the same, or i have to use another type of stream?... if so, what it is?.... and how do i know that i'm finish reading the socket, for that jpg file?...
 
Ariffin Ahmad
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
The simplest way to implement a progress tracker would be to precede the file with a number indicating the length of the file; for instance, chop a long up in four bytes and write them to/read them from the stream. You can then compare the number of bytes received with the total number of bytes expected.
Obviously this only works if you are in control of both the sending and receiving end. Otherwise, you might try parsing gif/jpeg headers as they come in and use that, but that'd be rather more difficult.
- Peter

and how actually the web browser do it?.... the web browser can give me an estimate time and percentage of the downloaded files, from any web server..... is there any standard way that i can follow?.....
 
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your web browser was downloading using HTTP, then as part of the HTTP header, it would have a "Content-Length" header that would tell you how much data there is.
If using FTP, then you'd be able to query the size of the file, so you'd know.
Most transfer protocols include some kind of "how much data am I sending" otherwise, how would you know when you got it all.
L
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!