請問怎么用inet_addr()?
發表于:2007-05-26來源:作者:點擊數:
標簽:
.. 一看就知道你缺少基礎知識。先看看這個: 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.kjueaiud.com
- 評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
-