BSD Socket API
Header File
Source: include/sys/socket.h
#include <sys/socket.h>
Functions
-
int socket(int domain, int type, int protocol)
Create an endpoint for communication
- Parameters:
domain – [in] Communication domain, Must be AF_INET
type – [in] Socket type SOCK_STREAM or SOCK_DGRAM
protocol – [in] Protocol type, 0 for auto-select
- Returns:
On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errno is set appropriately.
-
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
Initiate a connection socket
- Parameters:
sockfd – [in] Socket file descriptor
addr – [in] Socket address
addrlen – [in] Size of addr
- Returns:
If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.
-
int send(int sockfd, const void *buf, size_t len, int flags)
Send data on a connected socket
- Parameters:
sockfd – [in] Socket file descriptor
buf – [in] data buffer to send
len – [in] size of data to send
flags – [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flags
- Returns:
On success, return the number of bytes sent. On error, -1 is returned, and errno is set appropriately.
-
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.
- Parameters:
sockfd – [in] Socket file descriptor
buf – [in] data buffer to send
len – [in] size of data to send
flags – [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flags
dest_addr – [in] Destination socket address
addrlen – [in] Sizeof dest_addr
- Returns:
On success, return the number of bytes sent. On error, -1 is returned, and errno is set appropriately.
-
int recv(int sockfd, void *buf, size_t len, int flags)
Receive data from connected socket
- Parameters:
sockfd – [in] Socket file descriptor
buf – [out] Pointer to buffer to store received data
len – [in] requested length of data to read
flags – [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flags
- Returns:
On success, return the number of bytes received. On error, -1 is returned, and errno is set appropriately.
-
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
- Parameters:
sockfd – [in] Socket file descriptor
buf – [out] Pointer to buffer to store received data
len – [in] Requested length of data
flags – [in] Flags can be MSG_DONTWAIT for non-blocking operation or 0 for no flags
src_addr – [out] Source address of node
addrlen – [out] Length of src_addr
- Returns:
On success, return the number of bytes received. On error, -1 is returned, and errno is set appropriately.
-
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen)
Get socket option
- Parameters:
sockfd – [in] Socket file descriptor
level – [in] Socket API level (SOL_SOCKET only)
optname – [in] Name of option
optval – [out] Buffer to store option value
optlen – [out] length of optval buffer, value is updated with actual size of optval
- Returns:
On success, zero is returned for the standard options. On error, -1 is returned, and errno is set appropriately.
-
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
Set socket option
- Parameters:
sockfd – [in] Socket file descriptor
level – [in] Socket API level (SOL_SOCKET only)
optname – [in] Name of option
optval – [in] buffer containing value of option
optlen – [in] sizeof optval
- Returns:
On success, zero is returned for the standard options. On error, -1 is returned, and errno is set appropriately.
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 sa_len
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
