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:
TCPTransmission 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)

  1. Create a socket for listening
    • Listening : Listening for connections from clients
    • Socket : This socket is actually a file descriptor
  2. 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.
  3. Set up the monitor and fdstart
  4. Blocking waiting , when a client initiates a connection, unblocks, accepts the client's connection, and will get a communication with the client套接字(fd)
  5. communication
    • Receive data
    • send data
  6. Communication ends, disconnect

②Client (initiate connection actively)

  1. create a communication套接字(fd)
  2. To connect to the server, you need to specify the server to connect to IP 和 端口
  3. The connection is successful, and the client can communicate directly with the server
    • Receive data
    • send data
  4. Communication ends, disconnect

Guess you like

Origin blog.csdn.net/z2812470857/article/details/130173400