Linux Network Programming-socket (1)

Source: Internet
Author: User

Above is the address structure of the corresponding IpV4:

Sin_len the size of the entire structure

sin_family protocol family, corresponding to TCP fixed to af_inet, in addition to TCP Protocol support UNIX domain protocol, etc.

Port for Sin_port socket communication

SIN_ADDR is an unsigned 32-bit network byte address

The above structure only supports IPV4 address protocol, if we support other protocols, we have introduced the General Protocol address Protocol

In use, the IPV4 protocol can be converted into a passing address structure where the IPV4

Sin_port Socket Communication Port 2 bytes

SIN_ADDR is an unsigned 32-bit network byte address 4 bytes

Sin_zero[8] 8 bytes

It adds up to a total of 14 bytes.

SA_DATA[14] bytes that correspond to just Universal

In decimal we say that the left side is high, the right side is low, and so is the other system. Take 0x12345678, the bytes from high to low are 0x12, 0x34, 0x56, and 0x78 in turn.
The high/low address end and high/low byte are all clear. Let's review the definitions of Big-endian and Little-endian and illustrate the two byte-sequences with illustrations:
Take unsigned int value = 0x12345678 as an example, and look at its storage in two byte sequences, we can use unsigned char buf[4] to represent value:
Big-endian: Low address holds high , such as:
Bottom of stack (high address)
---------------
BUF[3] (0x78)--Low
BUF[2] (0x56)
BUF[1] (0x34)
Buf[0] (0x12)--high
---------------
Top of stack (low address)

Little-endian: Low address storage , such as:
Bottom of stack (high address)
---------------
BUF[3] (0x12)--high
BUF[2] (0x34)
BUF[1] (0x56)
Buf[0] (0x78)--Low
--------------
Top of stack (low address)

We test it by code:

#include <stdio.h>int  main () {        int0x12345678;     char*p = (char*) &A;    printf ("%0x%0x%0x%0x \ n", p[0],p[1],p[2], p[3]);     return 0 ;    }

The running result of the program is:

Indicates that the current machine is a small-end mode

Let's test, convert a 32-bit native address into a network byte, the network byte output should be the big-endian mode

Let's look at the code for the program:

Linux Network Programming-socket (1)

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.