# prologue
UDP (User Datagram Protocol) is not connected, it is datagram-oriented, unreliable
# Sockets
Is the IP address and port number
IP address: 4 bytes
Port: 2-byte, i.e. the range from 0 to 65536
Port number into
Well-known port
0--1023: http, ssh, ftp, telnet and some other protocol port numbers are fixed to the operating system can not be assigned
Some fixed port numbers
ssh server, using port 22
ftp server, using port 21
telnet server, using port 23
http server, port 80
https server, using port 443
Operating system dynamically assigned port numbers
Port number of the client-server, this range of port numbers can be assigned operating system
Check the port number
less / etc / Services
// you can view all the port numbers under Linux
IP addresses of understanding:
IP address is used to identify a host
Port number understanding:
Port number is used to tell the operating system that should operate for a process, that is the port number is used to identify a process
A port number can only be occupied by a process, but a process can have multiple port numbers, that is, the process and the port number is one to many relationship
When we write a program using the port number when you want to avoid these well-known port numbers
【problem】
Whether a process can bind multiple port numbers it?
Yes, because a process can open multiple file descriptors, and each descriptor corresponds to a port number, so the number one process can bind multiple ports
Whether or not a port number can be bind multiple processes?
Can not
If a process to bind a port number, and then fork a child process, so to achieve the multiple processes to bind a port number, but different process to bind the same port number is not possible
TIME_WAIT state, the server can not be explained without immediately restart process can not bind the same port number at the same time
Multiple processes can listen to the same port number, please?
can. To create a socket before listening -> bind ip :: port number -> listener. We can use the setsockopt function before bind, set socket options, including REUSEADDR this option indicates that multiple processes can reuse the function specified in bind address and port number
So sockets can accurately identify a process on a host computer, thus completing the communication between computers
Communication between computers:
Another process is a process on the host A and the host B communicate
# Network byte order conversion
For the time of data transmission in the network has a transport rule to follow its own big-endian transmission
For data transmission sequence on the host has two kinds:
Big-endian: i.e., high byte on the low order address
Small end: i.e. low byte on the lower order address
Transmission: the first transmission of data are on the lower address and higher address data on
So for data transmission on the time when the host has transmitted to the network may result in a data error (e.g., a small end, when the host, need to be converted)
Conversion functions:
#include <ARPA / inet.h> uint32_t htonl (uint32_t hostlong); uint16_t the htons (UInt16 hostshort); uint32_t ntohl (uint32_t netlong); uint16_t ntohs (uint16_t netshort); H: indicates that the host host name
n: represents the network network
l: indicates 4 bytes long
s: 2 bytes indicates short
# Address translation function
String into in_addr
in_addr_t inet_addr(const char* strptr)
in_addr into a string
char * inet_ntoa (struct in_addr inaddr)
Has not re-entrant nature, that is not called more than once, because the function yourself open up a space in the static IP address is used to store the string
# UDP protocol
UDP protocol endian format
Illustration: UDP protocol endian format
16 is a UDP length indicating the entire data packet (UDP header + UDP data) of the maximum length (64KB)
And testing: If the checksum error, will be directly discarded (the test is the header and data parts are tested together)
First check value obtained through a special calculation algorithm at the data transmission side, after transmission to the recipient, but also recalculated. If a datagram is a third party during transmission or tampered with since the damage due to line noise, etc., and transmits the checksum calculated value will not match the recipient, whereby the UDP protocol may check whether an error occurred.
Source port number: In the other letter is the choice, either in full is not required 0:00
Destination port number: At the end of timekeeping must be used to deliver
Length: length of the UDP user datagram, the minimum value is 8 (only header)
UDP features
No connection: until the end of the IP and port number is transmitted directly, without establishing a connection
Unreliable: no acknowledgment mechanism, no retransmission mechanisms; because there is no network failure can not be sent to the other side of the segment, UDP protocol layers application layer will not return any error messages
Can not be flexible and the number of times the control reading and writing data: datagram-oriented
Control options less data transmission delay is small, a high data transmission efficiency
Datagram-oriented
How long the application layer to the UDP packet, is transmitted as UDP, neither split nor merge
Example: UDP data transfer 100 bytes
If the sender calls a sendto, sending 100 bytes. Then the receiving end must call the corresponding recvfrom to receive 100 bytes; and can not be recycled 10 times recvfrom call, transmitting 10 bytes per
UDP buffer zone
UDP does not send buffer, after the call directly to the sendto kernel by the kernel-network layer protocol data to the subsequent transfer operation. Preserved because UDP is not connection-oriented, so there is no retransmission mechanism, there is no need to send the buffer zone will have to send the data failed to send retransmission prepare
UDP has a receiving buffer. But this can not guarantee that the receiving buffer received the order consistent with the order of the UDP header and the UDP packets sent; and if the buffer is full, data will be discarded arriving UDP
The UDP Socket both read and can write, full-duplex
Note the use of UDP
UDP protocol header has a maximum length of 16 bits, the maximum length that is capable of transmitting a UDP data is 64K (comprising header UDP). But 64K in today's Internet environment, is a very small number. If we need to transfer data exceeds 64K, the application layer will need to manually sub repeatedly transmitted and assembled at the receiving end
UDP header checksum calculation method of some special. When computing the checksum, to add the pseudo-header of 12 bytes before UDP User Datagram
Neither dummy header do not want the delivery downward transmission, but only to calculate the checksum, and
The IP datagram header checksum and only test different IP datagrams, UDP checksum is the header and data parts are tested together
Pseudo-header:
Illustration: pseudo-header
UDP-based application layer protocol
NFS: Network File System
TFTP: Trivial File Transfer Protocol file
DHCP: Dynamic Host Configuration Protocol
DNS: Domain Name Resolution Protocol
Interview questions: reliable transmission UDP?
Reference reliability mechanisms of TCP, achieve a similar logic in the application layer
Reference sequence number, to ensure that data is sequentially
The introduction of the acknowledgment to ensure that the peer received data
The introduction of retransmission timeout, from time to time if there is no response, it retransmits data
1. For the use of socket function
1.1 function prototype
Socket int (Domain int, int type, int Protocol); Domain: FIELD AF_INET: the IPV4 the AF_INET6: IPV6 type: type SOCK_STREAM SOCK_DGARM Protocol: Protocol action function 1.2
Creating a socket in the communications field is not binding, and returns a file descriptor, can be used in subsequent function calls to operate in a socket
2. For use bind function
2.1 function prototype
int bind (int socket, const struct sockaddr * address, socklen_t address_len);
action function 2.2.
This function uses the previously created good sockets to be bound to the IP address and port number, that is, it indicates that the socket can be identified to determine the hosts in a network and a host of process
3. For use recvfrom function
3.1 function prototype
recvfrom an ssize_t (int Socket, the restrict Buffer void *, size_t length,
int the flags, struct the restrict the sockaddr * address,
the socklen_t the restrict address_len *); Socket: a socket to accept the message buffer: buffer for receiving messages length: length of the received message flags: type of address: sockaddr structure null pointer or transmitting information stored addless_len: sockaddr structure specified address length parameter points to the role of a function of 3.2 3.2 role function
For receiving a message transmitted from the socket to the socket. The structure of the socket sockaddr know
4. For use 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 函数的作用
This function is to receive messages from a socket socket dest_addr
5. expansion of knowledge
5.1 netstat
netstat is used to monitor a TCP / IP network non-critical tool
Syntax: netstat [options]
Function: View network status
Options:
-a, show all Socket connection
-c, maintains a list of network status
-n, use the ip address directly, without passing through the domain name server, which is displayed as numbers
-l, display Socket monitoring of servers, listed only under Socket listening (Listen) state
-p, is displayed using the identification code and the name of the program Socket (PID / Program name)
-t, displays the connection status of TCP transport protocol
-u, display the connection status of the UDP transport protocol
-v, display process execution instruction
-V, Display version information
-x, the connection status display UNIX Transport Protocol
-s, display networking information tables
-h, online help
5.2 pidof
Check the server process id is very aspects
Syntax: pisdof [process name]
Function: The process name, process id view
5.3 scp command
Web-based secure remote file copy command ssh Sign in
Example: To clinet files in your current path to the host IP for the home directory of 192.168.153.140
scp ./clinet [email protected]:/home
5.4 command on the firewall
Start: systemctl start firewalld
Close: systemctl stop firewalld
View status: systemctl status firewalld
Power disabled: systemctl disable firewalld
Power On: systemctl enable firewalld
# Server for UDP writing ideas
Since UDP is connectionless, for communication between the two processes in the same local area network computer, so the process is not required between the two computers connected to the interface need to include UDP is used to know from where the message received where to send the message to.
For local communication
server
Only you need to create a server socket
The socket is bound to a local address (127.0.0.1), port number and a binding (1024--65535) on the line
Bind local address to two processes-process communication for the local computer, the port number and bind to a binding process that is used to send messages to the client to the server, the server can be found
And then accept the message sent by the client
For the client can then process the message to the message processing returns again
Illustration: Server Process
Client
Bind a socket
In order to bind a process can communicate with the server, the message sent last time to let the server know which one he-process communication and process again
The client only need to send a message
Then receiving a message from the client again like, need not be considered to be connected
Illustration: Client Process
Achieved in communication between different hosts on the same local area network
server
And consistent local communication, but the socket is bound to ip address is not the same
Also for the socket to be bound ip address and a port number of the LAN, you do not need to bind the local address (127.0.0.1) a
So in the process of computers under the same local area network can communicate the
Client
No change for the client, is still only need to know the ip and port number of the server on the list
For question # UDP server should be noted
Start the client
When start the client must enter an ip address and port number to the client, the ip address and port number that is to know the client wants to send a message to a server which sends
Start the server
You must give a binding server ip address and port number, which is to be noted that the server is on which a process on the computer
----------------
Disclaimer: This article is CSDN bloggers '_YKitty' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_40399012/java/article/details/85983221
http://www.dj024.com/user/57396.html
http://www.dj024.com/user/57409.html
http://www.dj024.com/user/57411.html
http://www.dj024.com/user/57410.html
http://c.bailitop.com/user/61983
http://c.bailitop.com/user/61983/about
http://c.bailitop.com/user/61987
http://c.bailitop.com/user/61987/about
http://c.bailitop.com/user/61989
http://c.bailitop.com/user/61989/about
http://c.bailitop.com/user/61991
http://c.bailitop.com/user/61991/about
http://www.zhijieketang.com/user/15692
http://www.zhijieketang.com/user/15692/about
http://www.zhijieketang.com/user/15693
http://www.zhijieketang.com/user/15693/about
http://www.zhijieketang.com/user/15695
http://www.zhijieketang.com/user/15695/about
http://www.zhijieketang.com/user/15697