加入收藏
举报
当前仅显示指定条件回帖 [ 展开查看全部 ]
02-14 20:03
#
文件名称:
net.md
所在目录:
Openharmony v1.0 / docs / docs-en / api / api-SmartVision-Devices
文件大小:
163.37 KB
下载地址:
fenwii/OpenHarmony
   
免责声明:本网站仅提供指向 GitHub 上的文件的链接,所有文件的版权归原作者所有,本网站不对文件内容的合法性、准确性或安全性承担任何责任。
文本预览:
# NET
## **Overview**
Provides functions and data structures related to network operations.
**Since:**
1.0
**Version:**
1.0
## **Summary**
## Files



























File Name


Description


inet.h


Provides functions and data structures related to network operations.


ifaddrs.h


Provides functions and data structures related to network operations.


if.h


Provides functions and data structures related to network operations. For example, you can use the functions to send and receive network data, manage network addresses, and convert bytes.


netdb.h


Provides functions and data structures related to network operations. For example, you can use the functions to send and receive network data, manage network addresses, and convert bytes.


ether.h


Provides functions and data structures related to network operations. For example, you can use the functions to send and receive network data, manage network addresses, and convert bytes.


resolv.h


Provides functions and data structures related to network operations. For example, you can use the functions to send and receive network data, manage network addresses, and convert bytes.


socket.h


Provides functions and data structures related to network operations.



## Data Structures


















Data Structure Name


Description


ifaddrs


Defines the network interface information.


if_nameindex


Defines the network adapter information.


hostent


Describes the host name and address information.


sockaddr


Describes the socket address information.



## Macros












Macro Name and Value


Description


h_addr h_addr_list[0]


Defines the first address in h_addr_list for backward compatibility.


SOL_SOCKET 1


Defines options for socket level.



## Functions







































































































































Function Name


Description


htonl (uint32_t n)


uint32_t


Converts an integer from the host byte order to the network byte order.


htons (uint16_t n)


uint16_t


Converts a 16-bit integer from the host byte order to the network byte order.


ntohl (uint32_t n)


uint32_t


Converts an integer from the network byte order to the host byte order.


ntohs (uint16_t n)


uint16_t


Converts a 16-bit integer from the network byte order to the host byte order.


inet_addr (const char *p)


in_addr_t


Converts a string from the IPv4 numbers-and-dots notation to the binary data in network byte order.


inet_network (const char *p)


in_addr_t


Converts a string from the IPv4 numbers-and-dots notation to the binary data in host byte order.


inet_ntoa (struct in_addr in)


char *


Converts a network address to a string in dotted-decimal format.


inet_pton (int af, const char *__restrict s, void *__restrict a0)


int


Converts a string to a network address in the specified address family.


inet_ntop (int af, const void *restrict a0, char *restrict s, socklen_t l)


const char *


Converts a network address in the specified address family to a string.


inet_aton (const char *s0, struct in_addr *dest)


int


Converts an IP address from the string format to the 32-bit binary format in network byte order.


inet_makeaddr (in_addr_t n, in_addr_t h)


struct in_addr


Converts the network number and host address to the network address.


inet_lnaof (struct in_addr in)


in_addr_t


Converts an IP address into a host ID in host byte order without network bits.


inet_netof (struct in_addr in)


in_addr_t


Extracts the network number from the in_addr structure and converts it to the host byte order.


freeifaddrs (struct ifaddrs *ifp)


void


Frees the network interface information obtained by getifaddrs.


if_freenameindex (struct if_nameindex *idx)


void


Frees the network adapter information obtained by if_nameindex.


setprotoent (int stayopen)


void


Opens a connection to the database and sets the next entry to the first entry.


getprotoent (void)


struct protoent *


Retrieves the current protocol information.


getprotobyname (const char *name)


struct protoent *


Retrieves the information about a specified protocol.


getprotobynumber (int num)


struct protoent *


Retrieves the information about a protocol with the specified number.


herror (const char *msg)


void


Prints error information.


hstrerror (int ecode)


const char *


Retrieves error information associated with the specified error number.


ether_ntoa (const struct ether_addr *p_a)


char *


Converts binary data in network byte order into a standard 48-bit Ethernet host address in the colon hexadecimal notation.


ether_aton (const char *x)


struct ether_addr *


Converts a standard 48-bit Ethernet host address in the colon hexadecimal notation into binary data in network byte order.


ether_ntoa_r (const struct ether_addr *p_a, char *x)


char *


Converts binary data in network byte order into a standard 48-bit Ethernet host address in the colon hexadecimal notation. This function is reentrant.


ether_aton_r (const char *x, struct ether_addr *p_a)


struct ether_addr *


Converts a standard 48-bit Ethernet host address in the colon hexadecimal notation into binary data in network byte order. This function is reentrant.


dn_comp (const char *src, unsigned char *dst, int space, unsigned char **dnptrs, unsigned char **lastdnptr)


int


Compresses a network domain name.


dn_expand (const unsigned char *base, const unsigned char *end, const unsigned char *src, char *dest, int space)


int


Expands a compressed domain name to a full domain name.


socket (int domain, int type, int protocol)


int


Creates a socket and returns its descriptor.


shutdown (int sockfd, int how)


int


Shuts down a socket.


bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen)


int


Binds a local protocol address to a socket.


connect (int sockfd, const struct sockaddr *addr, socklen_t addrlen)


int


Initiates a connection to a socket.


listen (int sockfd, int backlog)


int


Listens for network connections.


accept (int sockfd, struct sockaddr *__restrict addr, socklen_t *__restrict addrlen)


int


Accepts incoming connection requests.


getsockname (int fd, struct sockaddr *restrict addr, socklen_t *restrict len)


int


Retrieves the local address of the specified socket.


getpeername (int fd, struct sockaddr *restrict addr, socklen_t *restrict len)


int


Retrieves the peer address of the specified socket.


send (int fd, const void *buf, size_t len, int flags)


ssize_t


Sends data to another socket.


recv (int fd, void *buf, size_t len, int flags)


ssize_t


Receives data from another socket.


sendto (int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)


ssize_t


Sends data to another socket.


recvfrom (int fd, void *__restrict buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)


ssize_t


Receives data from a specified socket.


sendmsg (int fd, const struct msghdr *msg, int flags)


ssize_t


Sends data to another socket.


recvmsg (int fd, struct msghdr *msg, int flags)


ssize_t


Receives data from a specified socket.


getsockopt (int fd, int level, int optname, void *__restrict optval, socklen_t *__restrict optlen)


int


Retrieves the socket options.


setsockopt (int fd, int level, int optname, const void *optval, socklen_t optlen)


int


Sets the socket options.



## **Details**
## **Function Documentation**
## accept\(\)
```
int accept (int sockfd, struct [sockaddr](sockaddr.md) *__restrict addr, socklen_t *__restrict addrlen )
```
**Description:**
Accepts incoming connection requests.
**Parameters:**















Name


Description


sockfd Indicates the socket descriptor.
addr Indicates the pointer to the socket address requiring for the connection.
addrlen Indicates the length of the address structure pointed to by addr.

**Returns:**
Returns a valid socket if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
























errno


Description


EINVAL


Invalid input parameter


EOPNOTSUPP


The socket requiring for the connection is not of the NETCONN_TCP type.


EWOULDBLOCK


Failure to receive events in non-blocking mode.


ENFILE


Failure to creat a new socket.


ECONNABORTED


Failure to receive the remote information.


ENOMEM


Memory copy exception.



## bind\(\)
```
int bind (int sockfd, const struct [sockaddr](sockaddr.md) * addr, socklen_t addrlen )
```
**Description:**
Binds a local protocol address to a socket.
**Parameters:**















Name


Description


sockfd Indicates the socket descriptor.
addr Indicates the address to be bound to the socket.
addrlen Indicates the length of the address structure pointed to by addr.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.









errno


Description


EINVAL


Invalid input parameter



## connect\(\)
```
int connect (int sockfd, const struct [sockaddr](sockaddr.md) * addr, socklen_t addrlen )
```
**Description:**
Initiates a connection to a socket.
**Parameters:**















Name


Description


sockfd Indicates the socket descriptor.
addr Indicates the pointer to the address to be connected.
addrlen Indicates the length of the address structure pointed to by addr.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.





















errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


EIO


Incorrect connection type obtained based on the socket descriptor


ECONNREFUSED


Restart due to connection failure


ECONNABORTED


Shut-down due to connection failure



## dn\_comp\(\)
```
int dn_comp (const char * src, unsigned char * dst, int space, unsigned char ** dnptrs, unsigned char ** lastdnptr )
```
**Description:**
Compresses a network domain name.
To reduce the size of DNS messages, domain names in the messages are compressed. For details about the compression scheme, see RFC 1035.
**Parameters:**





















Name


Description


src Indicates the pointer to the domain name to be compressed.
dst Indicates the pointer to the compressed domain name.
space Indicates the size of the compressed domain name.
dnptrs Indicates the pointer to an array of pointers to previously compressed domain names. If the value is NULL, the current domain name will not be compressed.
lastdnptr Indicates the pointer to the last element of the array. If the value is NULL, the array is not updated.

**Returns:**
Returns the length of the compressed domain name if the operation is successful; returns **-1** otherwise.
## dn\_expand\(\)
```
int dn_expand (const unsigned char * base, const unsigned char * end, const unsigned char * src, char * dest, int space )
```
**Description:**
Expands a compressed domain name to a full domain name.
The compressed domain name is contained in a query or reply message.
**Parameters:**





















Name


Description


base Indicates the pointer to the start of the DNS message body.
end Indicates the pointer to the end of the DNS message body.
src Indicates the pointer to the compressed domain name.
dest Indicates the pointer to the expanded domain name.
space Indicates the memory size of the dest address.

**Returns:**
Returns the length of the compressed domain name if the operation is successful; returns **-1** otherwise.
## ether\_aton\(\)
```
struct ether_addr* ether_aton (const char * x)
```
**Description:**
Converts a standard 48-bit Ethernet host address in the colon hexadecimal notation into binary data in network byte order.
**Parameters:**









Name


Description


x Indicates the pointer to the host address to be converted.

**Returns:**
Returns the binary data if the operation is successful; returns **0** otherwise.
## ether\_aton\_r\(\)
```
struct ether_addr* ether_aton_r (const char * x, struct ether_addr * p_a )
```
**Description:**
Converts a standard 48-bit Ethernet host address in the colon hexadecimal notation into binary data in network byte order. This function is reentrant.
This function is used in multitasking scenarios.
**Parameters:**












Name


Description


x Indicates the pointer to the host address to be converted.
p_a Indicates the pointer to the temporary buffer during conversion.

**Returns:**
Returns the binary data represented by **ether\_addr** if the operation is successful; returns **0** otherwise.
## ether\_ntoa\(\)
```
char* ether_ntoa (const struct ether_addr * p_a)
```
**Description:**
Converts binary data in network byte order into a standard 48-bit Ethernet host address in the colon hexadecimal notation.
**Parameters:**









Name


Description


p_a Indicates the pointer to the binary data to be converted.

**Attention:**
Different from glibc \(for example, 1:2:3:4:5:f\), 0 is prefixed and letters are capitalized \(for example, 01:02:03:04:05:0F\).
**Returns:**
Returns the host address if the operation is successful; returns **0** otherwise.
## ether\_ntoa\_r\(\)
```
char* ether_ntoa_r (const struct ether_addr * p_a, char * x )
```
**Description:**
Converts binary data in network byte order into a standard 48-bit Ethernet host address in the colon hexadecimal notation. This function is reentrant.
This function is used in multitasking scenarios.
**Parameters:**












Name


Description


p_a Indicates the pointer to the binary data to be converted.
x Indicates the pointer to the temporary buffer during conversion.

**Attention:**
Different from glibc \(for example, 1:2:3:4:5:f\), 0 is prefixed and letters are capitalized \(for example, 01:02:03:04:05:0F\).
**Returns:**
Returns the host address if the operation is successful; returns **0** otherwise.
## freeifaddrs\(\)
```
void freeifaddrs (struct [ifaddrs](ifaddrs.md) * ifp)
```
**Description:**
Frees the network interface information obtained by **getifaddrs**.
**Parameters:**









Name


Description


ifp Indicates the pointer to the network interface information to be freed.

## getpeername\(\)
```
int getpeername (int fd, struct [sockaddr](sockaddr.md) *restrict addr, socklen_t *restrict len )
```
**Description:**
Retrieves the peer address of the specified socket.
**Parameters:**















Name


Description


fd Indicates the socket descriptor.
addr Indicates the pointer to the address to be retrieved.
len Indicates the pointer to the length of the address pointed to by addr.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.















errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


ENOMEM


Memory copy exception



## getprotobyname\(\)
```
struct protoent* getprotobyname (const char * name)
```
**Description:**
Retrieves the information about a specified protocol.
**Parameters:**









Name


Description


addr Indicates the pointer to the protocol name.

**Attention:**
The **/etc/protocols** configuration file is not supported. Only the following built-in protocols are supported: ip/icmp/igmp/ggp/ipencap/st/tcp/egp/pup/udp/hmp/xns-idp/rdp/iso-tp4/xtp/ ddp/idpr-cmtp/ipv6/ipv6-route/ipv6-frag/idrp/rsvp/gre/esp/ah/skip/ipv6-icmp/ ipv6-nonxt/ipv6-opts/rspf/vmtp/ospf/ipip/encap/pim/raw \(This protocol does not have the **aliases** attribute, that is, **aliases** is empty.\)
**Returns:**
Returns the protocol information if the operation is successful; returns **NULL** otherwise.
## getprotobynumber\(\)
```
struct protoent* getprotobynumber (int num)
```
**Description:**
Retrieves the information about a protocol with the specified number.
**Parameters:**









Name


Description


num Indicates the protocol number.

**Attention:**
The **/etc/protocols** configuration file is not supported. Only the following built-in protocols are supported: ip/icmp/igmp/ggp/ipencap/st/tcp/egp/pup/udp/hmp/xns-idp/rdp/iso-tp4/xtp/ ddp/idpr-cmtp/ipv6/ipv6-route/ipv6-frag/idrp/rsvp/gre/esp/ah/skip/ipv6-icmp/ ipv6-nonxt/ipv6-opts/rspf/vmtp/ospf/ipip/encap/pim/raw \(This protocol does not have the **aliases** attribute, that is, **aliases** is empty.\)
**Returns:**
Returns the protocol information if the operation is successful; returns **NULL** otherwise.
## getprotoent\(\)
```
struct protoent* getprotoent (void )
```
**Description:**
Retrieves the current protocol information.
**Attention:**
The **/etc/protocols** configuration file is not supported. Only the following built-in protocols are supported: ip/icmp/igmp/ggp/ipencap/st/tcp/egp/pup/udp/hmp/xns-idp/rdp/iso-tp4/xtp/ ddp/idpr-cmtp/ipv6/ipv6-route/ipv6-frag/idrp/rsvp/gre/esp/ah/skip/ipv6-icmp/ ipv6-nonxt/ipv6-opts/rspf/vmtp/ospf/ipip/encap/pim/raw \(This protocol does not have the **aliases** attribute, that is, **aliases** is empty.\)
**Returns:**
Returns the protocol information if the operation is successful; returns **NULL** otherwise.
## getsockname\(\)
```
int getsockname (int fd, struct [sockaddr](sockaddr.md) *restrict addr, socklen_t *restrict len )
```
**Description:**
Retrieves the local address of the specified socket.
**Parameters:**















Name


Description


fd Indicates the socket descriptor.
addr Indicates the pointer to the address to be retrieved.
len Indicates the pointer to the length of the address pointed to by addr.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.















errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


ENOMEM


Memory copy exception



## getsockopt\(\)
```
int getsockopt (int fd, int level, int optname, void *__restrict optval, socklen_t *__restrict optlen )
```
**Description:**
Retrieves the socket options.
**Parameters:**





















Name


Description


fd Indicates the socket descriptor.
level Indicates the protocol level at which the option resides. Only SOL_SOCKET is supported.
optname Indicates the socket options to be retrieved, which has the following values:
optval Indicates the pointer to the option data.
optlen Indicates the pointer to the size of the buffer pointed to by optval.

























value


Description


SO_BROADCAST


Permits sending broadcast messages.


SO_RCVTIMEO


Receiving data times out.


SO_RCVBUF


Sets or gets the maximum socket receiving buffer in bytes.


SO_TYPE


Gets the socket type.


SO_DONTROUTE


Prohibits messages from being sent via a gateway.


SO_BINDTODEVICE


Binds this socket to a particular device.



**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.





















errno


Description


EINVAL


Small optlen


EBADF


Invalid socket or socket status exception


EFAULT


The input parameter is a null pointer.


ENOBUFS


Retrieved length exception


ENOMEM


Memory copy exception



## herror\(\)
```
void herror (const char * msg)
```
**Description:**
Prints error information.
**Parameters:**









Name


Description


msg Indicates the pointer to the string to be printed together with the error information. This parameter can be null.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.












errno


Description


EINVAL


Incorrect symbols or parameters


EOVERFLOW


Long item for printing



## hstrerror\(\)
```
const char* hstrerror (int ecode)
```
**Description:**
Retrieves error information associated with the specified error number.
**Parameters:**









Name


Description


ecode Indicates the error number.

**Returns:**
Returns the string representing the error information.
## htonl\(\)
```
uint32_t htonl (uint32_t n)
```
**Description:**
Converts an integer from the host byte order to the network byte order.
**Parameters:**









Name


Description


n Indicates the integer in host byte order.

**Returns:**
Returns the original data if the system is in big-endian mode; returns the converted data otherwise.
## htons\(\)
```
uint16_t htons (uint16_t n)
```
**Description:**
Converts a 16-bit integer from the host byte order to the network byte order.
**Parameters:**









Name


Description


n Indicates the integer in host byte order.

**Returns:**
Returns the original data if the system is in big-endian mode; returns the converted data otherwise.
## if\_freenameindex\(\)
```
void if_freenameindex (struct [if_nameindex](if_nameindex.md) * idx)
```
**Description:**
Frees the network adapter information obtained by [if\_nameindex](if_nameindex.md).
**Parameters:**









Name


Description


idx Indicates the pointer to the network adapter to be freed.

## inet\_addr\(\)
```
in_addr_t inet_addr (const char * p)
```
**Description:**
Converts a string from the IPv4 numbers-and-dots notation to the binary data in network byte order.
**Parameters:**









Name


Description


p Indicates the pointer to the string to be converted.

**Returns:**
Returns the converted data if the operation is successful; returns **-1** otherwise.
## inet\_aton\(\)
```
int inet_aton (const char * s0, struct in_addr * dest )
```
**Description:**
Converts an IP address from the string format to the 32-bit binary format in network byte order.
**Parameters:**












Name


Description


s0 Indicates the pointer to the IP address to be converted.
dest Indicates the pointer to the structure for conversion.

**Returns:**
Returns **1** if the operation is successful; returns **0** otherwise.
## inet\_lnaof\(\)
```
in_addr_t inet_lnaof (struct in_addr in)
```
**Description:**
Converts an IP address into a host ID in host byte order without network bits.
**Parameters:**









Name


Description


in Indicates the network address structure.

**Returns:**
Returns the converted host address.
## inet\_makeaddr\(\)
```
struct in_addr inet_makeaddr (in_addr_t n, in_addr_t h )
```
**Description:**
Converts the network number and host address to the network address.
**Parameters:**












Name


Description


n Indicates the network number.
h Indicates the host address.

**Returns:**
Returns the converted network address.
## inet\_netof\(\)
```
in_addr_t inet_netof (struct in_addr in)
```
**Description:**
Extracts the network number from the **in\_addr** structure and converts it to the host byte order.
**Parameters:**









Name


Description


in Indicates the network address structure.

**Returns:**
Returns the converted network number.
## inet\_network\(\)
```
in_addr_t inet_network (const char * p)
```
**Description:**
Converts a string from the IPv4 numbers-and-dots notation to the binary data in host byte order.
**Parameters:**









Name


Description


p Indicates the pointer to the string to be converted.

**Returns:**
Returns the converted data if the operation is successful; returns **-1** otherwise.
## inet\_ntoa\(\)
```
char* inet_ntoa (struct in_addr in)
```
**Description:**
Converts a network address to a string in dotted-decimal format.
**Parameters:**









Name


Description


in Indicates the network address to be converted.

**Returns:**
Returns the string.
## inet\_ntop\(\)
```
const char* inet_ntop (int af, const void *restrict a0, char *restrict s, socklen_t l )
```
**Description:**
Converts a network address in the specified address family to a string.
**Parameters:**


















Name


Description


af Indicates the address family. Currently, only AF_INET and AF_INET6 are supported.
a0 Indicates the pointer to the data to be converted.
s Indicates the pointer to the string to be converted.
l Indicates the size of the string to be converted.

**Returns:**
Returns the converted string if the operation is successful; returns **NULL** and sets **errno** to a value in the following table if the operation fails.












errno


Description


ENOSPC


Insufficient space for conversion


EAFNOSUPPORT


Unsupported address family



## inet\_pton\(\)
```
int inet_pton (int af, const char *__restrict s, void *__restrict a0 )
```
**Description:**
Converts a string to a network address in the specified address family.
**Parameters:**















Name


Description


af Indicates the address family. Currently, only AF_INET and AF_INET6 are supported.
s Indicates the pointer to the string to be converted.
a0 Indicates the pointer to the converted data.

**Returns:**
Returns **1** if the operation is successful; returns **0** if the input address family is invalid; returns **-1** if the operation fails. The error codes are set as follows:









errno


Description


EAFNOSUPPORT


Unsupported address family



## listen\(\)
```
int listen (int sockfd, int backlog )
```
**Description:**
Listens for network connections.
**Parameters:**












Name


Description


sockfd Indicates the socket descriptor.
backlog Indicates the maximum length of the queue to accept incoming connection requests.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.















errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


EIO


Incorrect connection type obtained based on the socket descriptor



## ntohl\(\)
```
uint32_t ntohl (uint32_t n)
```
**Description:**
Converts an integer from the network byte order to the host byte order.
**Parameters:**









Name


Description


n Indicates the integer in network byte order.

**Returns:**
Returns the original data if the system is in big-endian mode; returns the converted data otherwise.
## ntohs\(\)
```
uint16_t ntohs (uint16_t n)
```
**Description:**
Converts a 16-bit integer from the network byte order to the host byte order.
**Parameters:**









Name


Description


n Indicates the integer in network byte order.

**Returns:**
Returns the original data if the system is in big-endian mode; returns the converted data otherwise.
## recv\(\)
```
ssize_t recv (int fd, void * buf, size_t len, int flags )
```
**Description:**
Receives data from another socket.
**Parameters:**


















Name


Description


fd Indicates the socket descriptor.
buf Indicates the pointer to a buffer for storing the data to be received.
len Indicates the length of the buffer pointed to by buf.
flags Indicates the socket flags. This parameter has the following values:
















value


Description


MSG_DONTWAIT


Enables the non-blocking operation.


MSG_MORE


The caller has more data to send.


MSG_NOSIGNAL


No signals are generated.



**Returns:**
Returns the length of the received data if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.



























errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


ENOMEM


Memory copy exception


EOPNOTSUPP


Unsupported message type


ENOTCONN


Socket not connected


EWOULDBLOCK


Blocking operation


ENOTCONN


Connection closed



## recvfrom\(\)
```
ssize_t recvfrom (int fd, void *__restrict buf, size_t len, int flags, struct [sockaddr](sockaddr.md) *__restrict addr, socklen_t *__restrict alen )
```
**Description:**
Receives data from a specified socket.
**Parameters:**
























Name


Description


fd Indicates the socket descriptor.
buf Indicates the pointer to a buffer for storing the data to be received.
len Indicates the length of the buffer pointed to by buf.
flags Indicates the socket flags. This parameter has the following values:
addr Indicates the pointer to the source address.
alen Indicates the size of the source address.
















value


Description


MSG_DONTWAIT


Enables the non-blocking operation.


MSG_MORE


The caller has more data to send.


MSG_NOSIGNAL


No signals are generated.



**Returns:**
Returns the length of the received data if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
























errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


ENOMEM


Memory copy exception


EOPNOTSUPP


Unsupported message type


ENOTCONN


Socket not connected


ENOTCONN


Connection closed



## recvmsg\(\)
```
ssize_t recvmsg (int fd, struct msghdr * msg, int flags )
```
**Description:**
Receives data from a specified socket.
**Parameters:**















Name


Description


fd Indicates the socket descriptor.
msg Indicates the pointer to the address to receive the message header.
flags Indicates the socket flags. This parameter has the following values:
















value


Description


MSG_DONTWAIT


Enables the non-blocking operation.


MSG_MORE


The caller has more data to send.


MSG_NOSIGNAL


No signals are generated.



**Attention:**
This function does not allow sending ancillary data. Currently, the length of the array defined by **msg\_iov** can only be **1**.
**Returns:**
Returns the length of the received data if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.





















errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


EDESTADDRREQ


Invalid connection


ENOMEM


Memory copy exception


EIO


Incorrect connection type obtained based on the socket descriptor



## send\(\)
```
ssize_t send (int fd, const void * buf, size_t len, int flags )
```
**Description:**
Sends data to another socket.
**Parameters:**


















Name


Description


fd Indicates the socket descriptor.
buf Indicates the pointer to a buffer for storing the data to be sent.
len Indicates the length of the buffer pointed to by buf.
flags Indicates the socket flags. This parameter has the following values:
















value


Description


MSG_DONTWAIT


Enables the non-blocking operation.


MSG_MORE


The caller has more data to send.


MSG_NOSIGNAL


No signals are generated.



**Returns:**
Returns the length of the sent data if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.



























errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


EDESTADDRREQ


Invalid connection


ENOMEM


Memory copy exception


EOPNOTSUPP


Unsupported message type


ENOTCONN


Socket not connected


EIO


Incorrect connection type obtained based on the socket descriptor



## sendmsg\(\)
```
ssize_t sendmsg (int fd, const struct msghdr * msg, int flags )
```
**Description:**
Sends data to another socket.
**Parameters:**















Name


Description


fd Indicates the socket descriptor.
msg Indicates the pointer to the address of the message header to be sent.
flags Indicates the socket flags. This parameter has the following values:
















value


Description


MSG_DONTWAIT


Enables the non-blocking operation.


MSG_MORE


The caller has more data to send.


MSG_NOSIGNAL


No signals are generated.



**Attention:**
This function does not support ancillary data.
**Returns:**
Returns the length of the sent data if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
























errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


EDESTADDRREQ


Invalid connection


ENOMEM


Memory copy exception


EIO


Incorrect connection type obtained based on the socket descriptor


EOPNOTSUPP


Unsupported message type



## sendto\(\)
```
ssize_t sendto (int fd, const void * buf, size_t len, int flags, const struct [sockaddr](sockaddr.md) * addr, socklen_t alen )
```
**Description:**
Sends data to another socket.
**Parameters:**
























Name


Description


fd Indicates the socket descriptor.
buf Indicates the pointer to a buffer for storing the data to be sent.
len Indicates the length of the buffer pointed to by buf.
flags Indicates the socket flags. This parameter has the following values:
addr Indicates the pointer to the destination network address.
alen Indicates the length of the destination network address.
















value


Description


MSG_DONTWAIT


Enables the non-blocking operation.


MSG_MORE


The caller has more data to send.


MSG_NOSIGNAL


No signals are generated.



**Returns:**
Returns the length of the sent data if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.



























errno


Description


EINVAL


Invalid input parameter


EBADF


Invalid socket or socket status exception


EDESTADDRREQ


Invalid connection


ENOMEM


Memory copy exception


EOPNOTSUPP


Unsupported message type


ENOTCONN


Socket not connected


EIO


Incorrect connection type obtained based on the socket descriptor



## setprotoent\(\)
```
void setprotoent (int stayopen)
```
**Description:**
Opens a connection to the database and sets the next entry to the first entry.
**Parameters:**









Name


Description


stayopen Reserved for interface compatibility (not used currently).

**Attention:**
The **stayopen** parameter does not take effect. It is equivalent to nonzero for [getprotoent](net.md#gaca0da70657afbc3e723990bb229deec3) and zero for [getprotobyname](net.md#ga83ac8a97dd9d895cda658af3aa46fd55) and **getprotobynumbmer**.
## setsockopt\(\)
```
int setsockopt (int fd, int level, int optname, const void * optval, socklen_t optlen )
```
**Description:**
Sets the socket options.
**Parameters:**





















Name


Description


fd Indicates the socket descriptor.
level Indicates the protocol level at which the option resides. Only SOL_SOCKET is supported.
optname Indicates the socket options to set, which has the following values:
optval Indicates the pointer to the option data.
optlen Indicates the size of the buffer pointed to by optval.






















value


Description


SO_BROADCAST


Permits sending broadcast messages.


SO_RCVTIMEO


Receiving data times out.


SO_RCVBUF


Sets or gets the maximum socket receiving buffer in bytes.


SO_DONTROUTE


Prohibits messages from being sent via a gateway.


SO_BINDTODEVICE


Binds this socket to a particular device.



**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.





















errno


Description


EINVAL


Small optlen


EBADF


Invalid socket or socket status exception


EFAULT


The input parameter is a null pointer.


ENOBUFS


Retrieved length exception


ENOMEM


Memory copy exception



## shutdown\(\)
```
int shutdown (int sockfd, int how )
```
**Description:**
Shuts down a socket.
**Parameters:**












Name


Description


sockfd Indicates the socket descriptor.
how Indicates how to shut down the socket. You can disable either receptions by SHUT_RD or transmissions by SHUT_WR, or both by SHUT_RDWR.

**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.


















errno


Description


EINVAL


Invalid input parameter


ENOTCONN


Socket not connected


ENOMEM


Memory copy exception


EIO


Incorrect connection type obtained based on the socket descriptor



## socket\(\)
```
int socket (int domain, int type, int protocol )
```
**Description:**
Creates a socket and returns its descriptor.
**Parameters:**















Name


Description


domain Indicates the socket domain.
type Indicates the socket type.
protocol Indicates the socket protocol.

**Attention:**
Only the following socket domains are supported: **AF\_INET**, **AF\_INET6**. Only the socket types **SOCK\_RAW**, **SOCK\_DGRAM** and **SOCK\_STREAM** are supported, and the OR operation between the extra socket flag and the socket type is not supported.
**Returns:**
Returns the descriptor for the new socket if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
























errno


Description


EAFNOSUPPORT


Invalid socket domain


ESOCKTNOSUPPORT


Unsupported socket domain


EPROTONOSUPPORT


Unsupported protocol


EINVAL


Invalid input parameter


ENOBUFS


Connection setup error


ENFILE


File table addition exception


点赞 回复
回帖
支持markdown部分语法 ?