LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 5897|回复: 9

请问怎么用inet_addr()?

[复制链接]
发表于 2003-12-19 15:46:27 | 显示全部楼层 |阅读模式
请给个简单的例子!!!先谢了。
发表于 2003-12-19 16:08:26 | 显示全部楼层
一看就知道你缺少基础知识。先看看这个:
http://www.linuxsir.cn/forum.php ... hlight=%BF%CD%BB%A7
再看看语法:
http://www.mkssoftware.com/docs/man3/inet_addr.3.asp
发表于 2003-12-19 17:45:50 | 显示全部楼层
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");
/*...*/
 楼主| 发表于 2003-12-19 20:13:18 | 显示全部楼层
谢谢两位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);
}
发表于 2003-12-19 20:53:25 | 显示全部楼层
in_addr_t是一定义好的类型,
在/usr/include/netinet/in.h

typedef uint32_t in_addr_t;
发表于 2003-12-19 20:54:14 | 显示全部楼层
main ()
{
in_addr_t addr ;
addr=inet_addr(" 211.100.7.101");
printf ("%d\n",addr);
}
发表于 2003-12-19 21:44:22 | 显示全部楼层

  1.       1 #include <sys/socket.h>
  2.       2 #include <netinet/in.h>
  3.       3 #include <arpa/inet.h>
  4.       4
  5.       5 main ()
  6.       6 {
  7.       7   in_addr_t addr ;
  8.       8   addr=inet_addr("211.92.88.40");
  9.       9 //        addr = inet_addr("127.0.0.1");
  10.      10   printf ("%d\n",addr);
  11.      11 }
复制代码

我成功收到值了。呵呵
676879571
可惜看不到字符串。是怎么看到的啊。?
发表于 2003-12-19 21:57:43 | 显示全部楼层
为什么我不行,出了一大堆的错误?
这是源代码:
#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'
发表于 2003-12-21 15:21:59 | 显示全部楼层
看样子你好像没有装头文件似的
发表于 2003-12-21 17:54:45 | 显示全部楼层
不明白阿,我是照抄上面的了。:ask:ask
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表