Chapter 7 UDP Data News
7.1 UDP Data News
UDP source port, UDP destination port indication process; UDP datagram length; checksum, optional.
UDP is non-connection, unreliable transmission, has a small transmission delay.
7.2 UDP transmission process
Server: Socket () -> Bind () -> recvfrom () -> sendto () -> close ()
Client: Socket () -> sendto () -> recvfrom () -> close ()
Server-side and TCP protocols have two processes for listen () and Accept (), while the client does not need to establish a connection.
Header file:
Send and receive functions:
Int Sendto (int sockfd, const void * msg, int LEN)
Int Recvfrom (int SockFD, Const Void * Buf, INT LEN, Unsigned Int Flags, Const Struct SockAddr * from, Socklen_t * addrlen); // Parameter from is the address of saving sender
INT Send (int setfd, void * buf, int flag); // Send to the default address after using Connect binding
INT Recv (int SockFD, Void * BUF, INT FLAGS); // Receive from the default address after using the Connect binding
// Socket (AF_INET, SOCK_DGRAM, 0) Create a UDP socket
7.3 Comparison of UDP Server and TCP Server
Servers using UDP protocols are usually unconnected, so there is no use of listen and accept. The UDP server only needs to wait for the client's datagram on its port.
The TCP server needs to connect to the client and then monitors a connection socket for its services; and the UDP server is not connected to the client, and the UDP server is only received, processing and returns the result.
The UDP protocol does not care about the reliability and order of the datagram, and it hopes that the application guarantees these.
In the TCP server, the server can call the GetSockpeer function to obtain the client address information and port number, use the GetSockName to get the IP address and port number corresponding to the socket.
In the UDP server, you can get the source address of the datagram in the RECVFROM function, and get the port of the datagram using GetSockName. If the UDP server has multiple IP addresses, it is impossible to determine which IP address gets the datagram. Because the UDP protocol is not connected to the connection, the receiver's IP address is not recorded. At this time, if you need to know which IP address is not known, you need to bind the server to each built multiple UDP sockets in different networks. On the interface, get the corresponding IP number through GetSockName.
· 7.4 UDP "connection"
The UDP is non-connected, but can also call the Connect function to bind the socket to the default IP address.
After the UDP server calls conncet, does not enable "three handshake", remember only the destination address and port. When using the send function, you will automatically fill in the data header according to the default; you can also send a specified location with the Sendto function.
But when receiving, if the socket has been bound to an address and port, the UDP server receives only the same data report on the upper source address and port. If a data source address is different from the port and the socket setting, discard it. The UDP socket without binding addresses and ports can receive any source of datagram. You can call the Connect function multiple times to modify the binding address and port settings of the UDP socket. If CONNECT (AF_UNSPEC, NULL, 0) can be called, the binding of the socket can be canceled.
· 7.4 UDP application performance improvement
1 Solve the disorderly issues of the message:
Design a data serial number for UDP packets, and receive the packet before sorting and then transferred to the data processing program.
2 Solve the flow control problem of packets:
If the client receives a much higher than the server-side transmission rate, network transmission performance is greatly reduced;
If the client receives a much lower than the server-side delivery rate, the UDP packet will make the server-end resources. Therefore, it is necessary to match the transmission and reception rates of both sides, and use to improve overall performance. We need to establish a end-to-end traffic control feedback mechanism in the application, sending feedback from the client to the server side, allowing the server to dynamically adjust the transmission rate.
Chapter 8 Domain Name System and General Socket Options
· 8.1 domain name system
#include
Struct Hostent * gethostByname (const char * hostname); / / Specify a domain name address to get an IP address
Struct Hostent * gethostByaddr (const char * addr, size_t len, int family); / / Specify the IP address to get domain name
Struct hostent {
Char * h_name; // host name
Char ** h_alias; // host alias list
INT H_ADDRTYPE; / / Host address type
INT h_LENGTH; / / Host address length
Char ** h_addr_list; // Host IP address list
}
INT gethostname (char * name, size_t len); // Return to this domain name address
INT uname (struct utsname * name); //, # include
Struct Server * getServbyName (const char * servname); // Service Name Get Port
Struct server * getServByport (int port, const char * protoname); // Get service information by port name
· 8.2 sets of text options
#include
INT GetSockopt (int Sockfd, int Level, int Optname, void * potval, socklen_t * optlen);
Int setsockopt (int SockFD, int Level, int Optname, COSNT VOID * POTVAL, SOCKLEN_T * OPTLEN);
[Universal socket option, reference manual, slightly. ]
Chapter 8 Domain Name System and General Socket Options
· 8.1 domain name system
#include
Struct Hostent * gethostByname (const char * hostname); / / Specify a domain name address to get an IP address
Struct Hostent * gethostByaddr (const char * addr, size_t len, int family); / / Specify the IP address to get domain name
Struct hostent {
Char * h_name; // host name char ** h_alias; // host alias list
INT H_ADDRTYPE; / / Host address type
INT h_LENGTH; / / Host address length
Char ** h_addr_list; // Host IP address list
}
INT gethostname (char * name, size_t len); // Return to this domain name address
INT uname (struct utsname * name); //, # include
Struct Server * getServbyName (const char * servname); // Service Name Get Port
Struct server * getServByport (int port, const char * protoname); // Get service information by port name
· 8.2 sets of text options
#include
INT GetSockopt (int Sockfd, int Level, int Optname, void * potval, socklen_t * optlen);
Int setsockopt (int SockFD, int Level, int Optname, COSNT VOID * POTVAL, SOCKLEN_T * OPTLEN);
[Universal socket option, reference manual, slightly. ]