fyplinux
April 2nd, 2008, 11:57 PM
I am using select in such a way:
.....
fd_set confds;
struct timeval contv;
int conretval;
FD_ZERO(&confds);
FD_SET(local_sock, &confds);//set the pipe of send/receive
contv.tv_sec = TIME_OUT;//timeout
contv.tv_usec = 0;
printf("select : waiting the connection to be established\n");
conretval = select(local_sock+1, NULL, &confds, NULL, &contv);//after this,the sets and timeout become undefined
printf("select returned\n");
if(conretval > 0){
if(FD_ISSET(local_sock,&confds)){
printf("local_sock is in set\n");
if(write(local_sock,"hello",5) == -1){
perror("Connect to a peer ");
constat = 0;
}
//read ack
char *hello = calloc(6,sizeof(char));
if(read(local_sock,hello,5) == -1){
perror("Connect to a peer ");
constat = 0;
}
free(hello);
}
else{
printf("Connect to %s time out\n", ipbuf);
constat = 0;
}
}
else{
printf("Connect to %s time out\n", ipbuf);
constat = 0;
}
....
The message sequence could be shown:
select : waiting the connection to be established
select returned
local_sock is in set
then the program is blocked for a long time(about 3 mins) to send the message.
Could you help me to find the reason?
.....
fd_set confds;
struct timeval contv;
int conretval;
FD_ZERO(&confds);
FD_SET(local_sock, &confds);//set the pipe of send/receive
contv.tv_sec = TIME_OUT;//timeout
contv.tv_usec = 0;
printf("select : waiting the connection to be established\n");
conretval = select(local_sock+1, NULL, &confds, NULL, &contv);//after this,the sets and timeout become undefined
printf("select returned\n");
if(conretval > 0){
if(FD_ISSET(local_sock,&confds)){
printf("local_sock is in set\n");
if(write(local_sock,"hello",5) == -1){
perror("Connect to a peer ");
constat = 0;
}
//read ack
char *hello = calloc(6,sizeof(char));
if(read(local_sock,hello,5) == -1){
perror("Connect to a peer ");
constat = 0;
}
free(hello);
}
else{
printf("Connect to %s time out\n", ipbuf);
constat = 0;
}
}
else{
printf("Connect to %s time out\n", ipbuf);
constat = 0;
}
....
The message sequence could be shown:
select : waiting the connection to be established
select returned
local_sock is in set
then the program is blocked for a long time(about 3 mins) to send the message.
Could you help me to find the reason?