나는 이런 식으로 뭔가가) (선택에서 실행되지 않습니다 : 나는 "타이머가 경과"고 기대했다C : 코드의 일부는
#define QUIT_TIME 5
int main(int argc, char **argv) {
//... SOCKETS STUFF ....
fdmax = parentfd;
while (notdone) {
//Set the timers
waitd.tv_sec = 1;
waitd.tv_usec = 0;
FD_ZERO(&tempreadfds);
FD_ZERO(&tempwritefds);
FD_ZERO(&readfds); /* initialize the read fd set */
FD_ZERO(&writefds); /* initialize the write fd set */
FD_SET(parentfd, &readfds); /* add listener socket fd */
FD_SET(0, &readfds); /* add stdin fd (0) */
tempreadfds = readfds; //make a copy
tempwritefds = writefds; //make a copy
if (select(fdmax+1, &tempreadfds, &tempwritefds, (fd_set*) 0, &waitd) < 0) {
error("ERROR in select");
}
for(i = 1; i <= fdmax; i++) {
if(FD_ISSET(i, &readfds)) {
if(i == parentfd) {
//This is a new connection
childfd = accept(parentfd, (struct sockaddr *) &clientaddr, &clientlen);
if (childfd < 0)
error("ERROR on accept");
InitializeDataStructures(childfd);
FD_SET(childfd, &readfds); //add to the master set
if(childfd > fdmax)
fdmax = childfd;
} else {
//Existing connection
if((nBytes = read(i, connections[i].buffer, MAXBUFFER)) <= 0) {
if(nBytes == 0) {
//Connection closed
printf("Socket %d hung up\n", read_write_loop);
} else {
error("\nReceive error\n");
}
FD_CLR(i, &readfds);
} else {
//We have some data from the connection
//... Manipulate the buffer
//Handle the message
}
}
}
if(FD_ISSET(i, &writefds)) {
.....
FD_CLR(i, &writefds);
}
//Timer checking
if(connections[i].active) {
gettimeofday(&TimeNow, NULL);
timeval_diff(&Interval, &TimeNow, &connections[i].TimeConnected);
printf("*_*_*__*_*_*__*_*_*_*_* difference is %ld seconds, %ld microseconds\n",
Interval.tv_sec,
Interval.tv_usec
);
if(Interval.tv_sec >= QUIT_TIME) {
printf("Timer elapsed!!\n");
}
}
}
}
/* clean up */
printf("Terminating server.\n");
close(parentfd);
return 0;
}
void InitializeDataStructures(int i) {
clients[i].active = YES;
clients[i].fd = i;
//Initialize other members of the structure
}
long long timeval_diff(struct timeval *difference, timeval *end_time, struct timeval *start_time) {
struct timeval temp_diff;
if(difference==NULL)
difference=&temp_diff;
difference->tv_sec =end_time->tv_sec -start_time->tv_sec ;
difference->tv_usec=end_time->tv_usec-start_time->tv_usec;
while(difference->tv_usec<0)
{
difference->tv_usec+=1000000;
difference->tv_sec -=1;
}
return 1000000LL*difference->tv_sec + difference->tv_usec;
}
을 행합니다 (TimeConnected 적어도 한 번 매를 인쇄 할 수 있습니다 실행 중에 if 조건 중 하나로 초기화되었습니다.) 어떤 이유로 인쇄되지 않습니다. 내 while 루프가 계속 인쇄해야한다고 생각했는데 ... 어딘가에서 어수선해도 누구나 알고 있니?
편집 : 사실, 타이머를 사용하여 시간 초과 후 연결을 끊습니다. 방금 다른 클라이언트가 서버에 연결하는 경우 "Timer elapsed"라는 메시지가 출력됩니다. 선택하기 위해 최종 매개 변수를 전달했지만 왜 효과가 없는지 확신 할 수 없습니다.
덕분에 bdk !! 이 코드에있는 "어리석은"버그를 알고 싶다면 아래의 토론을 자세히 읽으십시오 ... 튜토리얼의 한 문장 때문에 간과 한 실수였습니다 : "select modifies 원래의 설명자 ". 변화의
목록 : FD_ZERO 문 세트가 잘못
- 공지 사항 ...
일하고 CODE :
#define QUIT_TIME 5
int main(int argc, char **argv) {
//... SOCKETS STUFF ....
fdmax = parentfd;
FD_ZERO(&readfds); /* initialize the read fd set */
FD_ZERO(&writefds); /* initialize the write fd set */
while (notdone) {
//Set the timers
waitd.tv_sec = 1;
waitd.tv_usec = 0;
FD_ZERO(&tempreadfds);
FD_ZERO(&tempwritefds);
FD_SET(parentfd, &readfds); /* add listener socket fd */
FD_SET(0, &readfds); /* add stdin fd (0) */
tempreadfds = readfds; //make a copy
tempwritefds = writefds; //make a copy
if (select(fdmax+1, &tempreadfds, &tempwritefds, (fd_set*) 0, &waitd) < 0) {
error("ERROR in select");
}
for(i = 1; i <= fdmax; i++) {
if(FD_ISSET(i, &tempreadfds)) {
if(i == parentfd) {
//This is a new connection
childfd = accept(parentfd, (struct sockaddr *) &clientaddr, &clientlen);
if (childfd < 0)
error("ERROR on accept");
InitializeDataStructures(childfd);
FD_SET(childfd, &readfds); //add to the master set
if(childfd > fdmax)
fdmax = childfd;
} else {
//Existing connection
if((nBytes = read(i, connections[i].buffer, MAXBUFFER)) <= 0) {
if(nBytes == 0) {
//Connection closed
printf("Socket %d hung up\n", read_write_loop);
} else {
error("\nReceive error\n");
}
FD_CLR(i, &readfds);
} else {
//We have some data from the connection
//... Manipulate the buffer
//Handle the message
}
}
}
if(FD_ISSET(i, &tempwritefds)) {
.....
FD_CLR(i, &writefds);
}
//Timer checking
if(connections[i].active) {
gettimeofday(&TimeNow, NULL);
timeval_diff(&Interval, &TimeNow, &connections[i].TimeConnected);
printf("*_*_*__*_*_*__*_*_*_*_* difference is %ld seconds, %ld microseconds\n",
Interval.tv_sec,
Interval.tv_usec
);
if(Interval.tv_sec >= QUIT_TIME) {
printf("Timer elapsed!!\n");
}
}
}
}
/* clean up */
printf("Terminating server.\n");
close(parentfd);
return 0;
}
void InitializeDataStructures(int i) {
clients[i].active = YES;
clients[i].fd = i;
//Initialize other members of the structure
}
long long timeval_diff(struct timeval *difference, timeval *end_time, struct timeval *start_time) {
struct timeval temp_diff;
if(difference==NULL)
difference=&temp_diff;
difference->tv_sec =end_time->tv_sec -start_time->tv_sec ;
difference->tv_usec=end_time->tv_usec-start_time->tv_usec;
while(difference->tv_usec<0)
{
difference->tv_usec+=1000000;
difference->tv_sec -=1;
}
return 1000000LL*difference->tv_sec + difference->tv_usec;
}
괄호가 불균형 한 것처럼 보입니다. –
오, 여기에 코드를 입력하고있었습니다 ... 오타가 있었음에 틀림 없습니다 ... 지금 고치십시오 ... – Legend
코드를 복사하여 붙여 넣는 것이 어떨까요? 그러면 실제로 무엇이 있는지 알 수 있습니다. 바보 같은 오타가 생겼어? –