The most complete difference between network protocol-TCP and UDP is introduced

Source: Internet
Author: User
Tags bind requires socket
The most complete difference between network protocol-TCP and UDP is introduced

Preface: In order to facilitate the late turn look, the public number in slowly finishing the article, about the Java Network Programming content of the article, can be viewed in the menu bar-article Finishing-advanced module.


Basic differences between TCP and UDP
1. Connection-based and no-connection
2.TCP requires more system resources, less UDP;
3.UDP program structure is relatively simple
4. Stream mode (TCP) and datagram Mode (UDP);
5.TCP guaranteed data correctness, UDP may drop packets
6.TCP guaranteed Data order, UDP does not guarantee
  
UDP Application Scenario:
1. Datagram-oriented approach
2. Network data is mostly short message
3. Have a large number of client
4. No special requirements for data security
5. The network burden is very heavy, but the response speed requires high

The difference between specific programming
1.socket () with different parameters
2.UDP server does not need to call listen and accept
Sendto/recvfrom function for 3.UDP transceiver data
4.TCP: Address information is determined at connect/accept
5.UDP: Address information must be specified each time in the Sendto/recvfrom function
Invalid 6.udp:shutdown function

Programming differences
Usually when we talk about network programming, we refer to TCP programming by default, which is to create a socket for TCP communication using the previously mentioned socket function, which we usually fill as sock_stream. That is, the socket (pf_inet, sock_stream, 0), which indicates the creation of a socket for streaming network traffic.
Sock_stream This feature is connection-oriented, that is, each time the data must be connected through connect, but also bidirectional, that is, either party can send and receive data, the protocol itself provides a number of security mechanisms to ensure that it is reliable, orderly, that is, each packet in the order of delivery to the receiving party.

And Sock_dgram This is the user Datagram Protocol Protocol network communication, it is non-connected, unreliable, because the communication between the two parties do not know whether the other party has received data, whether the normal receipt of data. Either party establishes a socket and can send data using sendto, or it can receive data with Recvfrom. There is no concern about whether the other party exists or not, and whether the data is sent. It is characterized by a faster communication speed. We all know that TCP is going through three handshakes, and UDP is not.

Depending on the above, the UDP and TCP programming steps are somewhat different, as follows:


Tcp:

The general steps for the server side of TCP programming are:
1, create a socket, with a function socket ();
2, set the socket properties, with the function setsockopt (); * Optional
3, binding IP address, port and other information to the socket, with the function bind ();
4, open monitoring, with function listen ();
5, receive the connection from the client, with the function accept ();
6, send and receive data, with function Send () and recv (), or read () and write ();
7, close the network connection;
8, shut down monitoring;

The general steps for TCP programming clients are:
1, create a socket, with a function socket ();
2, set the socket properties, with the function setsockopt (); * Optional
3, binding IP address, port and other information to the socket, with the function bind (); * Optional
4, set the IP address and port to connect the other properties;
5, connect the server, with function connect ();
6, send and receive data, with function Send () and recv (), or read () and write ();
7, close the network connection;


Udp:

The corresponding UDP programming steps are much simpler, as follows:
The general steps of the server side of UDP programming are:
1, create a socket, with a function socket ();
2, set the socket properties, with the function setsockopt (); * Optional
3, binding IP address, port and other information to the socket, with the function bind ();
4, the loop receives the data, uses the function recvfrom ();
5, close the network connection;

The general steps for UDP programming clients are:
1, create a socket, with a function socket ();
2, set the socket properties, with the function setsockopt (); * Optional
3, binding IP address, port and other information to the socket, with the function bind (); * Optional
4, set the other IP address and port properties;
5, send data, with function sendto ();
6, close the network connection;

TCP and UDP are the protocols in the transport layer in the OSI model. TCP provides reliable communication transmission, while UDP is often used to transfer broadcast and detail control to the application's communication.


Difference Supplement:

Udp:
UDP does not provide sophisticated control mechanisms, using IP to provide communication services for non-connected. And it is a mechanism for sending data from an application to the network at the instant it is received. Even in the case of network congestion, UDP can not carry out traffic control and other activities to avoid network congestion. In addition, Udo is not responsible for retransmission if a packet loss occurs during transmission. Even when the arrival order of packets has been messed up, there is no correct function. If these detail controls are needed, they have to be handled by the application using Udo. In other words, UDP transfers part of the control to the application, but only provides the most basic functionality as a transport layer protocol. UDP is a bit similar to what the user is saying about what to listen to, but requires the user to take full account of the upper layer protocol type and make the appropriate application.

Tcp:
TCP fully realizes the various control functions of data transmission, can carry out the heavy control of packet loss, and can control the order of the sub-packets. And none of this is in UDP. In addition, TCP acts as a connection-oriented protocol that sends data only when it confirms the presence of a communication peer, which can control the waste of traffic. TCP transmits reliability through such mechanisms as inspection and serial number, acknowledgment response, retransmission control, connection management, and window control.


TCP vs. UDP differences Summary:

1, TCP-oriented connection (such as the phone to dial the first to establish a connection); UDP is not connected, that is, you do not need to establish a connection before sending data
2, TCP provides reliable service. In other words, the data transmitted through the TCP connection is error-free, not lost, not duplicated, and arrives sequentially; UDP does its best to deliver, that is, does not guarantee reliable delivery
3, TCP-oriented byte stream, in fact, TCP data as a series of non-structured byte stream; UDP is message-oriented
UDP does not have congestion control, so network congestion does not reduce the sending rate of the source host (useful for real-time applications such as IP telephony, real-time video conferencing, etc.)
4, each TCP connection can only be point-to; UDP supports one-to-many, multi-pair, many-to-many interactive communication
5, TCP header overhead 20 bytes; UDP has a small header overhead and only 8 bytes
6, the logical communication channel of TCP is full-duplex reliable channel, UDP is unreliable channel


Source: CSDN

Links: http://blog.csdn.net/Li_Ning_/article/details/52117463

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.