Detailed code for implementing the UDP protocol using the WinSock API (1)

Source: Internet
Author: User

UDP can be used in many aspects. So what we will introduce to you today is the process of implementing UDP using the WinSock API. First, let's take a look at the basic definition. User Data Protocol (UDP) is a Protocol used to define computer communication for Packet Exchange in an interconnected network environment. It is one of the widely used communication protocols on the Internet. UDP is directly located at the top layer of the IP protocol and belongs to the transport layer protocol. It provides the simplest Protocol mechanism to send information to another user program.

Unlike TCP, UDP is a connectionless protocol, which does not establish a connection between the sender and the receiver. UDP does not provide a data transmission guarantee mechanism, it can be said that it is an unreliable transmission protocol. The UDP protocol cannot ensure the order of data transmission and receipt. In fact, this disorder rarely occurs, it is usually possible only when the network is very crowded. Since UDP has so many shortcomings, what is its significance? In fact, due to these shortcomings of UDP, it has many advantages beyond the reach of TCP. Various Security Protection Functions embedded in the TCP protocol increase the system overhead during execution and severely affect the speed. UDP does not provide a reliable information transmission mechanism, the security and sorting functions are handed over to upper-layer applications, greatly improving the execution speed. The UDP protocol is fast and suitable for network transmission of large-scale data such as videos, audios, and files.

Although there is a huge difference between the UDP protocol and the TCP protocol, the basic steps of programming are similar. The UDP protocol does not include the server and client in the TCP protocol. Compared with the C/S model of the TCP protocol, the communication model of the UDP protocol is more symmetrical. In UDP network communication, different functions can be divided into the sender and acceptor. However, such division is a dynamic division, not an absolute division, when a socket sends data at a certain time point, it is the sender, and when receiving data at another time point, it is the receiver. That is to say, the same socket can be either a sender or an acceptor. In addition, we mentioned that UDP is a connectionless protocol, that is, when we compile a network communication program based on UDP protocol, you do not need to listen on ports, request connections, accept connection requests, or disconnect connections.

The symmetry and non-connection characteristics of UDP protocol determine that implementing UDP-based network communication is much easier than implementing TCP-based network communication in programming. However, in practical applications, due to the reliability and disorder of UDP, upper-layer applications are often required to complete functions such as security and sorting.

The following describes how to use the WinSock API to Implement UDP-based network programming steps and source code.

(1) initialize the communication port.

You can add Windows Sockets support in the program wizard or directly add code:

# Include <afxsock. h>
If (! AfxSocketInit ())
{
AfxMessageBox ("Windows communication port initialization failed! ");
}


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.