4.13, TCP communication process
1. The difference between TCP and UDP (transport layer protocol)
UDP
: User Datagram Protocol, connectionless-oriented, unicast, multicast, broadcast, datagram-oriented, unreliable:
TCP
Transmission Control Protocol, connection-oriented, reliable, byte stream-based, only supports unicast transmission
UDP | TCP | |
---|---|---|
Whether to create a connection | no connection | connection-oriented |
Is it reliable | Unreliable | reliable |
number of connected objects | One-to-one, one-to-many, many-to-one, many-to-many | Support one-on-one |
way of transmission | Datagram Oriented | stream-oriented |
head overhead | 8 bytes | 20 bytes minimum |
Applicable scene | Real-time applications (video conferencing, live streaming) | Applications with high reliability (file transfer) |
2. TCP communication process
① Server side (the role of passively accepting connections)
- Create a socket for listening
- Listening : Listening for connections from clients
- Socket : This socket is actually a file descriptor
- Bind this listening file descriptor to the local IP and port (the IP and port are the address information of the server)
- The client uses this IP and port when connecting to the server.
- Set up the monitor and
fd
start - Blocking waiting , when a client initiates a connection, unblocks, accepts the client's connection, and will get a communication with the client
套接字(fd)
- communication
- Receive data
- send data
- Communication ends, disconnect
②Client (initiate connection actively)
- create a communication
套接字(fd)
- To connect to the server, you need to specify the server to connect to
IP 和 端口
- The connection is successful, and the client can communicate directly with the server
- Receive data
- send data
- Communication ends, disconnect