如何在 c 語言中取得 ubuntu 的 ip [論壇 - Ubuntu 程式設計]
正在瀏覽:
1 名遊客
cris
|
如何在 c 語言中取得 ubuntu 的 ip |
|
---|---|---|
Anon:cris
|
我的電腦在ip分享器下每次都會取得不同的ip
如 192.168.1.10, 192.168.1.15等等 每次編譯都要查看目前ip是什麼 請問是否有辦法直接取得ip位址呢? 謝謝
2010/4/30 10:49
| ea0ef c1d3c 19f56 b751b
|
|
![]() |
回覆: 如何在 c 語言中取得 ubuntu 的 ip |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員二級
![]() ![]() 註冊日期:
2009/11/23 13:39 所屬群組:
已註冊使用者 等級: 6
HP : 0 / 147
![]() |
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <net/if_arp.h> #include <arpa/inet.h> #include <errno.h> #define ETH_NAME "eth0" int main() { int sock; struct sockaddr_in sin; struct ifreq ifr; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); return -1; } strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0; if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { perror("ioctl"); return -1; } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); fprintf(stdout, "eth0: %s\n", inet_ntoa(sin.sin_addr)); return 0; } http://www.52language.com/Article/17054.html 這是我google 龜到 不知道是不是你想要的 我愛龜,因為龜讓得學到的更多
2010/4/30 11:51
|
||||||||||
![]() |
回覆: 如何在 c 語言中取得 ubuntu 的 ip |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員二級
![]() ![]() 註冊日期:
2008/12/18 22:41 所屬群組:
已註冊使用者 等級: 6
HP : 0 / 126
![]() |
WOW
2010/4/30 12:49
|
||||||||||
![]() |
回覆: 如何在 c 語言中取得 ubuntu 的 ip |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員一級
![]() ![]() 註冊日期:
2010/4/20 15:21 所屬群組:
已註冊使用者 等級: 1
HP : 0 / 2
![]() |
我是用很懶的方法
FILE *ptr = popen("ifconfig eth0", "r"); 然後在ptr裡一的其中一行就會有ip位置 pclose(ptr); 這樣
2010/4/30 13:28
|
||||||||||
![]() |
您可以查看帖子.
您不可發帖.
您不可回覆.
您不可編輯自己的帖子.
您不可刪除自己的帖子.
您不可發起投票調查.
您不可在投票調查中投票.
您不可上傳附件.
您不可不經審核直接發帖.