`
dll38dll
  • 浏览: 19695 次
社区版块
存档分类
最新评论

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

 
阅读更多

Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)
2009年12月27日
  htonl() htons() ntohl() ntohs()及inet_ntoa() inet_addr()的用法
  注:其中的h表示“host”,n表示“net”,l表示“long”,
                      s表示“short”a表示“ascii”,ddr表示“in_addr结构体”
  现在我们很幸运,因为我们有很多的函数来方便地操作 IP 地址。没有 必要用手工计算它们,也没有必要用"一个sockaddr_in结构体变量ina,你有一个IP地址"132.241.5.10" 要储存在其中,你就要用到函数inet_addr(),将IP地址从点数格式转换成无符号长整型。使用方法如下:
  ina.sin_addr.s_addr = inet_addr("132.241.5.10");
  注意,inet_addr()返回的地址已经是网络字节格式,所以你无需再调用 函数htonl()。
  现在你可以将IP地址转换成长整型了。有没有其相反的方法呢? 它可以将一个in_addr结构体输出成点数格式?这样的话,你就要用到函数 inet_ntoa()("ntoa"的含义是"network to ascii"),就像这样:
  printf("%s",inet_ntoa(ina.sin_addr));
  它将输出IP地址。需要注意的是inet_ntoa()将结构体in-addr作为一 个参数,不是长整形。同样需要注意的是它返回的是一个指向一个字符的指针。它是一个由inet_ntoa()控制的静态的固定的指针,所以每次调用 inet_ntoa(),它就将覆盖上次调用时所得的IP地址。例如:
  char *a1, *a2;
  a1 = inet_ntoa(ina1.sin_addr); /* 这是198.92.129.1 */
  a2 = inet_ntoa(ina2.sin_addr); /* 这是132.241.5.10 */
  printf("address 1: %s ",a1);
  printf("address 2: %s ",a2);
  输出如下:
  address 1: 132.241.5.10
  address 2: 132.241.5.10
  内存结构:
  
  
  
  假如你需要保存这个IP地址,使用strcpy()函数来指向你自己的字符指针。
  ==================================================================
  htonl()表示将32位的主机字节顺序转化为32位的网络字节顺序 htons()表示将16位的主机字节顺序转化为16位的网络字节顺序(ip地址是32位的端口号是16位的 )
  inet_ntoa()
  简述:
  将网络地址转换成“.”点隔的字符串格式。
  #include
  char FAR* PASCAL FAR inet_ntoa( struct in_addr in);
  in:一个表示Internet主机地址的结构。
  注释:
  本函数将一个用in参数所表示的Internet地址结构转换成以“.” 间隔的诸如“a.b.c.d”的字符串形式。请注意inet_ntoa()返回的字符串存放在WINDOWS套接口实现所分配的内存中。应用程序不应假设该内存是如何分配的。在同一个线程的下一个WINDOWS套接口调用前,数据将保证是有效。
  返回值:
      若无错误发生,inet_ntoa()返回一个字符指针。否则的话,返回NULL。其中的数据应在下一个WINDOWS套接口调用前复制出来。
  参见:
  inet_addr().
  测试代码如下
  include
  #include
  #include
  #include
  #include
  int main(int aargc, char* argv[])
  {
  struct in_addr addr1,addr2;
  ulong   l1,l2;
  l1= inet_addr("192.168.0.74");
  l2 = inet_addr("211.100.21.179");
  memcpy(&addr1, &l1, 4);
  memcpy(&addr2, &l2, 4);
  printf("%s : %s\n", inet_ntoa(addr1), inet_ntoa(addr2));    //注意这一句的运行结果
  printf("%s\n", inet_ntoa(addr1));
  printf("%s\n", inet_ntoa(addr2));
  return 0;
  }
  实际运行结果如下:
  192.168.0.74 : 192.168.0.74       //从这里可以看出,printf里的inet_ntoa只运行了一次。
  192.168.0.74
  211.100.21.179
  inet_ntoa返回一个char *,而这个char *的空间是在inet_ntoa里面静态分配的,所以inet_ntoa后面的调用会覆盖上一次的调用。第一句printf的结果只能说明在printf里面的可变参数的求值是从右到左的,仅此而已。
分享到:
评论

相关推荐

    IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa).txt

    描述了IP地址格式转换的常用接口,主机字节序和网络字节序相互转换,Ip字符串转换成long型数值;高字节序和低字节序的名词解释

    inet_ntoa()&inet;_addr()&htonl;()

    网络程序设计inet_ntoa()&inet;_addr()&htonl;()函数代码,简单编写的,可供于关于以上函数的思想方式。没有头文件中的强大,没有错误提示及处理代码段,学习网络程序设计可以参考。

    WinSocket 常用函数及结构

    常用结构定义及函数 WSADATA ...inet_addr inet_ntoa socket closesocket bind listen connect accept select send recv send 、sendto 、recv 、recvfrom函数在不同模式下的不同表现

    telnet_clinet.zip

    inet_ntoa(client_addr.sin_addr)); if(write(new_fd,hello,strlen(hello))==-1) { fprintf(stderr,"Write Error:%s\n",strerror(errno)); exit(1); } /* 这个通讯已经结束 */ close(new_fd); /* 循环下一...

    易语言Socket编程之Select模型

    易语言Socket编程之Select模型源码,Socket编程之Select模型,CreateSocket,FD_CLR,FD_ISSET,FD_SET,FD_ZERO,ThreadAcceptProc,ThreadProc,GetClientInfo,htonl,ntohl,ntohs,htons,WSAStartup,WSACleanup,closesocket,...

    linux网络编程学习笔记

    char *inet_ntoa(struct in_addr in) 函数里面 a 代表 ascii n 代表 network.第一个函数表示将 a.b.c.d 的 IP转换为 32 位的 I P,存储在 inp 指针里面.第二个是将 32 位 IP转换为 a.b.c.d 的格式 服务信息函数 ...

    网络编程函数(练习与实例)

    server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(portnumber); /* 捆绑sockfd 描述符 */ if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))== -1) { /******...

    套接字编程

    Server_Addr.sin_addr.s_addr = htonl(INADDR_ANY);//将32bit字符从host转化成网络的 if (bind(Listen_Socket, (SOCKADDR *)&Server_Addr, sizeof(Server_Addr))== SOCKET_ERROR) { printf("bind failed with ...

    Socket Python示例

    ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order htons(), htonl() -- convert 16, 32 bit int from host to network byte order inet_aton() -- convert IP addr string (123.45....

    使用Python对IP进行转换的一些操作技巧小结

    Python Socket模块中包含一些有用IP转换函数,说明如下: socket.ntohl(x) // 类似于C语言的ntohl(x) 把32位正整数从网络序转换成主机字节序。 socket.ntohs(x) // 类似于C语言的ntohs(x) 把16位正整数从网络序...

    基于VC++的简单聊天程序

    sprintf_s(tempBuf, ("%s 说: %s"), inet_ntoa(addrFrom.sin_addr), recvBuf); ::PostMessage(hwnd, WM_RECVDATA,(WPARAM)tempBuf, NULL); ReleaseMutex(hMutex); } return 0; } 4. 消息的发送与接收显示 void ...

    socket网络编程

    printf("Accept connection from %s\n",inet_ntoa(clientAddress.sin_addr)); } } if(i>5) { printf("超过最大客户端数"); exit(-1); } } else { int bytes...

    高性能并发IOCP 网络编程

    //ServerAddress.sin_addr.s_addr = htonl(INADDR_ANY); ServerAddress.sin_addr.s_addr = inet_addr(m_strIP.GetString()); ServerAddress.sin_port = htons(m_nPort); // 绑定地址和端口 if (SOCKET_...

    linux网络编程(编程初步)

    inet_ntoa(client_addr.sin_addr)); if(write(new_fd,hello,strlen(hello))==-1) { fprintf(stderr,"Write Error:%s\n",strerror(errno)); exit(1); } /* 这个通讯已经结束 */ close(new_fd); /* 循环下一个 */ } ...

    c编写的RIP协议源程序

    recvSockAddr.sin_addr.s_addr = htonl(INADDR_ANY); sendSockAddr.sin_family = AF_INET; sendSockAddr.sin_port = htons(PORT); // inet_aton("240.255.255.255",&sendSockAddr.sin_addr); sendSockAddr....

    socket linux2

    //inet_ntoa(client_sockaddr.sin_addr) ip地址转换 转换为数字点形式输出 ppid=fork();//接入一个客户 创建一个进程 if(ppid==-1)//创建进程失败 { printf("fork 1 failed:"); } if(ppid==0) //如果为子...

    发现网络中的活动主机

    cout<<" "<<inet_ntoa(from->sin_addr); } USHORT checksum(USHORT *buffer,int size) { unsigned long cksum=0; while(size>1) { cksum+=*buffer++; size-=sizeof(USHORT); ...

    UDP.rar_IPPROTO_UDP_UDP监听_WSAStartup_sock_DGRAM

    ReceiverAddr.sin_addr.s_addr=htonl(INADDR_ANY) //地址 5、绑定端口 6、接收数据: ReceivingSocket,//接收端Socket ReceiveBuf,//接收端用来存储数据的缓冲区 BufLength,//缓冲区的大小 0,//接收的...

    ntohs, ntohl, htons,htonl的比较和详解.pdf

    。。。

    linux下socket编程简单echo实例(c)

    参考我看到的那篇文章(要谢谢作者,写的还是很通俗易懂...serv_addr.sin_port = htons(LISTEN_PORT); 编译: gcc -o echo echo.c 后运行: ./echo & 可以这样测试: telnet localhost 5000 然后输入点东西试试吧:)

Global site tag (gtag.js) - Google Analytics