Sockets 简要介绍

版权声明:本文全部是胡说八道,如果你喜欢,可随意转载 https://blog.csdn.net/robinsongsog/article/details/79952953

    从这里开始介绍 socket API, 我们由socket 地址开始介绍,基本上后续的所有例子,都要涉及到这个结构体,这个结构体可以有两个传输方向,从用户进程到内核,或者从内核到用户进程Socket Address Structures 绝大多数socket 函数都须要传递一个指向socket的指针作为参数,

一个IPv4 socket 地址结构体,我们一般称之为网络socket 地址结构 体,名字叫作sockaddr_in IPv4 中的socket 地址结构:



Byte Odering 函数:

考虑一个16 bit 的整数,16 bit 可以放两个字节,那么显然就有两种存储方案,如果从起始位置存储的数权值越来越大,

则为little-endian. 反之则为big-endian.


#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
    union {
        short s;
        char c[sizeof(short)];
    }un;

    un.s = 0x0102;
    if (sizeof(short) == 2) {
        if (un.c[0] == 1 && un.c[1] == 2)
            printf("big-endian\n");
        else if (un.c[0] == 2 && un.c[1] == 1)
            printf("litte-endian\n");
        else
            printf("unknown\n");
    } else
        printf("sizeof(short) = %d\n", sizeof(short));

    exit(0);
}



猜你喜欢

转载自blog.csdn.net/robinsongsog/article/details/79952953