d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Socket Select() Not Working Properly?
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Dec 7 2014 10:54am
I'm using select to see if there's anything to be read in from stdin or from the socket I'm listening on.

For some reason, it never acknowledges that the socket on which I'm listening on has data waiting.

Not really sure why, it doesn't seem to be blocking and it I can read messages sent if I do it outside the FD_ISSET if statement for the socket.

Also it acknowledges if there is something to be read from stdin in the FD_ISSET if statement for stdin.

Code
int retval;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(fileno(stdin), &readfds); // stdin fd(0)
FD_SET(socket, &readfds); // socket listening on

retval = select(socket + 1, &readfds, NULL, NULL, &tv);

if(FD_ISSET(0, &readfds)){
fgets(buf, 1024, stdin);
printf("%s\n", buf);
fflush(stdout);
}
if(FD_ISSET(socket + 1, &readfds)){
printf("message received");
fflush(stdout);
}


This post was edited by lopelurag on Dec 7 2014 11:05am
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Dec 7 2014 11:30am
The second FD_ISSET should be, sorry mistyped it when I posted this but can't edit it

Code
if(FD_SET(socket, &readfds)){
//some code
}


This post was edited by lopelurag on Dec 7 2014 11:31am
Member
Posts: 22,346
Joined: Sep 21 2007
Gold: 145.06
Dec 7 2014 01:45pm
From abduct:

Select() is not meant to block, or rather not block for a significate amount of time. As per the &tv value you passed you are timing the funciton out at 0 seconds. This means if there is no FILE DESCRIPTOR ready to be read or written to before the time out is reached then it will return nothing. This explains the behavour you are getting. If you wish to correct this simply install your code within a loop so that select is called every iteration, or expand the timeout so that it gets a chance to look for descriptors that are in need of reading.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Dec 7 2014 03:53pm
Quote (Toilet @ Dec 7 2014 03:45pm)
From abduct:

Select() is not meant to block, or rather not block for a significate amount of time. As per the &tv value you passed you are timing the funciton out at 0 seconds. This means if there is no FILE DESCRIPTOR ready to be read or written to before the time out is reached then it will return nothing. This explains the behavour you are getting. If you wish to correct this simply install your code within a loop so that select is called every iteration, or expand the timeout so that it gets a chance to look for descriptors that are in need of reading.


Yea sorry the codes in a while loop, so it runs continuously until I manually shut down the program.

Sorry guess I should have made it clearer. In my code I do have the FD statements inside of a while loop as shown below.

Code

int retval;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
while(;;){
FD_ZERO(&readfds);
FD_SET(fileno(stdin), &readfds); // stdin fd(0)
FD_SET(socket, &readfds); // socket listening on

retval = select(socket + 1, &readfds, NULL, NULL, &tv);

if(FD_ISSET(0, &readfds)){
fgets(buf, 1024, stdin);
printf("%s\n", buf);
fflush(stdout);
}
if(FD_ISSET(socket + 1, &readfds)){
printf("message received");
fflush(stdout);
}
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll