On Sat, 9 May 2009, Rainer Stratmann wrote:
> Am Samstag, 9. Mai 2009 12:17 schrieb Michael Van Canneyt: > > On Sat, 9 May 2009, Rainer Stratmann wrote: > > > To find out if a nonblocking socket has connected I use the following > > > piece of code: > > > > > > With windows that works, but with linux I get alwas the result that the > > > socket is writable. > > > > > > function is_writable_socket( sck : integer ) : boolean; > > > var > > > fds : tfdset; > > > tv : timeval; > > > begin > > > {$ifdef linux} fpfd_zero( fds ); fpfd_set( sck , fds ); {$endif} > > > {$ifdef windows} fd_zero( fds ); fd_set( sck , fds ); {$endif} > > > tv.tv_sec := 0; > > > tv.tv_usec := 0; > > > // socket+1 , read , write , except , timeout > > > {$ifdef linux} > > > result := fpselect( sck + 1 , nil , @fds , nil , @tv ) > 0; > > > {$else} > > > result := select( sck + 1 , nil , @fds , nil , @tv ) > 0; > > > {$endif} > > > end; > > > > And why do you think that this is a bug ? > I know that the socket I tested is not writable, but the function returns > writable... always with linux. > > It is not working on linux operating system. With windows os that works. > > Someone else have (had?) exactly the same problem: > http://community.freepascal.org:10000/bboards/message?message_id=270583&forum_id=24083 The call functions correctly, IMHO, but the behaviour of the call is not as you expect. Select does *not* tell you if a file descriptor is writeable or readable. It tells you if the read/write operation will block. This is something subtly different. For instance, it reports file descriptor 0 as writable, which is rather strange, since it is read-only, but correct, because the write call will not block. Indeed the write call will return at once with an error condition. This is what the kernel sees (I used strace to test your code with descriptor 0): select(1, NULL, [0], NULL, {0, 0}) = 1 (out [0], left {0, 0}) Which is exactly what I sent. In short, is_writable_socket should be implemented differently, not using select. Michael. _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal