APUE Sockets

  • Socket Descriptors
    1
    2
    3
    #include <sys/socket.h>
    int socket(int domain, int type, int protocol);
    Returns: file (socket) descriptor if OK, −1 on error


1
2
3
#include <sys/socket.h>
int shutdown(int sockfd, int how);
Returns: 0 if OK, −1 on error

how : SHUT_RD SHUT_WR SHOT_RDWR

  • Byte Ordering
    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <arpa/inet.h>
    uint32_t htonl(uint32_t hostint32);
    $ Returns: 32-bit integer in network byte order
    uint16_t htons(uint16_t hostint16);
    $ Returns: 16-bit integer in network byte order
    uint32_t ntohl(uint32_t netint32);
    $ Returns: 32-bit integer in host byte order
    uint16_t ntohs(uint16_t netint16);
    $ Returns: 16-bit integer in host byte order
  • translate btween binary and text
    1
    2
    3
    4
    5
    6
    7
    #include <arpa/inet.h>
    const char *inet_ntop(int domain, const void *restrict addr,
    char *restrict str, socklen_t size);
    Returns: pointer to address string on success, NULL on error
    int inet_pton(int domain, const char *restrict str,
    void *restrict addr);
    Returns: 1 on success, 0 if the format is invalid, or −1 on error

dimain : AF_INET, AF_INET6
-