Nick Chapman
Nick Chapman

Reputation: 4634

What is the speed difference between downloading with URL and Sockets?

I'm looking to create a Java application which will download podcasts to someone's computer automatically. I'm wondering whether I should be making a Socket connection to my server or simply using the URL class.

Does anyone know what the speed difference is between downloading through a URL vs a socket connection. I know the URL object is built in part on top of the Socket object, but I'm not sure what the difference in run time is.

Upvotes: 1

Views: 151

Answers (1)

user2864740
user2864740

Reputation: 61885

Use the existing library support. The only reason to use a Socket directly for this task is when connecting to a custom protocol (i.e. not HTTP for which there is no suitable implementation). There are so many existing transfer protocols - don't waste time creating another for this generic case.

HttpURLConnection is ultimately implemented with TCP Sockets that "speak" HTTP. Using Sockets directly would require writing the code to understand HTTP - in whatever context is required. This code, even if done "more efficiently" than HttpUrlConnection will represent only a small fraction of the actual execution time which will be dominated by other factors.

Instead, consider how speed can be improved at a higher-level:

  • Download multiple remote resources concurrently
  • Using HTTP pipelining, where applicable
  • Enable HTTP compression, when applicable
  • Switch to a different protocol designed for multi-file synchronization

Upvotes: 2

Related Questions