netinet/in.h typedef uint32_t in_addr_t; typedef uint16_t in_port_t; struct in_addr { in_addr_t s_addr; }; struct sockaddr_in { sa_family_t sin_family; /* AF_INET, defined in sys/socket.h */ in_port_t sin_port; /* Port number */ struct in_addr sin_addr; /* IPv4 address */ }; struct in6_addr { uint8_t s6_addr[16]; }; struct sockaddr_in6 { sa_family_t sin6_family; /* AF_INET6, defined in sys/socket.h */ in_port_t sin6_port; /* Port number */ uint32_t sin6_flowinfo /* IPv6 traffic class and flow information.*/ struct in6_addr sin6_addr; /* IPv4 address */ uint32_t sin6_scope_id /*Set of interfaces for a scope. */ }; arpa/inet.h uint32_t htonl(uint32_t hostlong); Converts 32 bit value from host to network byte order. uint16_t htons(uint16_t hostshort); Converts 16 bit value from host to network byte order. uint32_t ntohl(uint32_t netlong); Converts 32 bit value from network to host byte order. uint16_t ntohs(uint16_t netshort); Converts 16 bit value from network to host byte order. in_addr_t inet_addr(const char *cp); Converts the string pointed to by cp, in the Internet standard dot notation,to an integer value suitable for use as an Internet address. in_addr_t inet_lnaof(struct in_addr in); Takes an Internet host address specified by in and extracts the local network address part, in host byte order. struct in_addr inet_makeaddr(in_addr_t net, in_addr_t lna); Takes the Internet network number specified by net and the local network address specified by lna, both in host byte order, and constructs an Internet address from them. in_addr_t inet_netof(struct in_addr in); Takes an Internet host address specified by in and extracts the network number part, in host byte order. in_addr_t inet_network(const char *cp); Converts the string pointed to by cp, in the Internet standard dot notation, to an integer value suitable for use as an Internet network number. char *inet_ntoa(struct in_addr in); Converts the Internet host address specified by in to a string in the Internet standard dot notation. sys/socket.h struct sockaddr { sa_family_t sa_family; /* Address family */ char sa_data[]; /* Socket address */ }; typedef /* ... */ socklen_t; Describes the length of a socket address. This is an integer type of at least 32 bits. typedef /* ... */ sa_family_t; Describes a socket's protocol family. This is an unsigned integer type. Following values are specified in the header file for sa_family_t. AF_INET Internet domain sockets for use with IPv4 addresses. AF_INET6 Internet domain sockets for use with IPv6 addresses. AF_UNIX UNIX domain sockets. AF_UNSPEC Unspecified. struct msghdr { void* msg_name; /* Optional, points to a buffer containing the address of a datagram socket*/ socklen_t msg_namelen; /* Size of address */ struct iovec* msg_iov; /* Scatter/gather array */ size_t msg_iovlen; /* # elements in msg_iov */ void* msg_control; /* Ancillary data, see below */ size_t msg_controllen; /* Ancillary data buffer len */ int msg_flags; /* Flags (unused) */ }; int socket(int domain, int type, int protocol); Creates an endpoint for communication and returns a file descriptor that refers to that endpoint. int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); Sets the optval of option optname of the socket sockfd int getsockopt(int sockfd, int level, int optname, void *restrict optval, socklen_t *restrict optlen); Gets the optval of option optname of the socket sockfd that was set using setsockopt() function. int bind(int sockfd, const struct sockaddr addr*, socklen_t addrlen); Assigns the address specified by addr to the socket referred to by the file descriptor sockfd. int listen(int sockfd, int backlog); Marks the socket referred to by sockfd as a passive socket that will be used to accept incoming connection requests int connect(int sockfd, const struct sockaddr * addr, socklen_t addrlen); Connects the socket referred to by the file descriptor sockfd to the address specified by addr. A passive socket is listening for the connection request at the address addr. int accept(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen); Takes the first connection request from the queue on a passive or listening socket sockfd, fills addr with the peer address and returns a connection socket int getpeername(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen); Fills the buffer pointed to by addr with the address of the peer connected to the socket sockfd. int getsockname(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen); Fills the buffer pointed by addr with the current address to which the socket sockfd is bound. ssize_t send(int sockfd, const void * buf, size_t len, int flags); Sends a message referred by buf using a stream (connected) socket sockfd. Stream socket must be obtained using either connect() or accept() system call ssize_t recv(int sockfd, void * buf, size_t len, int flags); Receives a message into a buffer referred by buf using a stream (connected) socket sockfd. ssize_t sendto(int sockfd, const void * buf, size_t len, int flags, const struct sockaddr * dest_addr, socklen_t addrlen); Sends a message referred by buf using a datagram (unconnected) socket sockfd to a destination datagram socket bound to dest_addr. ssize_t recvfrom(int sockfd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict src_addr, socklen_t *restrict addr_len); Receives a message into a buffer referred by buf using a datagram (unconnected) socket sockfd from a source datagram socket bound to src_addr. ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); Sends a message msg using either a stream or a datagram socket. If a datagram socket is being used destination datagram socket is specified by msg->msg_name ssize_t recvmsg(int sockfd, struct msghdr * msg, int flags); Received a message into a data structure referred by msg using either a stream or a datagram socket. If a datagram socket is being used source datagram socket will be written into msg->msg_name int shutdown(int sockfd, int how); Shuts all or a part of a full duplex connection on a socket sockfd. int socketpair(int domain, int type, int protocol, int[2] socket_vector); Creates a pair of connected sockets returned through socket_vector[0] and socket_vector[1], suitable to use between parent and child processes. netdb.h struct hostent { char* h_name //Official name of the host. char** h_aliases //A pointer to an array of pointers to alternative host names, terminated by a null pointer. int h_length //The length, in bytes, of the address. char** h_addr_list //A pointer to an array of pointers to network addresses (in network byte order) for the host, terminated by a null pointer. }; struct netent { char* n_name //Official, fully-qualified (including the domain) name of the host. char** n_aliases //A pointer to an array of pointers to lternative network names, terminated by a null pointer. int n_addrtype //The address type of the network. uint32_t n_net //The network number, in host byte order. }; struct protoent { char* p_name //Official name of the protocol. char** p_aliases //A pointer to an array of pointers to alternative protocol names, terminated by a null pointer. int p_proto //The protocol number. }; struct servent { char* s_name //Official name of the service. char** s_aliases //A pointer to an array of pointers to alternative service names, terminated by a null pointer. int s_port //The port number at which the service resides, in network byte order. char* s_proto //The name of the protocol to use when contacting the service. }; struct addrinfo { int ai_flags //Input flags. int ai_family //Address family of socket. int ai_socktype //Socket type. int ai_protocol //Protocol of socket. socklen_t ai_addrlen //Length of socket address. struct sockaddr* ai_addr //Socket address of socket. char* ai_canonname //Canonical name of service location. struct addrinfo* ai_next //Pointer to next in list. }; void sethostent(int stayopen); Opens a connection to domain name system database at the host and set the next entry for retrieval to the first entry in the database. struct hostent *gethostent(void); Reads the next host entry from domain name system database. This function is not reentrant, i.e., not thread safe. void endhostent(void); Closes the connection to domain name system database struct hostent *gethostbyname(const char *name); Returns a host entry containing addresses of address family AF_INET for the host with name name. This function is not reentrant, i.e., not thread safe. struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type); Returns s host entry containing addresses of address family type for the host with address addr. This function is not reentrant, i.e., not thread safe. void setservent(int stayopen); Opens a connection to domain name system database at the host and set the next entry for retrieval to the first entry in the database. struct servent *getservent(void); Reads the next service entry from domain name system database. This function is not reentrant, i.e., not thread safe. void endservent(void); Closes the connection to domain name system database struct servent *getservbyname(const char *name, const char *proto); Searches the database from the beginning and find the first entry for which the service name specified by name matches the s_name member and the protocol name specified by proto matches the s_proto member of the entry. This function is not reentrant, i.e., not thread safe. struct servent *getservbyport(int port, const char *proto); Searches the database from the beginning and find the first entry for which the port specified by port matches the s_port member and the protocol name specified by proto matches the s_proto member of the entry. This function is not reentrant, i.e., not thread safe. void setprotoent(int stayopen); Opens a connection to domain name system database at the host and set the next entry for retrieval to the first entry in the database. struct protoent *getprotoent(void); Reads the next protocol entry from domain name system database. This function is not reentrant, i.e., not thread safe. void endprotoent(void); Closes the connection to domain name system database. struct protoent *getprotobyname(const char *name); Searches the database from the beginning and find the first entry for which the protocol name specified by name matches the p_name member. This function is not reentrant, i.e., not thread safe. struct protoent *getprotobynumber(int proto); Searches the database from the beginning and find the first entry for which the protocol number specified by proto matches the p_proto member. This function is not reentrant, i.e., not thread safe. void setnetent(int stayopen); Opens a connection to domain name system database at the host and set the next entry for retrieval to the first entry in the database. struct netent *getnetent(void); Reads the next network entry from domain name system database. This function is not reentrant, i.e., not thread safe. void endnetent(void); Closes the connection to domain name system database. struct netent *getnetbyaddr(uint32_t net, int type); Searches the database from the beginning, and find the first entry for which the address family specified by type matches the n_addrtype member and the network number net matches the n_net member. This function is not reentrant, i.e., not thread safe. struct netent *getnetbyname(const char *name); Searches the database from the beginning and find the first entry for which the network name specified by name matches the n_name member. This function is not reentrant, i.e., not thread safe. int getaddrinfo(const char *restrict node, const char *restrict service, const struct addrinfo *restrict hints, struct addrinfo **restrict res); Given node and service, which identify an Internet host and a service, returns one or more addrinfo structures, each of which contains an Internet address void freeaddrinfo(struct addrinfo *res); Frees the memory that was allocated for the dynamically allocated linked list res. const char *gai_strerror(int errcode); Translates these error codes to a human readable string, suitable for error reporting. int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen, char *restrict node, socklen_t nodelen, char *restrict service, socklen_t servicelen, int flags); Translates a socket address into a node (host) name and servic name.