10、host,net字节转换函数

本文深入探讨了socket编程中的关键函数及其应用,并详细解释了如何进行网络地址转换,包括inet_aton、inet_addr、inet_network和inet_ntoa等核心函数的使用方法。

1、函数

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);

in_addr_t inet_network(const char *cp);

char *inet_ntoa(struct in_addr in);

typedef u_int32_t in_addr_t;

typedef unsigned char u_int8_t;

typedef unsigned short int u_int16_t;

typedef unsigned int u_int32_t;

///usr/include/sys/types.h

********

uint32_t htonl(uint32_t hostlong);

uint16_t htons(uint16_t hostshort);

uint32_t ntohl(uint32_t netlong);

uint16_t ntohs(uint16_t netshort);

2、结构体

struct sockaddr_in {

    sa_family_t    sin_family; /* address family: AF_INET */

    u_int16_t      sin_port;   /* port in network byte order */

    struct in_addr sin_addr;   /* internet address */

};

/* Internet address. */

struct in_addr {

    u_int32_t      s_addr;     /* address in network byte order */

};

参考

1http://www.cppblog.com/toMyself/archive/2010/08/13/123353.aspx

2http://linux.die.net/man/3/htonl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值