网络编程-字节序的转换

网络(六)字节序

c 复制代码
查看 ip 地址

windows 系统:   

        ipconfig
        ipconfig/all

linux系统:   
        
        ifconfig

查看⽹络状态:

c 复制代码
netstat : 查看当前⽹络服务和端⼝情况
参数:
        -a  (all) 显示所有选项,默认不显示LISTEN相关
        -t  (tcp)仅显tcp相关选项
        -u  (udp)仅显示udp相关选项
        -n 拒绝显示别名,能显示数字的全部转化成数字。
        -l 仅列出有在 Listen (监听) 的服务状态

字节序转换函数

IP字符串转换为网络字节序

方法一:inet_aton()

c 复制代码
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef unsigned int  uint32_t;
typedef unsigned int in_addr_t;

in_addr_t inet_addr(const char *cp);

功能:将cp指向的IP字符串转成⽹络字节序

返回值:

成功返回⽹络字节序,失败返回INADDR_NONE [0xffffffff]

注意:它不能识别255.255.255.255,因为它是一个特殊的IP地址。

方法二:inet_aton()

c 复制代码
#include <netinet/in.h>

struct in_addr
{
    unsigned int s_addr;
};

int inet_aton(const char *cp, struct in_addr *inp);     [addr to network]
功能:    将cp指向的IP字符串转成⽹络字节inp保存的地址中。

参数:
        @cp  IP字符串⾸地址
        @inp 存放⽹络字节序的地址
返回值:
            
        成功返回⾮0,失败返回0

实质: 存储在inp结构体指针中的 s_addr这个变量中。

示例

c 复制代码
// todo 转换⽹络字节序
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

void  ip_convert_first(char *ip);
void  ip_convert_second(char *ip);

int main(int argc, char const *argv[]){
    if(argc!= 2){
        printf("Usage: %s <IP address>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    
    ip_convert_first(argv[1]);
    ip_convert_second(argv[1]);

    
    return 0;
}

void  ip_convert_first(char *ip){
    in_addr_t addr;//unsigned int
    addr = inet_addr(ip);
    if(addr == INADDR_NONE){//!  255.255.255.255 会报错 INADDR_NONE
    printf("源 IP 地址无效: %s\n", ip);
    exit(EXIT_FAILURE);
}

    printf("源 IP 地址: %s\n", ip);
    printf("网络字节序: %u\n", ntohl(addr));//? %u是无符号整型 ntohl()函数功能是将网络字节序转换为主机字节序
    printf("16进制表示: %#x\n", addr);
}

void  ip_convert_second(char *ip){
    struct in_addr addr;
    int ret;
    //@param ip 字符串形式的IP地址
    // @param in_addr 结构体变量,用于存储IP地址
    ret=inet_aton(ip, &addr); // 成功返回⾮0,失败返回0
    if(ret == 0){
        printf("源 IP 地址无效: %s\n", ip);
        exit(EXIT_FAILURE);
    }

    
    printf("源 IP 地址: %s\n", ip);
    printf("网络字节序: %u\n", ntohl(addr.s_addr));//? %u是无符号整型 ntohl()函数功能是将网络字节序转换为主机字节序
    printf("16进制表示: %#x\n", addr.s_addr);

}

网络字节序转换为IP字符串

c 复制代码
char *inet_ntoa(struct in_addr in);   [network to addr]

功能:将IP⽹络字节序转换成IP字符串

参数:

    @in   IP⽹络字节序

返回值:

    成功返回IP字符串⾸地址,失败返回NULL

示例

c 复制代码
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>

// todo 将IP⽹络字节序转换成IP字符串
//源 IP 地址: 192.168.0.88
//网络字节序: 3232235608
//16进制表示: 0x5800a8c0
int main() {


    struct in_addr addr;
    addr.s_addr = 0x5800a8c0;
    // 将网络字节序转换成IP字符串
    //@param addr: 网络字节序
    //@return: IP字符串
    char *ip_str = inet_ntoa(addr);
    printf("IP address: %s\n", ip_str);
    
    return 0;
}

主机字节序转换为网络字节序

c 复制代码
short htons(short data); //16位  htonl()是 32位       [host  to network short ]
    功能:将主机字节序转成⽹络字节序
    参数:
        @data 序号转换的整数
    返回值:得到的⽹络字节序


uint32_t ntohs(uint32_t netlong);  16位   ntohl()是 32位           [network to host short]
    功能:把⽹络字节序转换为主机字节序
    参数:
    @   netlong⽹络字节序
    返回值: 返回对应的主机端⼝

示例

c 复制代码
// todo ⽹络字节序和主机字节序 转换
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//源 IP 地址: 192.168.0.88
//主机字节序: 3232235608
//16进制表示: 0x5800a8c0
int main() {
//将主机字节序转换为网络字节序
uint16_t port= htons(8000);
printf("IP address: %#x\n", port);


//将网络字节序转换为主机字节序
uint16_t port2 = ntohs(port);
printf("IP address: %d\n", port2);
//将主机字节序转换为网络字节序
uint16_t port3= htonl(3232235608);
printf("IP address: %#x\n", port3);//0xa8c0

//将网络字节序转换为主机字节序
uint16_t port4 = ntohl(0x5800a8c0);
printf("IP address: %d\n", port4); //88
return 0;
}
相关推荐
孙克旭_9 分钟前
day051-ansible循环、判断与jinja2模板
linux·运维·服务器·网络·ansible
悟空胆好小1 小时前
分音塔科技(BABEL Technology) 的公司背景、股权构成、产品类型及技术能力的全方位解读
网络·人工智能·科技·嵌入式硬件
ssswywywht3 小时前
OSPF实验
网络
FCM663 小时前
HCIA第一次实验报告:静态路由综合实验
网络·tcp/ip·信息与通信
apihz3 小时前
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
android·服务器·开发语言·网络·数据库·网络协议·tcp/ip
dog2504 小时前
TCP 传输时 sk_buff 的 clone 和 unclone
网络·网络协议·tcp/ip
学习溢出4 小时前
【网络安全】理解安全事件的“三分法”流程:应对警报的第一道防线
网络·安全·web安全·网络安全·ids
智慧化智能化数字化方案5 小时前
华为IPD(集成产品开发)流程是其研发管理的核心体系
网络·华为ipd流程·ipd流程体系·ipd产品研发
kfepiza5 小时前
Linux的NetworkManager的nmcli配置网桥(bridge) 笔记250712
linux·运维·网络·笔记·tcp/ip·ip·tcp
cui_win6 小时前
【网络】Linux 内核优化实战 - net.netfilter.nf_conntrack_buckets
linux·网络·.net