aa

相关函数原型及参数类型:

函数原型:
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); struct in_addr inet_makeaddr(int net, int host); in_addr_t inet_lnaof(struct in_addr in); in_addr_t inet_netof(struct in_addr in); int inet_pton(int af, const char *src, void *dst);
       inet_pton()  returns  1 on success (network address was successfully converted).  0 is returned if src does not contain a character string representing a valid network address in the specified
       address family.  If af does not contain a valid address family, -1 is returned and errno is set to EAFNOSUPPORT. 参数类型:
/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
{
    in_addr_t s_addr;
};
inet_addr函数:将IPv4的点分十进制地址转换为网络字节序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h> int main(int argc, char *argv[])
{
    struct in_addr addr;     if (argc != 2) {
        fprintf(stderr, "%s <dotted-address>\n", argv[0]);
        exit(EXIT_FAILURE);
    }     if (inet_aton(argv[1], &addr) == 0) {
        perror("inet_aton");
        exit(EXIT_FAILURE);
    }
    printf("addr = 0x%-10x\n", addr.s_addr);     printf("%s\n", inet_ntoa(addr));
    exit(EXIT_SUCCESS);
} yinguicai@Cpl-IBP-Product:~/tmp/network$ ./a.out 127.0.0.1
addr = 0x100007f   
127.0.0.1
char *inet_ntoa(struct in_addr in)
{
static char buf[INET_NTOA_MAX_LEN];
return inet_ntoa_r(in, buf);
}

注意:

1、inet_ntoa的返回值是一个static类型的char *指针,所以使用的时候需要注意(不可重入,可以使用线程安全的inet_ntoa_r函数代替)

2、The inet_addr() function converts the Internet host address cp from IPv4 numbers-and-dots notation into binary data in network byte order.  If the input is invalid, INADDR_NONE (usually -1) is
       returned.  Use of this function is problematic(有问题的) because -1 is a valid address (255.255.255.255).  Avoid its use in favor of inet_aton(), inet_pton(3), or getaddrinfo(3) which provide a  cleaner
       way to indicate error return.(因为其返回值为in_addr_t,当返回-1时,有二义性->表示两个含义)

3、inet_aton() returns nonzero if the address is valid, zero if not.

4、inet_aton/inet_addr/inet_ntoa仅适用于ipv4地址

5、inet_pton/inet_ntop对于v4和v6地址均适用

网络地址转换相关函数使用(inet_addr,inet_ntoa,inet_addr)的更多相关文章

  1. 地址转换函数:inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop

    在Unix网络编程中,我们常用到地址转换函数,它将ASCII字符串(如"206.62.226.33")与网络字节序的二进制值(这个值保存在套接口地址结构中)间进行地址的转换. 1. ...

  2. Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)

    Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa) htonl() htons() ntohl() ntohs()及inet_n ...

  3. IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa

    inet_pton NAME     inet_pton - 将 IPv4 和 IPv6 地址从点分十进制转换为二进制 SYNOPSIS #include <arpa/inet.h> in ...

  4. inet_addr() inet_ntoa() inet_pton inet_ntop sockaddr_in

    inet_addr() 简述:将一个点间隔地址转换成一个in_addr.#include <winsock.h>unsigned long PASCAL FAR inet_addr( co ...

  5. linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换

    用域名获取IP地址或者用IP获取域名 #include<stdio.h> #include<sys/socket.h> #include<netdb.h> int ...

  6. NAT 网络地址转换

    NAT  网络地址转换(Network Address Translation) NAT(Network Address Translation,网络地址转换)是1994年提出的. 属接入广域网(WA ...

  7. CCNA学习 NAT网络地址转换

    CCNA基础 NAT网络地址转换 在计算机网络中,网络地址转换(Network Address Translation,缩写为NAT),也叫做网络掩蔽或者IP掩蔽(IP masquerading),是 ...

  8. 网络地址转换NAT原理及其作用

    1 概述 1.1 简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task F ...

  9. RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数

    正如第一部分(“设置静态网络路由”)提到的,在这篇文章(RHCE 系列第二部分),我们首先介绍红帽企业版 Linux 7(RHEL)中包过滤和网络地址转换(NAT)的原理,然后再介绍在某些条件发生变化 ...

随机推荐

  1. 【LeetCode】32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. java 抽象类 接口 区别

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 接口 里面 都是抽象方法. 接口里面的 字段 都是 public static fina ...

  3. 【四边形不等式】noi95- 合并石子

    [题目大意] 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分.试设计出1个算法,计算出将N堆石子合并成 ...

  4. Centos 右上角面板里没有wired network图标的问题

    开了很多的网页查看解决这个问题,都不是很有效,最后很简单的改了下一个文件就ok了,自己记录下,以免忘记! 打入命令:sudo gedit /etc/NetworkManager/nm-system-s ...

  5. python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...

  6. Educational Codeforces Round 11 B. Seating On Bus 水题

    B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...

  7. Process ID, Process handle, Window handle

    http://forums.codeguru.com/showthread.php?392273-RESOLVED-How-to-get-window-s-HWND-from-it-s-process ...

  8. .NET 4.5 is an in-place replacement for .NET 4.0

    With the betas for .NET 4.5 and Visual Studio 11 and Windows 8 shipping many people will be installi ...

  9. 虚拟机网络配置详解(NAT、桥接、Hostonly) z

    http://www.cnblogs.com/beginmind/p/6379881.html VirtualBox中有四种网络连接方式: NAT Bridged Adapter Internal H ...

  10. webbrowser 常用方法(C#)

    0.常用方法   Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...