本文共 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/