菜鸟提问_怎样才能在linux下面获得ip地址

guaiwei 2004-11-10 11:50:03
各位大侠,小弟刚刚接触linux,很多关于系统的东西一点儿都弄不明白,现在要实现系统信息的访问,比如要获得ip地址,不知道怎样才能实现,还有就是如果要弄懂这些东西,应该看看什么方面的书籍。谢谢指教
...全文
154 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingzai 2004-11-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <netdb.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define DEBUG 1
#define MAXMSG 560

struct tm tmn;
time_t now;

int main(int argc,char *argv[])
{
int sockfd;
char SOCKIP[15];
char SOCKPORT[6];
char MyName[20];
char MSG[MAXMSG];
char *IP, *hostname, *ptr;
struct hostent *HostIP;
struct sockaddr_in dest;
struct in_addr inip;

/* 得到该运行程序所在主机的主机名称 */
if( gethostname(MyName,sizeof(MyName))<0 )
{
printf("Gethostname Error\n");
return -1;
}

#if DEBUG
printf("My Host Name is %s\n",MyName);
#endif

if(!(HostIP = gethostbyname(MyName)))
{
printf("Get host name failed.\n");
return -1;
}

/* 得到该运行程序所在主机的IP地址(形如 *.*.*.* ) */
memset((void *)&inip, sizeof(inip), 0);
hostname=HostIP->h_name;
inip.s_addr=*((unsigned long *)HostIP->h_addr_list[0]);

if(!(ptr=(char *)inet_ntoa(inip)))
memcpy(&inip,HostIP->h_addr,HostIP->h_length);

#if DEBUG
printf("My IP is %s\n",ptr);
#endif

return 0;
}
ChChip 2004-11-16
  • 打赏
  • 举报
回复
gethostbyname函数
guaiwei 2004-11-10
  • 打赏
  • 举报
回复
我的意思是用c程序编程实现,呵呵
icecools 2004-11-10
  • 打赏
  • 举报
回复
ifconfig
guaiwei 2004-11-10
  • 打赏
  • 举报
回复
刚刚查过了,没有关于linux下面C编程获得IP地址的文档阿
guaiwei 2004-11-10
  • 打赏
  • 举报
回复
thank blh.
blh 2004-11-10
  • 打赏
  • 举报
回复
ioctl
man ioctl_list

I had answer this question more times and you only search correlative topic in this forum .

4,438

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧