Linux Network Programming Notes

UDP (User Datagram Protocol) and TCP (Transmission Control Protocol) have the following differences in the client-side and server-side programming modes and the functions used:

UDP client and server programming modes and functions used:

UDP client:
1. Create socket: Use the `socket()` function to create a UDP socket.
2. Send data: Use the `sendto()` function to send data to the server.
3. Receive data: Use the `recvfrom()` function to receive data from the server.
4. Close the socket: Use the `close()` function to close the socket.

UDP server side:
1. Create socket: Use the `socket()` function to create a UDP socket.
2. Bind address: Use the `bind()` function to bind the socket to a specific local address and port.
3. Receive data: Use the `recvfrom()` function to receive data from the client.
4. Process data: Process and respond to received data.
5. Send data: Use the `sendto()` function to send the response data to the client.
6. Close the socket: Use the `close()` function to close the socket.

TCP client and server programming modes and functions used:

TCP client:
1. Create socket: Use the `socket()` function to create a TCP socket.
2. Establish a connection: Use the `connect()` function to establish a connection with the server.
3. Send data: Use the `send()` function to send data to the server.
4. Receive data: Use the `recv()` function to receive data from the server.
5. Close the socket: Use the `close()` function to close the socket.

TCP server side:
1. Create socket: Use the `socket()` function to create a TCP socket.
2. Bind address: Use the `bind()` function to bind the socket to a specific local address and port.
3. Listen for connections: Use the `listen()` function to start listening for connection requests.
4. Accept the connection: Use the `accept()` function to accept the client's connection request and create a new socket for communicating with the client.
5. Receive data: Use the `recv()` function to receive data from the client.
6. Process data: Process and respond to received data.
7. Send data: Use the `send()` function to send the response data to the client.
8. Close the socket: Use the `close()` function to close the socket.

Summary:
The programming mode of UDP is connectionless and stateless. Each datagram is independent. Use the `sendto()` and `recvfrom()` functions to send and receive data.
The programming mode of TCP is connection-oriented and reliable. Data transmission can only be carried out after a connection is established. Use the `connect()` and `accept()` functions to establish the connection and send and receive data.
There are obvious differences between the two in programming modes and functions used. Choose the appropriate protocol according to specific needs to implement the corresponding functions.

Guess you like

Origin blog.csdn.net/qq_23172985/article/details/131697575