Deep Dive Into TCP/UDP Protocol
Q: Difference between socket VS TCP?
A: Socket are a method of IPC that allow data to be exchanged between applications. Socket connection implies two peer connected with each other, Protocol can be TCP, UDP or others.
Q: TCP establish connection: 3-way handshakes
Q: To establish TCP connection, why not only 2-way handshakes, instead of 3-way handshakes?
A: TCP is a bi-directional communication protocol, which means either end ought to be able to send data reliably.
Q: TCP terminate connection: 4-way handshake
Q: Why 3-way handshakes for establish a TCP connection, but 4-way for terminate a TCP connection?
A: Like SYN-ACK, why not having a FIN-ACK segment?
TCP is a bidirectional and full-duplex protocol.
When establishing bidirectional connection, SYN(request opening the source end) is the natural next step of ACK(acknowledge opening the server end). So combining them as one SYN-ACK is a safe performance tuning.
However when client requests terminating connection, server may have some data haven’t got enough time sending to the client.
Two use cases when client have to terminate a TCP connection.
- Server doesn’t have unsent data. In this case, server can merge FIN(request closing the source end) and ACK(acknowledge closing the server end) as one package.
- Server has unsent data. The data need to be sent to client, before terminating the connection. In this case, FIN and ACK can’t be merged.
Q: After terminating a TCP connection, why socket state would be set to
A: The purpose of
TIME_WAIT is to allow the networking to distinguish packets that arrive as belong to the “old, existing” connection from a new one.
Q: How does TCP sliding window work for congestion control?
Q: TCP VS UDP?
A: Both TCP and UDP are popular transport-layer protocols.
- Reliable or not: TCP is a reliable, bi-directional communication protocol
- Connections: UDP doesn’t need to establish and terminate connections for communication
- UDP permit broadcasting and multicasting; UDP server can receive(and reply to) datagrams from multiple clients
- TCP provide guarantees about in-order delivery