Introduction to Programming of socket interface

Source: Internet
Author: User
Tags decimal to binary posix

1, socket interface address structure

The POSIX specification requires only three members in a struct: sin_family, sin_addr, Sin_port. Where sin_addr is also a structure

2, universal socket interface address structure

A set of interface functions is defined as a pointer to a generic set of interface address structures. Since the definition of the SET interface function does not use the void* pointer type,

Therefore, you must force type conversions when you call the socket interface function.

Reason for using the generic socket address structure: The kernel must check the value of the sin_family to determine the type of the structure, based on the common socket interface address knot

3. Byte sorting function

There are two ways in which multibyte data is stored in memory. The small-endian byte sequence stores the low bytes at the start address, and the high-end byte-order byte is stored at the start address.

The POSIX specification stipulates that some members of the socket address structure are maintained by the network byte order, so it is important to note that the host byte order and network byte order

The conversion.

#include <netinet/in.h>

Returns the network byte order value

uint16_t htons (uint16_t)

uint32_t htonl (uint32_t)

Return host byte order value

uint16_t Ntohs (uint16_t)

uint32_t Ntohl (uint32_t)

4. Address conversion function

Address translation functions are used to convert addresses between ASCII strings and binary values of network byte order

#include <arpa/inet.h>

int Inet_aton (const char *strptr, struct in_addr *addrptr)

Converts the string referred to by StrPtr to a 32-bit network byte-order binary value, saving the result in addrptr. Successful return 1, failure returns 0.

in_addr_t inet_addr (const char *strptr)

Returns a 32-bit network byte-order binary value. A constant value of Inaddr_none (255.255.255.255) is returned on failure, so the function cannot handle

Dotted decimal number string 255.255.255.255

Char *inet_ntoa (struct in_addr inaddr)

Converts a 32-bit network byte-order binary value into a dotted decimal number string.

The following two conversion functions can be processed for both IPv4 and IPv6 addresses.

#include <arpa/inet.h>

Dotted decimal to binary conversion

int Inet_pton (int family, const char *strptr, void *addrptr)

Binary point-to-decimal conversion

const char *inet_ntop (int family, const void *addrptr, char *strptr, size_t len)

Introduction to Programming of socket interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.