两段Linux代码

 1 正常的Linux输入,必须要回车才能接收,使用这个函数可以做到每次响应。

不过一个BUG就是printf的时候,必须要加一个'/n'才能输出,原因不明。

void get_callee_number(char* buf)
{
 fd_set keyset;
 struct timeval tv;
 int keyfd = fileno(stdin), ret;
 char c;
  struct termios save, ne;
  int i = 0;
  
 ioctl(0, TCGETS, &save);
  ioctl(0, TCGETS, &ne);
  ne.c_lflag &= ~(ECHO | ICANON);
  ioctl(0, TCSETS, &ne);
 
 while(1)
 {
  tv.tv_sec = 2;
  tv.tv_usec = 0;
  FD_SET(keyfd, &keyset);
  ret = select(keyfd + 1, &keyset, NULL, NULL, &tv);
  if(ret == 0 && strlen(buf) != 0)
   break;
  if(FD_ISSET(keyfd, &keyset))
  {
   read(keyfd, &c, 1);
   buf[i++] = c;
   //printf("the key is %c/n", c);
  }
  if(c != 0)printf("%c/n", c);
  
 }
 ioctl(0, TCSETS, &save);
}

2 得到本地外网IP(第一张网卡)

void get_local_ip(int family, char *address, int size)
{
  int fd, intrface; 
 struct ifreq buf[16]; 
 //struct arpreq arp; 
 struct ifconf ifc; 
 
 memset(address, 0, size);
 
 if ((fd = socket (family, SOCK_DGRAM, 0)) >= 0) 
 { 
  ifc.ifc_len = sizeof buf; 
  ifc.ifc_buf = (caddr_t) buf; 
  if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) 
  { 
   intrface = ifc.ifc_len / sizeof (struct ifreq)-1; 
   //获取当前网卡的IP地址 
   if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface]))) 
   {
    //strcpy(address, inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
    snprintf(address, size, (char *)inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
   } 
   else 
   { 
    char str[256]; 
    sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name); 
    perror (str); 
   } 
  }else 
   perror ("cpm: ioctl"); 
 }else 
  perror ("cpm: socket"); 
 close (fd); 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值