中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
请问怎么用inet_addr()?
作者:未知 时间:2005-09-13 23:29 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:请问怎么用inet_addr()?
..

一看就知道你缺少基础知识。先看看这个:
http://www.linuxsir.org/bbs/showthr...ht=%BF%CD%BB%A7
再看看语法:
http://www.mkssoftware.com/docs/man3/inet_addr.3.asp
nvert a string like "192.168.0.1" to a integer

struct sockaddr_in sock;
/*...*/
sock.sin_addr.s_addr=inet_addr("192.168.0.1");
/*...*/
谢谢两位SIR,上面的代码和给的链接看得不是很明白。这是我写的,请帮看看错在哪里?

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
main ()
{
char in_addr_t ;
in_addr_t=inet_addr(" 211.100.7.101");
printf ("%s\n",in_addr_t);
}
in_addr_t是一定义好的类型,
在/usr/include/netinet/in.h

typedef uint32_t in_addr_t;
main ()
{
in_addr_t addr ;
addr=inet_addr(" 211.100.7.101");
printf ("%d\n",addr);
}
      1 #include <sys/socket.h>
      2 #include <netinet/in.h>
      3 #include <arpa/inet.h>
      4
      5 main ()
      6 {
      7   in_addr_t addr ;
      8   addr=inet_addr("211.92.88.40");
      9 //        addr = inet_addr("127.0.0.1");
     10   printf ("%d\n",addr);
     11 }
我成功收到值了。呵呵
676879571
可惜看不到字符串。是怎么看到的啊。?
为什么我不行,出了一大堆的错误?
这是源代码:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
main()
in_addr_t addr;
addr=inet_addr("211.100.7.101");
printf("%d\n",addr);
}

# gcc -c inet_addr.c
In file included from inet_addr.c:1:
/usr/include/sys/socket.h:52: syntax error before `sa_family_t'
/usr/include/sys/socket.h:52: warning: data definition has no type or storage cl
ass
/usr/include/sys/socket.h:163: syntax error before `u_char'
/usr/include/sys/socket.h:174: syntax error before `u_short'
/usr/include/sys/socket.h:188: syntax error before `u_char'
/usr/include/sys/socket.h:190: `int64_t' undeclared here (not in a function)
/usr/include/sys/socket.h:190: `u_char' undeclared here (not in a function)
/usr/include/sys/socket.h:190: size of array `__ss_pad1' is too large
/usr/include/sys/socket.h:191: syntax error before `int64_t'
/usr/include/sys/socket.h:192: `u_char' undeclared here (not in a function)
/usr/include/sys/socket.h:192: `int64_t' undeclared here (not in a function)
/usr/include/sys/socket.h:192: `u_char' undeclared here (not in a function)
/usr/include/sys/socket.h:192: `int64_t' undeclared here (not in a function)
/usr/include/sys/socket.h:359: syntax error before `pid_t'
/usr/include/sys/socket.h:364: syntax error before `gid_t'
/usr/include/sys/socket.h:399: syntax error before `u_short'
/usr/include/sys/socket.h:407: syntax error before `caddr_t'
/usr/include/sys/socket.h:411: syntax error before `caddr_t'
In file included from inet_addr.c:1:
/usr/include/sys/socket.h:444: syntax error before `recv'
/usr/include/sys/socket.h:444: syntax error before `size_t'
/usr/include/sys/socket.h:444: warning: data definition has no type or storage c

inet_addr.c: In function `main':
inet_addr.c:6: syntax error before `addr'
看样子你好像没有装头文件似的
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有