什么是连接?
我对面向连接的 TCP 和无连接的 UDP 感到困惑,所以请有人澄清这一点。
两台计算机之间的每次通信,无论是 TCP 还是 UDP,都是通过数据包进行的。无论是 TCP 还是 UDP,两个对等点之间都没有硬线连接。那么,为什么 TCP 被称为面向连接,只是因为如果没有收到确认,它会重新传输数据包,或者在数据包中嵌入序列号?
连接的实际含义是什么?两个通信对等点之间路径上的路由器是否会预订一段时间来接受该特定连接的数据包?
编辑
- 你什么时候说两点之间建立了连接?
谢谢
I am getting confused between TCP being Connection oriented and UDP being connectionless so please somebody clarify this.
Every communication between two computers whether its TCP or UDP is via packets. There is no hard wire connection between two peers whether its TCP or UDP. Then why TCP is said to be connection oriented just because it retransmits the packets if no acknowledgement is received or it embeds sequence number inside the packets ?
Whats the actual meaning of connection ? Does the routers along the path between two communicating peers booked for some time to accept packets for that particular connection ?
EDIT
- When do you say connection between two points established?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
连接只是两个端点之间的虚拟路径。使用 TCP,您可以打开连接并开始发送数据。它保证按顺序到达另一端(假设网络没有失败)。然后你关闭连接。
在连接期间,两端相互通信,确认收到数据包,以确保没有丢失或重复。
对于 UDP,情况略有不同。基本上,您只是将一个带有目标地址的数据包扔到那里,它可能会也可能不会到达 - 这就是 UDP 中的 U(不可靠)。
您不应该误以为 TCP 连接会导致所有数据包采用相同的物理路径。如有必要,他们将绕过问题区域。
至于您的更新,在发生以下情况后建立连接:
SYN
数据包。SYN-ACK
数据包。ACK
数据包。这是 TCP 的会话建立协议。数据包本身是在标头中设置了
SYN
和/或ACK
标志的正常数据包。关于 TCP(和其他协议)的开创性书籍是 Stevens,请自行获取一本如果你想要一个死树版本,那就用这个——我已经用这个很久了。或者,当然,还有维基百科内容。对于随意询问的人来说,这两本书都相当繁重,但如果您有兴趣深入了解,那么非常值得 - 我个人更喜欢这本书,它在我的书架上与高德纳 (Knuth) 并列。
A connection is simply a virtual pathway between two endpoints. With TCP, you open up the connection and start sending data through. It's guaranteed to arrive at the other end in order (assuming the network doesn't fail). Then you close down the connection.
For the duration of the connection, the two ends talk to each other, acknowledging receipt of packets, to ensure there's no loss or duplication.
With UDP, it's slightly different. You basically just throw a packet out there with a destination address and it may or may not arrive - that's the U in UDP (unreliable).
You shouldn't be lulled into thinking the connection of TCP results in all packets taking the same physical path. They will be routed around problem areas if necessary.
As for your update, the connection is established after the following has happened:
SYN
packet has been sent from the initiator.SYN-ACK
packet.ACK
packet.This is the session establishment protocol for TCP. The packets themselves are normal packets with the
SYN
and/orACK
flags set in the header.The seminal book on TCP (and other protocols) is Stevens, get yourself a copy of this if you want a dead-tree version - I've had this for ages. Or, of course, there's the Wikipedia stuff. Both of these are pretty heavy going for the casual enquirer, but well worth it if you're at all interested in going deeper - my own preference would be for the book, it ranks up there with Knuth on my bookshelf.
是的,TCP 嵌入序列号,并执行大量其他处理来“模拟”面向数据包的无连接网络上的专用点对点连接。
UDP 没有;每个数据报的传送完全独立于任何其他数据报。
Yes, TCP embeds sequence numbers, and does a whole lot of other processing to "simulate" a dedicated point-to-point connection over a packet-oriented, connection-less network.
UDP does not; each datagram is delivered totally independently of any other datagrams.
我先回答你的第二个问题。您所写的事情(“建立路径”)发生在某些网络架构(例如 GSM 语音呼叫)中,但不会发生在互联网中,称为电路交换。通过电路交换,网络基础设施本身可以感知两个端点之间发生的通信。然而,TCP/IP 堆栈被设计为数据包交换。这意味着每个数据包都是传送到另一个端点的单独信息,并且与任何其他数据包(例如发布消息)无关。
因此,IP 堆栈中的较低层协议不保证:
数据包(特别是 UDP),不尝试解决这些问题。另一方面,TCP协议必须摆脱这些问题。它使用确认和数据包序列号来处理它们。在 TCP 中,单个数据包与其他数据包并非无关。因此,双方应协商建立在无状态 IP 上的模拟通信路径。这种协商称为连接。它基本上是在链中的数据包之间建立关系。
I'll answer your second question first. The thing you wrote ("establishing a path") which happens in some network architectures (GSM voice calls, for instance) but not in the Internet is called circuit switching. With circuit switching, the network infrastructure itself is aware of a communication happening between the two endpoints. The TCP/IP stack, however, is designed to be packet switched. It means each packet is a separate piece of information that's delivered to the other endpoint and is unrelated to any other packet (like posting a message).
As a consequence, the lower level protocols in the IP stack do not guarantee that:
UDP in particular, does not try to solve these issues. On the other hand, TCP protocol has to get rid of these issues. It uses acknowledgements and packet sequence numbers to handle them. In TCP, a single packet is not unrelated to other packets. As a result, both parties should negotiate a simulated communication path built on the stateless IP. This negotiation is called a connection. It's basically establishing a relationship between the packets in a chain.
路由器不知道连接。连接是 TCP 逻辑概念,它增加了 UDP 所缺乏的数据包传输的可靠性。然而,路由器只对 IP、TCP、UDP 感兴趣,其他都是 IP 之上的层。路由器路由 IP 数据包,而不考虑它们包含的高级协议。
Routers have no knowledge of the connection. The connection is a TCP logical concept which adds the reliability of packet delivery that UDP lacks. However Routers are only interested in IP, the TCP, UDP whatever else are layers on top of IP. Routers route IP packets without any consideration to the high protocols that they contain.
两个 维基百科的定义对我来说似乎很清楚
我不认为我可以改进这些定义,但让我尝试从套接字编程的角度解释它,理解 socket是TCP/UDP的编程接口。具体来说,如果您对服务器套接字进行编程(例如使用 Java),您会注意到 TCP 和 UDP 的面向连接和无连接特性分别如何影响编程模型。
在基于 TCP 的客户端-服务器应用程序中,在完成任何数据通信之前,必须在客户端套接字与对应于该客户端套接字的服务器套接字之间建立连接。在服务器上,您需要创建一个
ServerSocket
,然后调用accept()
来获取与客户端连接相对应的Socket
。创建一个这样的套接字来与来自任何特定远程客户端的每个连接进行通信(通过实例化 Socket 类来启动)。您可以参考此代码示例了解详细信息。另一方面,如果您对 UPD 进行编程,您的服务器套接字基本上是一个 DatagramSocket 对象,它侦听端口并接收发送到它的所有数据报,并且能够将数据报发送回任何特定客户端的插座。也就是说,一个服务器套接字为所有客户端提供服务,因为任何客户端连接和服务器之间都没有端到端连接。 (请注意,也不需要“
serverSocket#accept()
”步骤。)换句话说,每个客户端和服务器都可以只发送数据报,而不必关心另一个端点是否准备好是否接收数据报。您可以参考此代码示例了解详细信息。The two definitions from Wikipedia seem clear to me
I don't think I can improve on those definitions but let me attempt to explain it from socket programming perspective, understanding that socket is the programming interface for TCP/UDP. Specifically, if you program server sockets, say in Java, you would notice how this connection-oriented and connection-less nature of TCP and UDP respectively affects the programming model.
In a TCP-based client-server application, before any data communication is done, a connection between a client socket with a server socket, corresponding to that client socket, must be established. On the server, you would need to create a
ServerSocket
and then callaccept()
to get aSocket
corresponding to a client's connection. One such socket is created to communicate with every connection from any particular remote client (which is initiated by instantiating theSocket
class). You can refer to this code sample for the detail.On the other hand, if you program UPD, your server socket is basically a
DatagramSocket
object which listens on a port and receives all datagrams sent to it as well as is able to send back datagrams to any particular client's socket. That is, one server socket serving all clients because there is no end-to-end connection between any client connection and the server. (Notice that there is also no "serverSocket#accept()
" step necessary.) In other words, each client and server can just send datagrams around without having to care whether the other end-point is ready to receive the datagrams or not. You can refer to this code sample for the detail.