Get the App
SLTechnology News&Howtos  ›  Servers  › 

LINUX high-performance network communication blocks UDP data reading and writing of notes.

Shulou Source: shulou.com Published: 2022-06-02 05:36:48 11月20日 Update

2.UDP data read and write

2.1ssize_t recvfrom (int sockfd,const void buf,size_t len,int flags,struct socketaddr src_addr,socklen_t addrlen)

2.2 ssize_t sendto (int sockfd,const void buf,size_t len,int flags,const struct sockaddr* dest_addr,socklen_t addrlen)

Note: recvfrom/sendto system call can also be used for data reading and writing of STREAM-oriented socket. You only need to set the last two parameters to NULL.

3. General data reading and writing

Ssize_t recvmsg (int sockfd,struct msghdr msg,int flags)

Ssize_t sendmsg (int sockfd,struct msghdr msg,int flags)

Struct msghdr {

Void msg_name; / / socket address

Socklen_t msg_namelen; / / socket address length

Struct iovec msg_iov; / / distributed memory blocks

Number of int msg_iovlen; / / distributed memory blocks

Void* msg_control; / / points to the starting position of the auxiliary data

Socklen_t msg_cintrollen; / / size of auxiliary data

Int msg_flags; / / copy the flags parameter in the function and update it during the call

}

Struct iovec {

Void * iov_base; / / memory starting address

Size_t iov_len; / / the length of this memory

}

4. Out-of-band mark

When will out-of-band data arrive?

The transport layer protocol uses out-of-band data (out-of-band,OOB) to send some important data. If the communicating party has important data to notify the other party, the protocol can quickly send the data to the other party. In order to send this data, the protocol generally does not use the same channel as ordinary data, but uses another channel. The socket mechanism of linux system supports low-level protocols to send and receive out-of-band data. But the TCP protocol has no out-of-band data in the real sense. To send important protocols, TCP provides a mechanism called emergency mode (urgent mode). The TCP protocol sets the URG bit in the segment to indicate that it is in emergency mode. The receiver can take special treatment for the emergency mode. It is easy to see that data is not easily blocked in this way, and it is possible to receive data in time by capturing SIGURG signals in our server-side programs. This is exactly what we are asking for.

Int sockatmark (int sockfd)

When 1 is returned, we use the recv call with the MSG_OOB flag to receive out-of-band data

If it doesn't return 0

5. Address information function

A local socket address connected to the socket and a remote socket address

Int getsocketname (int sockfd,struct sockaddr address,socklen_t address_len)

Int getpeername (int sockfd,struct sockaddr address,socklen_t address_len)

6.socket option

Fcntl system call is a general POSIX method for controlling file descriptor properties.

A special method to set the descriptor properties of socket files

Int getsockopt (int sockfd,int level,int option_name,void option_value,socklen_t restrict option_len)

Int setsockopt ()

I won't write this one for the time being.

Tags: Data address memory importance mode system function parameter other party property file method mechanism channel length start auxiliary communication general special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Shulou Technology Redmi vpn