博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
两段Linux代码
阅读量:2220 次
发布时间:2019-05-08

本文共 1467 字,大约阅读时间需要 4 分钟。

 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); 
}

转载地址:http://thnfb.baihongyu.com/

你可能感兴趣的文章
幂等性验证思想
查看>>
DB理论--数据存储方式
查看>>
PB协议的说明与使用
查看>>
什么是TPS,什么是QPS,区别是什么?
查看>>
git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:
查看>>
arraylist扩容时机java8
查看>>
logback中additivity的理解
查看>>
一篇文章搞懂hash,hashcode,equals,==的用法
查看>>
mysql数据库,悲观锁。for update 的用法。
查看>>
springboot+jta+atomikos多数据源和 springboot+mybatisplus+aop实现数据库读写分离而引发的一些思考
查看>>
java面试中常考的一些面试sql语句
查看>>
一个字节等于多少位?
查看>>
帧框架frameset的用法总结
查看>>
java1.8中创建hashmap的初始化大小设置标准
查看>>
mark一下,service的实现层没有加@service注解。
查看>>
jq对象转换成js对象。已经jq的复合选择器。
查看>>
(一)alin‘s mysql学习笔记----概述
查看>>
(二)alin’s mysql学习笔记----mysql的存储引擎
查看>>
(三)alin’s mysql学习笔记----常用的join连接查询
查看>>
(四)alin’s mysql学习笔记----索引简介
查看>>