Re: select(), sending and receiving data



Markus Pitha <markus@xxxxxxxxxx> writes:

> Hello,
>
> Nils O. Selåsdal wrote:
>> Remember also that select modifies the descriptor sets, be sure
>> you initialize the sets again on each iteration.
>
> Actually I tried a lot of ways with setting and clearing the sets but I
> always go in circles:
> When I only set FD_SET(sock, &readfds); (and not set FD_SET(sock,
> &writefds);) I receive the welcoming message of the server, but then I
> can never write. When I switch on both, I can write, but I don't receive
> the welcoming message at the beginning of the connection. It doesn't
> appear until I pressed ENTER the first time. (after I typed something).
> I think my main problem is that I dont know, how to control the SETs
> with the flags and return values.

Read again the man page of select(2) !

You must realize that fgets blocks. Instead of calling fgets when
there is no input from the user, you might want to put stdin on
readfds too.

loop
readfds={socks,fileno(stdin)}
writefds={socks,fileno(stdout)}
select
when stdin in readfds
fgets -> buffer.tosend
when stdout in writefds and buffer.toprint
printf buffer.toprint
when socks in readfds
recv -> buffer.toprint
when socks in writefds and buffer.tosend
send buffer.tosend

You should beware that stdin and stdout are buffered, and their
underlying file descriptors can be out of sync; use fflush and read
setbuf(3).

--
__Pascal Bourguignon__ http://www.informatimago.com/

"Our users will know fear and cower before our software! Ship it!
Ship it and let them flee like the dogs they are!"
.