15

Are sockets just a connection between 2 machines?

And if sockets can be established why do we even use HTTP connection?

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
Carbonizer
  • 1,915
  • 2
  • 19
  • 22
  • 20
    Why do we need cars when we've got wheels? – Marc B Jan 22 '11 at 02:58
  • There is merit in using sockets when you can control both ends and you have a specific use case (eg. push messaging to a desktop app). Only problem is that you restrict some types of environments which only allow HTTP (such as javascript). If your use case is HTML/basic file transfer though, HTTP has advantages in that it also carries relevant information such as content length, user agent, etc. – Matt Lyons-Wood Oct 09 '13 at 06:12

4 Answers4

26

I assume this is a very general question about the relationship between sockets and HTTP connections. I also assume that "HTTPConnection" does not refer to something involving a specific API/runtime/environment even though the way it's a camel cased term with spaces removed could suggest otherwise.

Now that that's out of the way, I present to you, the OSI model:

The OSI Model

The OSI Model describes levels of abstraction for network communication. A socket is a concept which would exist somewhere on layer 3, the Network Layer, as part of the Internet Protocol (IP).

HTTP is higher abstraction than IP, usually regarded as being up in the Application Layer, at the "top" of the OSI model.

An Analogy

You could define a city's transportation and traffic at different "layers" the same way we define network stuffs.

  • At its simplest, a city is a bunch of buildings.
  • As the city grows people need to travel from building to building, so they develop roads. The roads are a new "layer" to the city.
  • As more people use the roads, they begin to need a system of rules and laws to help keep everyone safe.
  • Once people are safe on the roads though, they want the roads to be efficient and quick, so a system of lights and signs help coordinate people on the roads.

Two important things:

First, each layer depends on the one "below" it. Without buildings (destinations) roads become silly. Without roads, traffic laws are silly. Without traffic laws, traffic lights are silly.

Second, the specifics of the higher layers vary depending on the city you're in: sometimes you find yourself in a country where people drive on the left, sometimes they drive on the right. Sometimes you can turn on a red, sometimes not. Sometimes there are roads, but they are without laws.

End of Analogy

So on the Internet, sometimes you communicate with different kinds of servers. Underneath, they may all rely on sockets (the "roads" of he internet) but they all have their own "traffic laws" that you have to respect - protocols like HTTP or FTP or SOAP.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
  • I don't know how I stumbled here and I like your analogy, except I lost you at the end, where the specifics of the higher layers depend on where you are. Are you trying to say HTTP is different in the US vs Russia? Sure there are different protocols, but the protocol established by the ISO should be consistently implemented. Instead, I'd change the analogy to say you need different laws for the type of traffic you have - cars may want to take HOV, trucks may need to be weighed - all traffic still use the roads, but have different requirements and follow different protocols. – vol7ron Jun 15 '13 at 17:44
  • 1
    Can someone elaborate on this answer to explain what is meant by a framework such as socket.io or firebase making use of "sockets"? I know about HTTP and how it is built, but maybe the semantics of saying "it uses sockets" is throwing me a curve ball. In other words, I would prefer a technical explanation over the analogy – Felipe May 16 '17 at 15:18
16

Http is a protocol built on top of sockets.

When you use Http, you're using a higher level of abstraction on top of sockets. You're still using sockets.

It's kind of like saying "Why would you use a .xyz document when you could just use a file?"

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 2
    or "Why use a computer when you got an Abacus?". –  Jan 22 '11 at 03:18
  • 1
    @nebukadnezzar - I don't think that's a good analogy. One either uses a computer or an abacus; in using a computer you are not implicitly using an abacus the way that using an HTTPConnection implicitly uses a socket. Abacus is not an abstraction of computer. – Richard JP Le Guen Jan 24 '11 at 15:21
  • @LeguRi I think you misunderstood it. I was using the term computer as metaphor for an (oversized) calculator. But i see your point. :-) –  Jan 24 '11 at 19:00
  • @nebukadnezzar - Ahhh, I get it now. Ya, I think "calculator" would have been the word ;) – Richard JP Le Guen Jan 24 '11 at 22:06
  • 2
    Please note that **HTTP** is built on top of **Sockets** not **Websockets**. it got me confused for a while! – Amin Jafari Nov 23 '16 at 08:02
6

Read about the OSI model for network communcations: http://en.wikipedia.org/wiki/OSI_model It should do a good job of explaining where each of those components fit.

Erik Iverson
  • 892
  • 4
  • 15
5

Lets say that socket is just a stream between 2 remote systems which uses TCP/IP or maybe UPD lower level protocols for transport data. And HTTP is the higher level protocol which specifies HOW systems are communicating.

Small example: air is a transport level for voice, but you need words (an upper level protocol) for communication with other ppl.

But better for you to read here: http://en.wikipedia.org/wiki/OSI_model

Elalfer
  • 5,312
  • 20
  • 25
  • This is great! __"air is a transport level for voice, but you need words for communication with other people"__ – Ivan Aracki Mar 07 '22 at 11:38