UDP protocol programming in Linux

Source: Internet
Author: User

UDP protocol programming in Linux

Liu Chang, Peng chuwu

(School of electrical and Information Engineering, Hunan University, Changsha, Hunan Province 410082)

  Abstract:Describes the UDP protocol and provides

.
  Keywords:Linux; UDP protocol; TCP/IP protocol; Programming

UDP Protocol Program Based on Linux

Liu Chang, Peng chuwu

(School of Electrical Engineering, Hunan University, Changsha 410082,

China)

  Abstract:UDP protocol is described in detail in the article and

A example program applying to client port and server port is given.
  Key words:Linux; UDP protocol; TCP/IP protocol; Programming

1. User Datagram Protocol (UDP)Introduction
User Data Protocol (UDP) is a simple datagram-oriented transport layer protocol that sends and

Receives a datagram. It is a non-connection protocol, that is, it does not need to establish a connection between the server and the client as TCP.

To work.
UDP is a simple protocol built on the IP protocol, which uses IP datagram to provide a connectionless high efficiency.

. It does not guarantee data reliability, does not re-transmit, and has a short latency. It is suitable for scenarios with high real-time requirements and does not require data.

Absolutely reliable applications. In network communication quality

UDP is highly efficient, which is suitable for applications that transmit a small number of packets. Its reliability is determined by the Application

To ensure that, for example, a ECHO is returned to the source after receiving the signal, timeout resend, Data check, and other functions need to be implemented by the application.

Now. UDP does not provide throttling. It sends data at the sender's rate regardless of the buffer size of the receiver.

This vulnerability may easily cause overflow errors. Because UDP does not have a connection between the client and the server, it can use the same socket

The same destination sends a datagram. Similarly, UDP can be bidirectional, so you can also use the same socket

.

2Socket programming Basics
In Linux, network programming is performed through sockets. The following describes the basic sockets used for UDP datagram programming.

Function:
A network program calls several other functions through a socket and returns a socket descriptor for communication. Linux applications

When a program executes any form of I/O, the program reads or writes a file descriptor. Therefore, we can

The created socket descriptor is treated as the descriptor of a common file and can be read and written to the socket descriptor.

Data exchange between networks is a benefit of Linux Device independence.
(1) int socket (INT domain, int type, int Protocol)
The socket () function is used to create a socket descriptor. The parameter domain indicates the communication between the host where the network program is located.

Protocols (such as afunix and afinet ). Afunix can only be used in a single UNIX System

Processes communicate with each other, while afinet is for the internet. Therefore, communication between remote hosts is allowed.

Generally, it is assigned as afinet. Parameter type indicates the socket type created. The corresponding parameter value is sockdgram: datagram socket, indicating that the UDP protocol is used to provide unordered, unreliable, and connectionless communication.

. The Protocol parameter, because the type is specified, is generally replaced by 0 here.

Socket ()

Obtain the socket Descriptor and make basic preparations for network communication. When the call is successful, the file descriptor is returned.

-1 will be returned when the error fails. You can view the error file to learn the error details.
(2) int BIND (INT sockfd, struct sockaddr * myaddr, int addrlen)
After obtaining the socket descriptor, bind the set interface with a certain port number on the machine. Sockfd is used to call socket

The file descriptor returned by the function; addrlen is the length of the sockaddr structure: myaddr is

ADDR

Structure pointer, which stores the address (port and IP address) information of the local socket. However, due to system compatibility

Generally, this structure is not used, instead of another structure (struct sockaddrin ).

. When the BIND () function is successfully called, 0 is returned. If an error occurs,-1 is returned.
The structure type struct sockaddrin stores the address information of the Set interface, which is defined as follows:
Struct sockaddrin {short int sinfamily;/* address family */
Unsigned short int sinport;/* Port Number */
Struct inaddr sinaddr;/* IP Address */
Unsigned char sinzero [8];}/* fill 0 to keep the sockaddr with struct

Same size */
This data structure makes it easier to use each element. sinzero (it is used to fill the sockaddrin structure with the same length as the sockaddr structure) should use bzero () or memset () the function sets it to 0.

. In addition, a pointer pointing to the sockaddrin data structure can be forcibly converted into a pointer pointing to the data structure s

Ockaddr

And vice versa.
(3) int close (INT sockfd)
Function close is used to close a socket descriptor. The sockfd parameter is the socket descriptor to be closed. The function clo

Se ()

0 is returned when execution is successful. Otherwise,-1 is returned.
The first two functions must contain the header file # include <sys/types. h> and # include <sys/socke

T.

H>, the last one contains # include <unistd. h> 〉. Two functions for receiving and sending UDP are introduced, including

Header file # include <sys/socket. h> 〉:
Int sendto (INT sockfd, const void * MSG, int Len, unsigned int flags, struct
 
3Use UDPDatagram Programming
UDP has its own communication model. First, the UDP server calls the function socket to create a datagram socket.

The server then calls the BIND function to bind it to a default UDP port, and then calls the function recvfrom in the specified

Wait for the arrival of UDP client datagram on the port. The UDP client also calls the function socket to create a datagram class.

And then call the sendto function to send a datagram to the UDP server. Note that UDP client applications

The program usually does not need to call the function bind to bind the socket to a fixed port.

Configure an idle port number. After the UDP server process receives the datagram from the client

Return. After processing the datagram, call the sendto function to return the result to the client.
Generally, the design and implementation of the client is easier than that of the server. A typical UDP Server communicates with the operating system.

Interaction, and most of them need to process multiple customers at the same time. A udp client directly communicates with a single server after it is started

And then it is over. For the server, it is in sleep state after startup, waiting for the arrival of customer requests. Target customers

When user data is reported, the server wakes up, and the datagram may contain some form of request message from the customer. I

Basic UDP communication process 1 is shown.

Finally, a server-side instance subroutine is provided, which can be applied to the client with a slight Transformation:
 
 
 4Conclusion
This article introduces the communication mechanism of UDP socket. UDP is a simple datagram-oriented transport layer protocol. Recommended Network Environment

Good. UDP is required for resources. For example, many real-time systems generally use UDP protocol for data.

. If you want

UDP

Sorts and controls the data packets, and ensures the reliability of the data packets,

You must add some control mechanisms in the application.

References

[1] Neil Matthew Richard stones. Beginning Linux programming, 2 ndedition [Z]. wrox Press, 2002.
[2] Alessandro Rubini & Jonathan Corbet. Linux Device Driver [M]. Beijing: China

Power Press, 2002.
[3] skynight studio. Linux Network Programming Technology [M]. Beijing: People's post and telecommunications Press, 2001.
[4] Zhang Wei. Linux Network Programming Tutorial [M]. Beijing: Beijing hope electronics Publishing House, 2002.

Instrumentation Technology

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.