What are Sockets afterall? Why can't we use sockets instead of HTTP?
Are socket开发者_运维技巧s just a connection between 2 machines?
And if sockets can be established why do we even use HTTP connection?
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 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.
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?"
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.
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
精彩评论