Socket (socket) and socket programming __ Network && Communication

Source: Internet
Author: User
BYTE order:
Host byte order: Different hosts have different byte order, x86: Small end, network byte order (network byte order): big-endian byte sequence; 0. What is a socketSockets are communications between network processes (network interprocess communication, also known as network IPC) interfaces that enable the process to communicate with other processes, whether on the same computer or not, as an abstraction of the communication endpoint, like a file descriptor Descriptor) is an abstract representation of a file; 1. Basic Concepts

The socket can be regarded as the programming interface of user process (application, working in user space) and Kernel network protocol stack (called the Socket Interface), to realize the access and invocation of user space application to the service provided by the Kernel network protocol stack;



Socket below the UDP/TCP, IP, Channel and so on is the kernel network protocol stack; 2. IPV4 Socket Address Interface

$ mans 7 IP

The IPV4 socket address structure, commonly referred to as the "Internet socket Address Structure", is named sockaddr_in, defined in <netinet.h>:

struct SOCKADDR_IN {
    sa_family_t    sin_family;/* address family:af_inet      /in_port_t sin_port;   /* port in Network byte order
    /struct in_addr sin_addr;   /* Internet address *
/};

/* Internet address. * *
struct IN_ADDR {
    uint32_t       s_addr;     /* address in network byte order *
/};
Af_inet: It represents the IPV4 agreement;
In addition, there are Unix domain protocols; network byte order: network byte sequence; 3. Common Address structure

Make the socket addresses between the different protocols compatible.

struct SOCKADDR {
    uint8_t sin_len;
    sa_family_t sin_family;
    Char sa_data[14];
}
Sin_len: Length of the entire SOCKADDR structure; sin_family: Specify the Address family; Sa_data: The form is determined by the sin_family; 4. Byte-Order conversion function
uint32_t htonl (uint32_t hostlong);
uint16_t htons (uint16_t hostshort);
uint32_t Ntohl (uint32_t hostlong);
uint16_t Ntohs (uint16_t hostshort);
H:host, representing the host (host byte order); N:network, representing the network (network byte order); S:short,l:long

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.