我使用一个程序从接收到的数据包中提取源地址。但我不明白,在下面的代码中使用&运营商:为什么我必须在&ip_reply-> saddr中使用“&”
==>函数的地址转换
struct in_addr {
unsigned long int s_addr; }
char *inet_ntoa(struct in_addr in);
==>接收数据包
recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen)) == -1)
===>显示源地址
struct iphdr* ip_reply;
/*code */
ip_reply = (struct iphdr*) buffer;
/*code */
printf("RCV ip->daddr = %s \n",inet_ntoa(*((struct in_addr *)&ip_reply->saddr)));
为什么我必须在012中使用?
如果您不知道如何/何时/为什么要使用addressof运算符,那么您几乎不知道内存管理。您应该先学习C的基础知识,然后才能制作复杂的程序(如TCP/IP通信)。 – 2013-07-05 10:38:42
或者你也可以通过编写提供Unix系统调用接口的更高级语言来学习TCP/IP。在[Ocaml](http://ocaml.org/)中(使用其[Unix](http://caml.inria.fr/pub/docs/manual-ocaml-4.00/libref/Unix.html)模块)。这可以帮助您从低级手动管理中解脱出来。 –