# socket.h - [Overview](#section1503586833165629) - [Summary](#section1957135947165629) - [Data Structures](#nested-classes) - [Macros](#define-members) - [Functions](#func-members) ## **Overview** **Related Modules:** [NET](NET.md) **Description:** Declares functions and data structures related to network sockets. For example, you can use the functions to create and delete network sockets, listen to the network connection state, and send and receive network data. **Since:** 1.0 **Version:** 1.0 ## **Summary** ## Data Structures

Data Structure Name

Description

sockaddr

Describes the socket address information.

## Macros

Macro Name and Value

Description

SHUT_RD 0

Disables reading data from a socket.

SHUT_WR 1

Disables writing data to a socket.

SHUT_RDWR 2

Disables reading data from and writing data to a socket.

PF_INET 2

Defines the IPv4 internet protocol family.

PF_INET6 10

Defines the IPv6 internet protocol family.

AF_INET PF_INET

Defines the IPv4 internet address.

AF_INET6 PF_INET6

Defines the IPv6 internet address.

SOCK_STREAM 1

Defines stream sockets.

SOCK_DGRAM 2

Defines datagram sockets.

SOCK_RAW 3

Defines raw sockets.

MSG_DONTWAIT 0x0040

Defines the message type of a non-blocking operation.

MSG_NOSIGNAL 0x4000

Defines the message type that does not generate a signal.

MSG_MORE 0x8000

Defines the message type that more data is to be transferred.

SO_TYPE 3

Obtains the socket type.

SO_DONTROUTE 5

Prohibits messages from being sent via a gateway.

SO_BROADCAST 6

Enables sending broadcast messages.

SO_SNDBUF 7

Defines the size of the socket sending buffer, in bytes.

SO_RCVBUF 8

Defines the size of the socket receiving buffer, in bytes.

SO_RCVTIMEO 66

Defines the data receiving timeout interval.

SO_SNDTIMEO 67

Defines the data sending timeout interval.

SO_BINDTODEVICE 25

Binds the socket to a particular device.

SOL_SOCKET 1

Defines the layers of a socket.

## Functions

Function

Description

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

Connects a socket to the specified address.

listen (int sockfd, int backlog)

int

Listens for network connection requests on a socket.

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

int

Accepts a connection request on a socket.

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.