1 Star 0 Fork 4.9K

fish / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
network.md 22.48 KB
一键复制 编辑 原始数据 按行查看 历史
NEEN 提交于 2021-03-12 17:59 . !197 Docs Update version 1.0.1

Network

Basic Concepts

The network module implements basic functions of the TCP/IP protocol stack and provides the standard POSIX socket interfaces.

NOTE: Currently, the OS uses lwIP to provide network capabilities.

When to Use

For user-space development, the OpenHarmony kernel provides a set of APIs for you to implement network functionalities, including creating and disabling sockets, transmitting and receiving data, and setting network attributes, in addition to standard POSIX socket functions provided by the C library.

Description

Table 1 Standard APIs in the C library

Header File

Function

Description

sys/socket.h

int accept(int socket, struct sockaddr *address, socklen_t *address_len)

Accepts incoming connection requests.

sys/socket.h

int bind(int s, const struct sockaddr *name, socklen_t namelen)

Binds an IP address to a socket.

sys/socket.h

int shutdown(int socket, int how)

Shuts down a socket.

sys/socket.h

int getpeername(int s, struct sockaddr *name, socklen_t *namelen)

Retrieves the peer address of the specified socket.

sys/socket.h

int getsockname(int s, struct sockaddr *name, socklen_t *namelen)

Retrieves the local address of the specified socket.

sys/socket.h

int getsockopt(int s, struct sockaddr *name, socklen_t *namelen)

Retrieves the socket options.

sys/socket.h

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

Sets the socket options.

unistd.h

int close(int s)

Closes a socket.

sys/socket.h

int connect(int s, const struct sockaddr *name, socklen_t namelen)

Initiates a connection to a socket.

sys/socket.h

int listen(int sockfd, int backlog)

Listens for network connections.

sys/socket.h

ssize_t recv(int socket, void *buffer, size_t length, int flags)

Receives data from another socket.

sys/socket.h

ssize_t recvmsg(int s, struct msghdr *message, int flags)

Receives data from a specified socket based on the input parameters.

sys/socket.h

ssize_t recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)

Receives data from a specified socket and obtains the IP address of the data source.

sys/socket.h

ssize_t send(int s, const void *dataptr, size_t size, int flags)

Sends data to another socket.

sys/socket.h

ssize_t sendmsg(int s, const struct msghdr *message, int flags)

Sends data to another socket based on the input parameters.

sys/socket.h

ssize_t sendto(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)

Sends data to another socket at the specified destination IP address.

sys/socket.h

int socket(int domain, int type, int protocol)

Creates a socket.

sys/select.h

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)

Monitors the I/O events of multiple file descriptors.

sys/ioctl.h

int ioctl(int s, int request, ...)

Obtains and sets socket options.

arpa/inet.h

const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)

Converts an IP address in binary format to a string.

arpa/inet.h

int inet_pton(int af, const char *src, void *dst)

Converts a string to an IP address in binary format.

Details on API differences:

  • sendmsg

    Function prototype:

    ssize_t sendmsg(int s, const struct msghdr *message, int flags)

    Function description: sends a message on a socket.

    Parameter description:

    Parameter

    Description

    s

    Indicates the socket descriptor.

    message

    Indicates the pointer to the message to be sent. The ancillary message is not supported.

    flags

    Indicates the socket flags for sending the message. The options are as follows:

    • MSG_MORE: allows messages that have been sent for multiple times to be packaged and sent at a time.
    • MSG_DONTWAIT: enables a non-blocking operation.

    Return values:

    • Returns the number of bytes that have been sent if the operation is successful.
    • Returns -1 and sets errno if the operation fails.
  • recvmsg

    Function prototype:

    ssize_t recvmsg(int s, struct msghdr *message, int flags)

    Function description: receives a message from a socket.

    Parameter description:

    Parameter

    Description

    s

    Indicates the socket descriptor.

    message

    Indicates the pointer to the address to receive the message. The ancillary message is not supported.

    flags

    Indicates the socket flags for receiving the message. The options are as follows:

    • MSG_PEEK: allows the message to be read without being removed.
    • MSG_DONTWAIT: enables a non-blocking operation.

    Return values:

    • Returns the number of bytes that have been received if the operation is successful.
    • Returns -1 and sets errno if the operation fails.
  • ioctl

    Function prototype:

    int ioctl(int s, int request, ...)

    Function description: obtains or sets socket options.

    Parameter description:

    Parameter

    Description

    s

    Indicates the socket descriptor.

    request

    Indicates the operation to perform on the socket options. The options are as follows:

    • FIONREAD: obtains the number of bytes of the data that can be read from the socket.
    • FIONBIO: sets whether the socket is non-blocked.

    Return values:

    • Returns 0 if the operation is successful.
    • Returns -1 and sets errno if the operation fails.
1
https://gitee.com/fish_neil/docs.git
git@gitee.com:fish_neil/docs.git
fish_neil
docs
docs
master

搜索帮助