The difference between TCP protocol and UDP protocol

Link: https://blog.csdn.net/xiaobangkuaipao/article/details/76793702

1. Summary of the differences between TCP and UDP:
1. TCP is connection-oriented (such as dialing up to establish a connection to make a call); UDP is connectionless , that is, it does not need to establish a connection before sending data

2. TCP provides reliable service . That is, data transmitted over a TCP connection is error-free, not lost, not duplicated, and arrives in order; UDP delivers best-effort delivery, i.e. does not guarantee reliable delivery

Tcp realizes reliable transmission through checksum, retransmission control, serial number identification, sliding window, and acknowledgment. For example, when the packet is lost, the retransmission control can also be used to control the sequence of the packets that are out of order.

3. UDP has better real-time performance and higher work efficiency than TCP, and is suitable for high-speed transmission and real-time communication or broadcast communication.

4. Each TCP connection can only be point-to-point; UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication

5. TCP requires more system resources, while UDP requires less system resources.


2. Why is UDP sometimes more advantageous than TCP?

With its advantages of simplicity and fast transmission, UDP has replaced TCP in more and more scenarios, such as real-time games.

(1) The improvement of network speed provides reliable network guarantee for the stability of UDP, and the packet loss rate is very low. If the application layer retransmission is used, the reliability of transmission can be ensured.

(2) In order to realize the reliability of network communication, TCP uses a complex congestion control algorithm and establishes a cumbersome handshake process. Due to the built-in system protocol stack of TCP, it is extremely difficult to improve it.

With TCP, once a packet loss occurs, TCP will cache the subsequent packets, wait for the previous packets to be retransmitted and received, and then continue to send, the delay will become larger and larger, based on UDP's stricter real-time requirements. , using a custom retransmission mechanism, which can minimize the delay caused by packet loss and minimize the impact of network problems on the gameplay.

3. The programming steps of UDP and TCP are also somewhat different, as follows:

TCP: 
The general steps on the server side of TCP programming are: 
  1. Create a socket , use the function socket(); SOCKET SocketListen = socket (AF_INET,SOCK_STREAM, IPPROTO_TCP);
  2. Set the socket properties, use the function setsockopt(); * optional 
  3. Bind IP address, port and other information to the socket, use the function bind(); SOCKET_ERROR =  bind (SocketListen,(const sockaddr*)&addr,sizeof(addr))
  4. Open the monitor and use the function listen(); SOCKET_ERROR == listen (SocketListen,2)
  5. To receive the connection from the client, use the function accept(); SOCKET SocketWaiter = accept (SocketListen,

                                                  _Out_    struct sockaddr *addr

 _Inout_   int *addrlen );
  6. To send and receive data, use the functions send() and recv(), or read() and write(); 
  7. Close the network connection; closesocket(SocketListen);closesocket(SocketWaiter);
  8. Close monitoring;  SOCK_STREAM is connection-oriented, that is, a connection must be established through connect before sending and receiving data each time, while SOCK_DGRAM is the network communication of the User Datagram Protocol, which is connectionless ,Unreliable. The general steps of the TCP programming client are:    1. Create a socket , use the function socket();    2. Set the socket properties, use the function setsockopt();* Optional    3. Bind the IP address, port and other information to the socket, Use the function bind();* Optional    4. Set the IP address and port of the other party to be connected;    5. To connect to the server, use the function connect();    6. To send and receive data, use the functions send() and recv(), Or read() and write();    7. Close the network connection;









int send(
  _In_ SOCKET s, // Which socket to send to, the socket returned by accept.
  _In_  const char *buf,
  _in_   int len,
  _In_  int flags
); due to
send(SocketClient,(const char *)&fh,sizeof(fh),0);

recv(SocketClient,szbuf,sizeof(szbuf),0);
UDP:
与之对应的UDP编程步骤要简单许多,分别如下: 
  UDP编程的服务器端一般步骤是: 
  1、创建一个socket,用函数socket(); 
  2、设置socket属性,用函数setsockopt();* 可选 
  3、绑定IP地址、端口等信息到socket上,用函数bind(); 
  4、循环接收数据,用函数recvfrom(); 
  5、关闭网络连接; 

UDP编程的客户端一般步骤是: 
  1、创建一个socket,用函数socket(); 
  2、设置socket属性,用函数setsockopt();* 可选 
  3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选 
  4、设置对方的IP地址和端口等属性; 
  5、发送数据,用函数sendto(); 
  6、关闭网络连接;

int recvfrom(
  _In_         SOCKET s,       //绑定的socket
  _Out_        char *buf,
  _In_         int len,
  _In_         int flags,
  _Out_        struct sockaddr *from,  //用来接收对方的
  _Inout_opt_  int *fromlen
);
int nres= recvfrom (pThis->m_socketListen,szBuf,sizeof(szBuf),0,(sockaddr*)&addrClient,&nSize);//0处标志位
sendto(m_socketListen,szBuffer,nSize,0,(const sockaddr*)&addr,sizeof(sockaddr_in))
TCP和UDP是OSI模型中的运输层中的协议。TCP提供可靠的通信传输,而UDP则常被用于让广播和细节控制交给应用的通信传输。

4、将socket设置为广播属性
bool optval=true;
setsockopt(m_socketListen,SOL_SOCKET,SO_BROADCAST,(const char *)&optval,sizeof(bool));

5、将Socket设置为非阻塞。
//bool benable=true;
//ioctlsocket(m_socketListen,FIONBIO,(u_long*)&benable);

6、Tcp头,20字节

7、UDP首部,8个字节


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325990917&siteId=291194637