10、host,net字节转换函数

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

原文地址:https://www.cnblogs.com/mydomain/p/2167448.html