BSD Socket API¶
Header File¶
Functions¶
-
int
socket(int domain, int type, int protocol)¶ Create an endpoint for communnication
- Return
On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errno is set appropriately.
- Parameters
domain: [in] Communication domain, Must be AF_INETtype: [in] Socket type SOCK_STREAM or SOCK_DGRAMprotocol: [in] Protocol type, 0 for auto-select
-
int
connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)¶ Initiate a connection socket
- Return
If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptoraddr: [in] Socket addressaddrlen: [in] Size of addr
-
int
send(int sockfd, const void *buf, size_t len, int flags)¶ Send data on a connected socket
- Return
On success, return the number of bytes sent. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptorbuf: [in] data buffer to sendlen: [in] size of data to sendflags: [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flags
-
int
sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)¶ Send data to node. This function is used to send data on datagram socket.
- Return
On success, return the number of bytes sent. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptorbuf: [in] data buffer to sendlen: [in] size of data to sendflags: [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flagsdest_addr: [in] Destination socket addressaddrlen: [in] Sizeof dest_addr
-
int
recv(int sockfd, void *buf, size_t len, int flags)¶ Receive data from connected socket
- Return
On success, return the number of bytes received. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptorbuf: [out] Pointer to buffer to store received datalen: [in] requested length of data to readflags: [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flags
-
int
recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)¶ Receive data from node. This function is used to receive data from datagram socket
- Return
On success, return the number of bytes received. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptorbuf: [out] Pointer to buffer to store received datalen: [in] Requested length of dataflags: [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flagssrc_addr: [out] Source address of nodeaddrlen: [out] Length of src_addr
-
int
getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen)¶ Get socket option
- Return
On success, zero is returned for the standard options. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptorlevel: [in] Socket API level (SOL_SOCKET only)optname: [in] Name of optionoptval: [out] Buffer to store option valueoptlen: [out] length of optval buffer, value is updated with actual size of optval
-
int
setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)¶ Set socket option
- Return
On success, zero is returned for the standard options. On error, -1 is returned, and errno is set appropriately.
- Parameters
sockfd: [in] Socket file descriptorlevel: [in] Socket API level (SOL_SOCKET only)optname: [in] Name of optionoptval: [in] buffer containing value of optionoptlen: [in] sizeof optval
Structures¶
-
struct
linger¶
-
struct
sockaddr_in¶ IPv4 Socket address structure
-
struct
sockaddr_in6¶
-
struct
sockaddr¶ Generic socket address structure
Public Members
-
unsigned char
sa_len¶ total length
-
sa_family_t
sa_family¶ address family
-
char
sa_data[14]¶ actually longer; address value
-
unsigned char
Macros¶
-
PF_UNSPEC¶ Protocol family unspecified
-
PF_INET¶ Internet address family (TCP, UDP)
-
PF_INET6¶ Internet address family V6
-
AF_UNSPEC¶ BSD address family
-
AF_INET¶ Internet address family (TCP, UDP)
-
AF_INET6¶ Internet address family V6
-
SOCK_STREAM¶ Stream socket type (TCP/connection oriented)
-
SOCK_DGRAM¶ Datagram socket (UDP/connection-less)
-
SOCK_NONBLOCK¶ Socket type non-blocking, This flag can be ORed with socket type to specify non-blocking operation of socket. This is to prevent extra call to fcntl
-
IPPROTO_TCP¶ TCP protocol
-
IPPROTO_UDP¶ UDP protocol
-
SOL_SOCKET¶ Socket level
-
SO_TYPE¶ Get socket type
-
SO_SNDBUF¶ Get send buffer size
-
SO_RCVBUF¶ Get receive buffer size
-
SO_KEEPALIVE¶ Get/set keep alive status
-
SO_RCVTIMEO¶ Set/get receive timeout
-
SO_SNDTIMEO¶ set/get send timeout
-
SO_CONTIMEO¶ set/get connection timeout
-
TCP_NODELAY¶ Enable/Disable Nagle algorithm
-
TCP_MAXSEG¶ Get max segment size
-
TCP_QUICKACK¶ Get Quickack mode status
-
IP_TTL¶ Get/Set TTL value
-
MSG_DONTWAIT¶ Nonblocking i/o for this operation only
Type Definitions¶
-
typedef uint8_t
sa_family_t¶
-
typedef __socklen_t
socklen_t¶
