Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report