using select with sockets. (solved)
exert from code: (just the essencial)
Code:
fd_set fdset,fdsetbck;
FD_ZERO(&fdsetbck);
FD_SET(sfd,&fdsetbck);
(sfd is a tcp sockect, after listen instruction)
while(1){
memcpy(&fdset,&fdsetbck,sizeof(fdsetbck));
select(maxfd+1, &fdset, NULL, NULL, NULL);
if (FD_ISSET(sfd,&fdset)){
if((newfd=accept(sfd,(struct sockaddr*)&addrs,&addrlens))==-1) ....
FD_SET(newfd,&fdsetbck);
if (newfd>maxfd) maxfd=newfd;
add newfd to descriptors array
...
for cycle testing array of descriptors
if (FD_ISSET(array[n],&fdset))
}
}
my ideia is: each time there is a new connection on the socket a new file descriptor is created and added to the file set. that way if i get a message on the file descritpr used in listen or any of the ones created before i can read from it.
the problem is: when i get the 1st connection on sfd, newfd is created and i can read from it, and add it to the fdset. when it enters 2 loop, the (FD_ISSET(sfd,&fdset)) condition is true again, when i know there was no connections on sfd.
am understanding this wrong, of do i have to flush, or clean, or something like that, sfd, so that select doesn´t think there is a new connection on it?
tks