In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the knowledge points of Linux UDP". In the daily operation, I believe many people have doubts about the knowledge points of Linux UDP. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the knowledge points of Linux UDP?" Next, please follow the editor to study!
# preface
UDP (user Datagram Protocol) is unconnected, Datagram-oriented, and unreliable.
# socket is the IP address + port number
IP address: 4 bytes
Port number: 2 bytes, which means the range is 0 bytes 65536
Port numbers are divided into
Well-known port number
The port numbers of some protocols, such as 0Mel 1023Rd httpGen, sshMagol, ftp, telnet, etc., are fixed and cannot be assigned to the operating system.
Some fixed port numbers
Ssh server, using port 22
Ftp server, using port 21
Telnet server, using port 23
Http server, using port 80
Https server, using port 443
Port number dynamically assigned by the operating system
The port number of the client server, which can be assigned by the port number operating system of this range
View port number
Less / etc/services / / you can check all the port numbers under Linux.
Understanding of IP address:
The IP address is used to identify a host
Understanding of port numbers:
The port number is used to tell the operating system which process to operate on, that is, the port number is used to identify a process.
A port number can only be occupied by one process, but a process can have multiple port numbers, that is, an one-to-many relationship between the process and the port number.
When we write a program that uses port numbers, we should avoid these well-known port numbers
[question]
1. Can a process bind multiple port numbers?
Yes, because a process can open multiple file descriptors, and each file descriptor corresponds to a port number, a process can bind multiple port numbers
two。 Can a port number be bind by multiple processes?
No
If a process binds a port number first, and then fork a child process, then multiple processes bind to the same port number, but it is not possible for different processes to bind the same port number.
TIME_WAIT status. The server cannot be restarted immediately, which means that different processes cannot bind the same port number at the same time.
3. Can multiple processes listen to the same port number?
Sure. Create socket-> bind ip:: port number-> listen before listening. We can use the setsockopt function before bind to set socket options, including REUSEADDR, indicating that multiple processes can reuse the address and port number specified in the bind function
So the socket can accurately identify a process on a host, thus completing the communication between computers.
Communication between computers:
One process of host A communicates with another process on host B.
# Network byte order conversion
For data transmission in the network has its own transmission rules for large-end transmission.
There are two kinds of transmission sequences for data on the host:
Big end: that is, the high-order byte order is placed on the low address
Small end: that is, the low-order byte order is placed on the low address
Transmission: all transfer the data at the low address and then the data at the high address
Therefore, when the data on the host is transferred to the network, it may lead to data errors (for example, when the host is small, so it needs to be converted).
Conversion function:
# include uint32_t htonl (uint32_t hostlong); uint16_t htons (uint16 hostshort); uint32_t ntohl (uint32_t netlong); uint16_t ntohs (uint16_t netshort)
H: indicates the host host name
N: represents the network network
L: represents a 4-byte long
S: represents 2-byte short
# address translation function
Convert a string to in_addr
In_addr_t inet_addr (const char* strptr)
Convert in_addr to string
Char* inet_ntoa (struct in_addr inaddr)
It is non-reentrant, that is, it cannot be called multiple times, because the function itself opens up a space in the static zone to store the IP address string
# UDP protocol
UDP protocol side format
Illustration: UDP protocol side format
16 is the UDP length, which represents the maximum length of the entire Datagram (UDP header + UDP data) (64KB)
Checksum: if there is an error in the checksum, it will be discarded directly (to check both the header and the data part)
The check value is first calculated by the data sender through a special algorithm, and then recalculated after it is transmitted to the receiver. If a Datagram is tampered with by a third party during transmission or is damaged due to line noise and other reasons, the check calculation values of the sender and receiver will not match, so the UDP protocol can verify whether there is an error.
Source port number: choose when the other party replies, and use all 0 when you don't need it.
Destination port number: must be used when delivering the report at the end point
Length: the length of the UDP user Datagram, with a minimum value of 8 (header only)
Characteristics of UDP
Connectionless: transfer directly until the IP and port number of the peer, no need to establish a connection
Unreliable: there is no acknowledgement mechanism, no retransmission mechanism; because this segment cannot be sent to the other party without network failure, the UDP protocol layer will not return any error messages to the application layer.
Datagram-oriented: can not flexibly control the number and quantity of data read and written
Less control options, less delay in data transmission and high data transmission efficiency.
Oriented Datagram
The length of the message given to UDP in the application layer will be sent by UDP as is, and will neither be split nor merged.
Example: using UDP to transfer 100byte data
If the sender invokes sendto once, it sends 100 bytes. Then the receiver must also call the corresponding recvfrom once to receive 100 bytes, but cannot call recvfrom 10 times and send 10 bytes each time.
Cache area of UDP
UDP does not send a cache area. After calling sendto, it will directly give it to the kernel, and the kernel will transmit the data to the network layer protocol for subsequent transmission. Because UDP is not connection-oriented, there is no retransmission mechanism, so there is no need to send cache to save the data that has been sent in preparation for retransmission if the transmission fails.
The UDP has a receive buffer area. However, this receiving cache does not guarantee that the order in which UDP packets are received is the same as that in which UDP packets are sent; if the cache area is full, the UDP data arriving will be discarded.
UDP's Socket can read and write, full-duplex
Considerations for the use of UDP
The header of UDP protocol has a maximum length of 16 bits, that is, the maximum length of data that a UDP can transmit is 64K (including UDP header). But 64K is a very small number in today's Internet environment. If the data we need to transmit is more than 64K, we need to manually subpackage at the application layer, send it multiple times, and assemble it at the receiving end.
The calculation method of checksum in the header of UDP is somewhat special. When calculating the checksum, add a 12-byte pseudo header before the UDP user Datagram
The pseudo header neither transmits down nor wants to be delivered, but only to calculate the checksum.
Unlike the checksum of the IP Datagram, which only checks the header of the IP Datagram, the UDP checksum verifies both the header and the data part together.
False head:
Illustration: false head
Application layer Protocol based on UDP
NFS: network file system
TFTP: simple File transfer File Protocol
DHCP: dynamic host configuration protocol
DNS: domain name resolution protocol
Interview question: using UDP to achieve reliable transmission?
Refer to the reliability mechanism of TCP to implement similar logic in the application layer.
Reference serial number to ensure the order of data
Introduce an acknowledgement reply to ensure that the data is received by the peer
Introduce timeout retransmission. If there is no reply after a period of time, the data will be retransmitted.
1. For the use of the socket function
1.1 function prototype
Int socket (int domain, int type, int protocol); domain: domain AF_INET:IPV4 AF_INET6:IPV6type: type SOCK_STREAM SOCK_DGARMprotocol: protocol
1.2 the role of function
Create an unbound socket in the communication domain and return a file descriptor that can be used later in function calls that operate on the socket
two。 For the use of the bind function
2.1 function prototype
Int bind (int socket, const struct sockaddr* address, socklen_t address_len)
2.2. The function of function
This function uses the previously created socket to bind the IP address and port number, which means that the socket can identify a determined host in a network and the processes in the host
3. For the use of the recvfrom function
3.1 function prototype
Ssize_t recvfrom (int socket, void* restrict buffer, size_t length, int flags, struct sockaddr* restrict address, socklen_t* restrict address_len)
Socket: the message to accept the socket buffer: the cache used to receive the message length: the length of the received message flags: type address: null pointer or sockaddr structure storing sending information addless_len: specify the length of the sockaddr structure pointed to by the address parameter
3.2 the role of function
Used to receive messages sent from socket sockets. The sockaddr structure of the socket is also known.
4. For the use of the sendto function
4.1 function prototype
Ssize_t recvfrom (int socket, const void* message, size_t length, int flags, const struct sockaddr* dest_addr, socklen_t* dest_len)
4.2 the role of function
This function is the socket socket that receives messages from dest_addr.
5. Expand knowledge
5.1 netstat
Netstat is a non-important tool for monitoring TCP/IP networks.
Syntax: netstat [option] function: view network status option:-a, display Socket-c of all connections, continuously list network status-n, use ip address directly, not through domain name server, that is, display as number-l, display Socket of server in monitoring, list only Socket-p in Listen state, show identification number and name (PID/Program name)-t of program using Socket Display the connection status of the TCP transport protocol-u, display the connection status of the UDP transport protocol-v, display the instruction execution process-V, display the version information-x, display the connection status of the UNIX transport protocol-s, display the network operation information statistics-h, online help
5.2 pidof
Viewing the server process id is a very important aspect
Syntax: pisdof [process name]
Function: view the process id through the process name
5.3 scp commands
Remote file copy command for network security based on ssh login
Example: to send the clinet file under your current path to the home directory where the host IP is 192.168.153.140
Scp. / clinet root@192.168.153.140:/home
5.4 commands about firewalls
Start: systemctl start firewalld off: systemctl stop firewalld View status: systemctl status firewalld boot disable: systemctl disable firewalld boot enable: systemctl enable firewalld
# ideas for UDP Writing Server
Because UDP is connectionless, for communication between processes of two computers on the same local area network, there is no need for processes between two computers to connect. The interface used by UDP needs to include knowing where to receive messages and where to send messages.
Achieve local communication
Server
All you need is the server to create a socket
Just bind the socket to the local address (127.0.0.1) and bind a port number (1024murmur65535)
Bind the local address to communicate with the two processes on the local computer, and bind the port number to bind a process, so that when the client sends a message to the server, the server can be found.
Then accept the message from the client.
The message from the client is processed and then the processed message can be returned again
Illustration: server proc
Client
Bind a socket
In order to bind a process, you can communicate with the server and let the server know which process is communicating with the other process when sending the message.
The client only needs to send a message to the server
Then just receive the message from the client again. You don't need to think about connecting.
Illustration: client process
Realize the communication between different hosts in the same local area network
Server
Consistent with local communications, except that the ip address bound to the socket is different
Also for sockets to bind the ip address of the local area network and a port number, there is no need to bind the local address (127.0.0.1)
In this way, the processes of computers on the same local area network can communicate.
Client
There is no change for the client, all you need to know is the ip and port number of the server.
# problems to pay attention to for UDP server
Start the client
When starting the client, you must enter an ip address and port number to the client. This ip address and port number means to know which server the client wants to send messages to.
Start the server
You must bind an ip address and port number to the server, that is, pay attention to which process the server is on the computer
At this point, the study of "what are the knowledge points of Linux UDP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.