2012-05-19 2 views
5
#include <stdio.h> 
#include <unistd.h> 
#include <sys/time.h> 
#include <sys/types.h> 

int main() 
{ 
char   name[20]; 
fd_set   input_set; 
struct timeval timeout; 
int    ready_for_reading = 0; 
int    read_bytes = 0; 

/* Empty the FD Set */ 
FD_ZERO(&input_set); 
/* Listen to the input descriptor */ 
FD_SET(0, &input_set); 

/* Waiting for some seconds */ 
timeout.tv_sec = 10; // 10 seconds 
timeout.tv_usec = 0; // 0 milliseconds 

/* Invitation for the user to write something */ 
printf("Enter Username: (in 15 seconds)\n"); 
printf("Time start now!!!\n"); 

/* Listening for input stream for any activity */ 
ready_for_reading = select(1, &input_set, NULL, NULL, &timeout); 
/* Here, first parameter is value of the socket descriptor + 1 (STDIN descriptor is 0, so 
* 0 +1 = 1) 
* in the set, second is our FD set for reading, 
* third is the FD set in which any write activity needs to updated, which is not required 
* in this case. Fourth is timeout 
*/ 

if (ready_for_reading == -1) { 
    /* Some error has occured in input */ 
    printf("Unable to read your input\n"); 
    return -1; 
} else { 
    if (ready_for_reading) { 
     read_bytes = read(0, name, 19); 
     printf("Read, %d bytes from input : %s \n", read_bytes, name); 
    } else { 
     printf(" 10 Seconds are over - no data input \n"); 
    } 
} 

return 0; 

} 동일한 작업을 수행하지만, 그냥 한 번,하지만 발생 후 휴식 무한 루프에 문자열을 '종료'하는 방법루프를 선택하는 방법() 데이터 무한히를 올릴 수

(예를 들어) . 내가 시도한 모든 방법 - 실패했습니다. 10 초 후에 아무 데이터도 입력되지 않으면 "10 초 이상 끝났습니다 - 데이터가 입력되지 않았습니다"라는 메시지가 출력되고 다시 대기 상태가됩니다. 입력 후와 동일 - 다시 시작되고 무한 루프에서 매번 동일한 동작을합니다.
조금만 더 절실하게 생각하십시오. 제발 도와주세요.
감사합니다.

+3

while (! strcmp (name, "quit")) 루프 또는 무엇인가에 모든 것을 넣으시겠습니까? :-) –

+0

"첫 번째 매개 변수는 집합의 FD 수입니다."** 아니요 **. 가장 높은 FD 번호 + 1이어야합니다. 가서 맨 페이지를 다시 확인하십시오. – Mat

+1

http://linux.die.net/man/2/select_tut의 하단에있는 예가 원하는 것입니다. –

답변

4

난 정말 여기에 문제가 표시되지 않습니다. 기본적으로 루프에 원하는 모든 것을 넣고 실행하십시오. 이거 해봤 니?

int main() 
{ 
    /* Declarations and stuff */ 
    /* ... */ 

    /* The loop */ 
    int break_condition = 0; 
    while (!break_condition) 
    { 
     /* Selection */ 
     FD_ZERO(&input_set); /* Empty the FD Set */ 
     FD_SET(0, &input_set); /* Listen to the input descriptor */ 
     ready_for_reading = select(1, &input_set, NULL, NULL, &timeout); 

     /* Selection handling */ 
     if (ready_for_reading) 
     { 
      /* Do something clever with the input */ 
     } 
     else 
     { 
      /* Handle the error */ 
     } 

     /* Test the breaking condition */ 
     break_condition = some_calculation(); 
    } 
    return 0; 
} 

다음 반복에서 다시 응답 할 수 있도록 루프 내의 선택을 계속 재설정해야합니다.

+0

대단히 감사합니다. 마침내 해결되었습니다! :))) 문제는 선택 인수를 한 번만 초기화하고 여러 번 사용하는 것이 아니라 select를 사용할 때마다 초기화 인수를 초기화해야한다는 것입니다. 정말로 대단히 감사합니다. !!!! 인사말 – azrahel

0

시간 초과을 NULL로 설정하여 select() 함수를 무기한 차단하도록 지시 할 수 있습니다. 선택 (2) 매뉴얼 페이지를 참조하십시오 :이 하기 전에 경과해야하는 시간의 상한이다

타임 아웃을()가 반환을 선택합니다. 시간대 구조의 두 필드가 모두 0이면 선택()이 즉시 반환됩니다. 타임 아웃이 NULL (제한 시간 없음) 인 경우 을 선택하면()이 무기한 차단 될 수 있습니다.

그래서 당신이 원하는 무엇인가 :

... 
ready_for_reading = select(1, &input_set, NULL, NULL, NULL); 
... 
관련 문제