Manish Basdeo
Manish Basdeo

Reputation: 6269

How is using TCP communication better than HTTP communication?

I have implemented a client server jave program using TCP for an assignment. Now I've to explain why I chose TCP for communication when other alternatives like HTTP are also available.. So I need some reasons why TCP is better than the other one ..

Upvotes: 3

Views: 2468

Answers (3)

Joachim Sauer
Joachim Sauer

Reputation: 308061

HTTP is not an alternative to TCP. It is a protocol built on top of TCP.

Custom, interactive protocols can be much more efficient when implemented on TCP than on HTTP, because HTTP works on a rather basic request/response base*.

On a pure TCP connection, both ends can send messages whenever they want. On HTTP the server can't really proactively send a message to the client. It needs to wait for the client to send a request.

An advantage of HTTP is that it's almost universally understood: there are server- and client-libraries for all languages, there are well-understood caching and proxy-ing mechanisms and there's a wide variety of content negotiation-mechanisms built in.

So it's the traditional trade-off between high-level or lower-level abstraction:

  • lower-level abstraction (TCP) provides high flexbility and the possibility of implementing almost everything, while it is not as simple to use
  • higher-level abstraction (HTTP) provides more built-in features and are easier to support, but additional features are harder to add

* Disclaimer: this answer was written in the era of HTTP/1.1 where this statement was much more correct than it currently is for HTTP/2 and HTTP/3. While these are still fundamentally request/response protocols, they have many advanced features which makes them less restricted in usage than "traditional" HTTP/1.1

Upvotes: 11

Michael Borgwardt
Michael Borgwardt

Reputation: 346327

HTTP is a protocol on top of TCP. It offers specific features and lacks others (most significantly statefulness and the ability for servers to initiate communication). If you need something that HTTP makes hard or impossible, it would be a good idea to use something else.

Or you can kludge those features on top of HTTP, which is what seems to be the most popular option (possibly because of the "only port 80 is open everywhere, so let's use it for everything" issue) but often leads to rather nasty hacks.

Upvotes: 4

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

TCP can't be told as better. It is a protocol of transport(4th) level of OSI model. HTTP is an application protocol(7th level).

They are different and HTTP is based on TCP.

HTTP is basically used for web communications - sites, web-services and so on. It can be told that HTTP is a client-oriented: client asks server for some data and receive the response. When it sends another request and so on. TCP is a base protocol which grants you that all your sent information will be received in the same order and intact.

Read about them on Wiki: HTTP and TCP.

Upvotes: 3

Related Questions