TCP vs UDP - What is a TCP connection?

60,251

Solution 1

Let's break this up into parts. First of, the network is based in IP, which is a protocol that assigns an address to each network node, and which allows you to send small amounts of data (usually up to 64kB, but typically only 1500B) from one node to another.

That by itself isn't worth much yet, because we can't make any checks that the data actually arrived, and that it arrived in the right order. If we want an abstract mechanism to transmit arbitrary amounts of data and ensure that they arrived, we need another protocol on top of the network that handles this "transmission". And that's the purpose of TCP.

However, in parallel to TCP, there's another "transmission" protocol that doesn't do any checking at all and has no reliability, UDP. UDP is just a thin wrapper around raw IP packets, which adds a little bit of meta data (like a port number).

UDP is still useful, though, since there are many situations in which the data integrity is already handed off to an even higher protocol, so there's no need for a complex transmission protocol. This is for example used in virtual networking services, where another instance of TCP/IP is typically run over a UDP channel. (Making the channel use a reliable protocol like TCP can actually have disastrous consequences in that case due to resend cascades.)

So the term "TCP connection" refers to the application of the TCProtocol. The protocol is stateful, naturally, and typically proceeds in a SYN-ACK-data-FIN sequence, or SYN/RST in case of a rejected transmission; both peers maintain a status of the connection (handshake, established, closing, closed.) TCP also introduces the terms "server" and "client", the server being the peer that listen()s for an incoming connection.

Solution 2

The main difference between TCP and UDP sockets is that UDP is conectionless and doesn't use any confirmation that the other end received the data.

The Transmission Control Protocol (TCP) is one of the core protocols of the Internet Protocol Suite. TCP is one of the two original components of the suite, complementing the Internet Protocol (IP), and therefore the entire suite is commonly referred to as TCP/IP. TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer. TCP is the protocol that major Internet applications such as the World Wide Web, email, remote administration and file transfer rely on. Other applications, which do not require reliable data stream service, may use the User Datagram Protocol (UDP), which provides a datagram service that emphasizes reduced latency over reliability.1

Share:
60,251
Cheese
Author by

Cheese

Updated on July 09, 2020

Comments

  • Cheese
    Cheese almost 4 years

    What exactly is a TCP connection? I understand there isn't a physical connection from the client to server. Is this connection just the client's socket being linked with the new socket created by the server after the three-way-handshake? Thereafter once the "connection" is set up, the sockets on either ends of the connection then know where to send their packets.

    How does this differ from the way UDP functions other than the initial handshake with TCP? Is it that each server socket only has one client that sends packets to that particular socket?

    What are some possible advantages of having a dedicated connection between hosts? My understanding of TCP and UDP is still very basic, so broad generalizations should suffice.

  • Cheese
    Cheese over 12 years
    Hmm... I'm trying to think of how I need to word this to get to what I'm after. I understand that TCP adds additional functionality that allows for more reliable data transfer. It also has features to handle congestion control. I guess I'm wanting to ignore those features and focus just on the actual "connection". If I was to design a UDP client and server that sent acknowledgments back and forth to increase reliability, would this then be a "connection" like that used in TCP?
  • Cheese
    Cheese over 12 years
    Does setting up a TCP connection form some sort of agreement between the host and server with regard to how future packets will be handled or interpreted? This would mean certain information would only be transmitted during the connection setup. Whereas UDP, on the other hand, would need to include such information with every packet sent.
  • Kerrek SB
    Kerrek SB over 12 years
    @Cheese: First question: That'd essentially just be re-inventing TCP! Second question: There's certainly a numbering of packets that's handled by the protocol. It's really not much more than "I promise that this arbitrary pack of data arrives at the other end." It's hard to envisage a solution on your part that would be better than just a native TCP connection...
  • Parziphal
    Parziphal over 8 years
    Man, the last paragraph of your answer appears at the top of Google when searching tcp connection. Congrats~, I guess.
  • Kerrek SB
    Kerrek SB over 8 years
    @renocor: Haha, indeed, thanks :-) That's a bit embarrassing, isn't it. Oh well.
  • Kasun Siyambalapitiya
    Kasun Siyambalapitiya almost 7 years
    @KerrekSB can you please be kind enough to tell me what is meant by SYN-ACK-data-FIN and SYN/RST
  • Kerrek SB
    Kerrek SB almost 7 years
    @KasunSiyambalapitiya: These are the kinds of TCP packages. SYN, ACK, FIN and RST are the handshaking metadata that establish or end a connection.
  • Rafael Eyng
    Rafael Eyng about 6 years
    Per the OP comments, I think this answer (while correct) doesn't answer the OP question, neither answers mine. My question would be: "when we say that there is a connection established between two hosts, what does that mean? If I could get a magic microscope and inspect the server or the client, and - a-ha! - find the connection, what would I be looking at? Some variable allocated by the OS code? Some entry in some kind of table? How and when does that gets there, and how and when it is removed from there".
  • Kerrek SB
    Kerrek SB about 6 years
    @RafaelEyng: In the most general sense the connection is represented by the state of both peers inside their relevant network stack in the operating system -- state like "port so-and-so on address so-and-so is part of an open connection". Traffic from the network hardware will then be made available for reading from the user-space process in some suitable fashion (e.g. via a file descriptor becoming ready for reading).
  • Suraj Jain
    Suraj Jain almost 6 years
    @KerrekSB How is this question off topic, Can you open it, I had same doubt and many others too, it is genuine conceptual question.
  • Kerrek SB
    Kerrek SB almost 6 years
    @SurajJain: No idea. The criteria for what makes for valid questions have been changing, and they were certainly different seven years ago. I recommend you flag the question for moderator attention, so the decision can be reviewed.
  • Suraj Jain
    Suraj Jain almost 6 years
    @KerrekSB I flagged it, asking for how can I vote for opening it, but I cannot see any link to which I click to vote. Can you flag it??
  • Suraj Jain
    Suraj Jain almost 6 years
    @KerrekSB Also I opened one question stackoverflow.com/questions/51233307/…, because this one was closed.
  • Kerrek SB
    Kerrek SB almost 6 years
    @SurajJain: Looks like it's now closed for a different reason!
  • Suraj Jain
    Suraj Jain almost 6 years
    @KerrekSB Can you write your answer on other question too, it is really good, or you can open this one, I really dont know how are they duplicate.