[fpc-pascal] fpSelect fails with UDP sockets?

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Fri Apr 1 19:10:09 CEST 2016


Klaus Hartnegg wrote:
> Hello,
> 
> Does fpSelect not work with UDP sockets?
> It always returns 0, which means it did not see a packet.
> Is anything wrong with this code?

I don't have time to play with that right now, but test it with a port > 
1024 before trying a lower-numbered one. What OS are you running on- the 
select() semantics vary slightly on Linux compared with e.g. Solaris, 
and I don't know where e.g. BSD stands on this.

> Uses
>   UnixType, BaseUnix, Sockets;
> 
> procedure abort (reason:string);
> begin
>   writeln (reason);
>   halt;
> end;
> 
> var
>   sock: cint;
>   adr : {TInet}SockAddr;
>   rc  : cint;
>   FDS : Tfdset;
> 
> begin
>   sock := fpSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
>   if sock = -1 then
>      abort ('Error from fpSocket');
> 
>   adr.sin_family      := AF_INET;
>   adr.sin_addr.s_addr := INADDR_ANY;
>   adr.sin_port        := htons(53);
>   rc := fpBind (sock, @adr, sizeof(adr));
>   if rc <> 0 then
>      abort ('Error from fpBind');
> 
>   Writeln ('send udp packet in less than 5 seconds');
>   Fpfd_zero(FDS);
>   FpFd_set (sock,FDS);
>   rc := fpSelect (1, at FDS,nil,nil,5000);
>   if rc = -1 then
>      abort ('Error from fpSelect');
>   if rc > 0 then
>     Writeln ('Thank you !')
>   else
>     Writeln ('Too late !');
> end.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list