UNIX 字节序转换函数(ntohl,ntohs, htonl, htons) 实现问题

lvciana 2009-02-18 04:43:23
上述四个函数在<arpa/inet.h>中定义如下:
#define ntohl(x) (x)
#define ntohs(x) (x)
#define htonl(x) (x)
#define htons(x) (x)

请问如上空的宏定义,是如何实现字节序的转换?
...全文
1501 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxl_c0s0d0n 2012-03-19
  • 打赏
  • 举报
回复
小哥,你确定正解太草率了,完全不对,更本不是那几个macro来操作的,netinet/in.h里面有函数的声明,那才是正确的接口,其实ntoh和hton只想同一个函数,glibc用weak_alias作的alias
lvciana 2009-04-11
  • 打赏
  • 举报
回复
呵呵,找到正解,原文如下:
When using these functions we do not care about the actual values ( big endian or little endian ) for the host byte order and the network byte order. What we must do is be certain to call the appropriate function to convert a given value between the host and network byte order. ON THOSE SYSTERMS THAT HAVE THE SAME BYTE ORDERING AS THE INTERNET PROTOCOLS ( BIG ENDIAN ), THESE FOUR FUNCTIONS ARE USUALLY DEFINED AS NULL MACROS.

原文来自《Unix Network Programming Vol 1》 Second Edition, by Richard Stevens, Page 82

呵呵,也就是说我看到的系统的字节序和网络字节序是相同的,因此无需转换,因此为空的宏定义。

challenge99, baihacke 的答案,正解,呵呵~

谢谢大家,散分结贴。
waizqfor 2009-02-18
  • 打赏
  • 举报
回复
帮顶!~
  • 打赏
  • 举报
回复
<arpa/inet.h>里的这4个宏真的什么都没做?
是不是有地方没看全?
baihacker 2009-02-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]
C/C++ code基本操作:
a = (a >> 8) | (a << 8) (short)
[/Quote]
额,看错...不是问如何实现...
由于系统原因,不需要处理呗.
baihacker 2009-02-18
  • 打赏
  • 举报
回复
基本操作:
a = (a >> 8) | (a << 8) (short)

challenge99 2009-02-18
  • 打赏
  • 举报
回复
根本就没有作转换 ^o^

莫非就是大端的系统? 等待解释
适合初学者 /******* 服务器程序 (server.c) ************/ #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; int main(int argc, char *argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]="Hello! Are You Fine?\n"; if(argc!=2) { fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); exit(1); } if((portnumber=atoi(argv[1]))<0) { fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); exit(1); } /* 服务器端开始建立socket 描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); exit(1); } /* 服务器端填充 sockaddr 结构 */ bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; 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) { /******* 客户端程序 client.c ************/ #include ; #include ; #include ; #include ; #include ; #include ; #include ; #include ; int main(int argc, char *argv[]) { int sockfd; char buffer[1024]; struct sockaddr_in server_addr; struct hostent *host; int portnumber,nbytes; if(argc!=3) { fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]); exit(1); } if((host=gethostbyname(argv[1]))==NULL) { fprintf(stderr,"Gethostname error\n"); exit(1); } if((portnumber=atoi(argv[2]))<0) { fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]); exit(1); } /* 客户程序开始建立 sockfd 描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } /* 客户程序填充服务端的资料 */ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(portnumber); server_addr.sin_addr=*((struct in_addr *)host->;h_addr); /* 客户程序发起连接请求 */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr) )==-1) { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } /* 连接成功了 */ if((nbytes=read(sockfd,buffer,1024))==-1) { fprintf(stderr,"Read Error:%s\n",strerror(errno)); exit(1); } buffer[nbytes]='\0'; printf("I have received:%s\n",buffer); /* 结束通讯 */ close(sockfd); exit(0); }

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧