Which is fast URLConnection or socket

To retrieve data from a remote URL whose connection type will work quickly URLConnection or Socket.

+4
source share
5 answers

Both have different goals, depending on your need.

A socket can implement an almost duplex kind of functionality, while connecting to a URL can connect to a given URI and read its contents.

+6
source

Presumably, you mean the java library "URLConnection" versus or raw sockets.

Since connecting to URLs uses sockets internally, logically, sockets should be slightly faster, since http makes all socket calls plus some overhead when processing the full protocol.

In practice, I would expect a very small difference. If you create your own socket interface, you will need to do most of the extra processing done using the URL connection to make the socket connection workable and reliable.

Also, the "URLConnection" java library was probably written by programmers who are better than you or me. Of course, 10 years have been discovered on most errors. So, why not take advantage of this skill and experience and use the simpler URLConnection.

+2
source

Although it depends on the type of data, server, etc.
I agree with r0ast3d.

For me, if the server allows both things, then I would prefer to use URLConnection.
Both send data using streams, but the standby mode in Socket is slightly higher than in URLConnection.

Please correct me if I am wrong.

+1
source

No matter which one is faster, they both have different goals. A socket is the endpoint for communication between two machines and requires access to another protocol. The class URL is a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something simple, like a file or directory, or it can be a link to a more complex object, such as a query to a database or search engine. In short, if you need to talk to another device or device implementation socket, and if you want to read data from the server, use a URL connection.

Happy coding: D

0
source

From the tutorial http://docs.oracle.com/javase/tutorial/networking/urls/connecting.html URLConnection is NOT abstract and can be inspected

I would like to see some landmarks on which everything is faster.

-1
source

Source: https://habr.com/ru/post/1381826/


All Articles