网络编程-字节序的转换

网络(六)字节序

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;
}
相关推荐
沫璃染墨1 小时前
从零入门计算机网络系列(二十六):IPv4分片机制详解——从MTU与MSS到报文分片与重组
运维·服务器·网络·网络协议·tcp/ip·ip
宝哥网络砖家13 小时前
软路由品牌推荐先看设备站在哪一层
网络·软路由·routeros
时间的拾荒人13 小时前
Linux 系统命令与网络、磁盘、日志监控(下篇):性能原理与优化
linux·运维·网络
nice_lcj52017 小时前
【从零理解计算机网络 · 13】DNS 解析全流程:域名怎么变成 IP,慢在哪
网络·计算机网络·面试·dns·域名解析·cname·ttl
张张123y1 天前
个人助手型 Agent 的六个工程细节:Hermes 与 OpenClaw 的审批、委派、上下文与调度
网络·人工智能·开源·后端开发·ai agent
nice_lcj5201 天前
【从零理解计算机网络 · 15】完整串联:一次请求的全旅程,以及那道经典面试题
网络·计算机网络·http·面试·tcp·总结·网络排查
光电笑映2 天前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(下)
linux·网络·网络协议·tcp
blanks20202 天前
c 语言基础 二级指针理解
c
如夏倾城2 天前
入侵检测系统(IDS)原理、部署模式与 IPS/防火墙对比
大数据·网络·人工智能·网络安全·云原生·架构·kubernetes
LabVIEW开发2 天前
LabVIEW 用 25 ns 精度捕捉赛车发动机共振
网络·labview·labview知识·labview功能·labview程序