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_INET

  • type: [in] Socket type SOCK_STREAM or SOCK_DGRAM

  • protocol: [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 descriptor

  • addr: [in] Socket address

  • addrlen: [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 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

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 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

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 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

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 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

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 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

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 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

Structures

struct linger

Public Members

int l_onoff

option on/off

int l_linger

linger time in seconds

struct sockaddr_in

IPv4 Socket address structure

Public Members

uint8_t sin_len

Length of structure

sa_family_t sin_family

Address family (AF_INET only)

in_port_t sin_port

Port in network byte order

struct in_addr sin_addr

IPv4 address

char sin_zero[8]

reserved

struct sockaddr_in6

Public Members

uint8_t sin6_len

length of this structure

sa_family_t sin6_family

AF_INET6

in_port_t sin6_port

Transport layer port #

uint32_t sin6_flowinfo

IPv6 flow information

struct in6_addr sin6_addr

IPv6 address

uint32_t sin6_scope_id

Set of interfaces for scope

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

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_LINGER

get/set linger structure

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