[Unix network programming] introduction to chapter3 socket programming

Chapter3 Introduction to Socket Programming
3.1 Overview

  • The address conversion functions convert between textual representations of addresses and their binary values ​​stored in the socket address structure. Most of the existing IPv4 code uses the two functions inet_addr and inet_ntoa, but the two new functions inet_pton and inet_ntop apply to both IPv4 and IPv6.

3.2 Socket address structure
   sockaddr_
   3.2.1 IPv4 socket address structure

  •   The IPv4 socket address structure is also commonly referred to as the "Internet socket address structure", which is defined in the <netinet/in.h> header file with the sockaddr_in command.
  •   struct in_addr
  •   {
  •    in_addr_t s_addr;
  •   };
  •   struct sockaddr_in
  •   {
  •    uint8_t sin_len;
  •    sa_family sin_family;
  •    in_port_t sin_port;
  •    struct in_addr sin_addr;
  •    char sin_zero[8];
  •   }

  3.2.2 Generic socket address structure

  •   #include <sys/socket.h>
  •   struct sockaddr
  •   {
  •    uint8_t sa_len;
  •    sa_family_t sa_family;
  •    char sa_data [14];
  •   };
  •   eg:int bind(int, struct sockaddr *, socklen_t_);
  •    struct sockaddr_in serv;
  •    /*  fill in serv */
  •    bind(sockfd, (struct sockaddr*)serv, sizeof(serv));


3.3 Value-result parameters

  •  Two ways of socket transfer: 1: from process to kernel 2: from kernel to process
  •  (1): There are 3 functions that pass the socket address structure from the process to the kernel: bind, connnet, sendto. One parameter of these functions is a pointer to a socket address structure, and the other parameter is the structure of the structure. Integer big.
  •   struct sockaddr_in serv;
  •   /* fill serv */
  •   connect(sockfd, (struct sockaddr*)serv, sizeof(serv));
  •  (2): There are four functions that pass the socket address structure from the kernel to the process: accept, recvfrom, getsockname and getpeername.
  •   struct sockaddr_un cli;
  •   socklen_t len;
  •   len = sizeof(cli);
  •   getpeername(unixfd, (struct sockaddr*)&cli, &len);

3.4 Byte ordering function

  •  The Internet Protocol uses big-endian byte order to transmit these multibyte integers.
  •  #include <netinet/in.h>
  •  uint16_t htons(uint16_t host16bitvalue);
  •  uint32_t htonl(uint32_t host32bitvalue);
  •  uint16_t ntohs(uint16_t net16bitvalue);
  •  uint32_t ntohl(uint32_t net32bitvalue);
  •  h: host
  •  n: network
  •  s: short
  •  l: long

3.5 Byte manipulation functions

  •  #include <strings.h>
  •  void bzero(void *dest, szie_t nbytes);
  •  void bcopy(const void *src, void *dest, size_t nbytes);
  •  int bcmp(const void *ptr, const void *ptr2, szie_t nbytes);
  •  #include <string.h>
  •  void *memset(void *dest, int c, size_t len);
  •  void *memcpy(void *dest, const void *src, size_t nbytes);
  •  int memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

3.6 inet_aton, inet_addr and inet_ntoa functions

  •  (1) inet_aton, inet_addr and inet_ntoa convert IPv4 addresses between a dotted decimal string ("192.168.1.100") and its 32-bit network byte order binary value.
  •  (2) The two newer functions inet_pton and inet_ntop are applicable to both IPv4 and IPv6 addresses
  •  #include <arpa/inet.h>
  •  int inet_aton(const char *strptr, struct in_addr *addrptr);
  •  in_addr_t inet_addr(const char *strptr);
  •  char * inet_ntoa (struct in_addr inaddr);

3.7 inet_pton and inet_ntop functions

  •  p:presentation(expression)->ASCII string
  •  n:numeric(value)->binary value stored in the socket address structure
  •  #include <arpa/inet.h>
  •  int inet_pton(int family, const char *strptr, void *addrptr);
  •  const char *inet_ntop(int family, const void *addrptr, char *strptr, size_tlen);
  •  eg:
  •   inet_pton (AF_INET, cp, & foo.sin_addr);
  •   char str[INET_ADDRSTRLEN];
  •   ptr = inet_ntop(AF_INET, &foo.sin_addr, str, sizeof(str));

3.8 sock_ntop and related functions

  •  struct sockaddr_in addr;
  •  inet_ntop(AF_INET, &addr.sin_addr, str, sizeof(str));

3.9 readn, writen and readline functions
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889301&siteId=291194637