请问怎么用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; /*...*/
..
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'
原文转自:http://www.ltesting.net
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
|