The difference between TCP and UDP in socket programming

Source: Internet
Author: User

First, TCP with the UDP the Difference

Connection-based and no-connection
Requirements for system resources (more TCP, less UDP)
The structure of the UDP program is relatively simple
Flow mode and Datagram mode
TCP guarantees data correctness, UDP may drop packets
TCP guarantees data order, UDP does not guarantee
When some of the following requirements are met, UDP-oriented datagram-based network data is mostly short message
Have a large number of client
No special requirements for data security
The network burden is very heavy, but the response speed requirements are high
Specific programming differences in socket () parameters
UDP server does not need to call listen and accept
Sendto/recvfrom function for UDP transceiver data
TCP: Address information is determined at connect/accept
UDP: Address information must be specified each time in the Sendto/recvfrom function
Invalid Udp:shutdown function

Second, Man ----Socket
By looking at the man Manual of the socket, you can see that the value of the first parameter of the socket function can be the following values:
Name Purpose
Pf_unix, pf_local LOCAL Communication
Pf_inet IPV4 Internet Protocols
Pf_inet6 IPV6 Internet Protocols
PF_IPX Ipx-novell Protocols
Pf_netlink Kernel User Interface Device
pf_x25 ITU-T X.25/ISO-8208 protocol
PF_AX25 Amateur Radio ax.25 protocol
PF_ATMPVC Access to Raw ATM PVCs
Pf_appletalk APPLETALK
Pf_packet Low Level PACKET interface

three, 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:
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;


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;

The difference between TCP and UDP in socket programming

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.