[fpc-pascal] "Aborting " a socket operation under Linux

Rainer Stratmann RainerStratmann at t-online.de
Fri May 29 22:16:14 CEST 2009


Am Freitag, 29. Mai 2009 20:29 schrieb Luca Olivetti:
> En/na Rainer Stratmann ha escrit:
> > May be socketoption so_linger would be a solutuion.
> > http://www.developerweb.net/forum/archive/index.php/t-2982.html
>
> Nope, no change.

> I'm testing a connect to a non existent host in the thread and it won't
> terminate before the (quite long, 3 minutes, since it ignores the 5
> seconds I set) timeout, no matter what the value of linger->l_onoff

In case of connection to a host:
Simply use nonblocking socket mode, than you get immediately a result.
And then you can write your own timeout function.


procedure setnonblockingsocket( s : integer );
var  arg : longint;
begin
  arg := fpfcntl( s , F_GETFL );
  if arg >= 0 then begin
   arg := arg or O_NONBLOCK;
   fpfcntl( s , F_SETFL , arg );
  end;
end;

...
timeout := 0;
repeat
 c := fpconnect( c_socket , @inetaddr_client_remote , 
sizeof( inetaddr_client_remote ) );
 timeout := gettimeoutval;
until ( c = 0 ) or timeout;

> Thanks anyway
>
> > Rainer
> >
> > Am Freitag, 29. Mai 2009 18:43 schrieb Luca Olivetti:
> >> I have a thread managing a socket (using synapse but I don't think that
> >> it matters, just keep in mind that each operation is blocking).
> >> Inside the thread, I try to connect, send and read in a loop.
> >> If I see an error, I just exit the loop and, if
> >> the thread is terminated, that's it, otherwise I try a new connection
> >> and so on and so forth.
> >> In the thread destructor (called in the context of the main thread),
> >> apart from terminate, I use the synapse AbortSocket call (which in turn
> >> calls CloseSocket, which in turn calls fpClose under linux), which
> >> supposedly terminates any pending call on the socket in the thread, so
> >> it can terminate immediately.
> >> It works fine under windows (i.e the socket calls terminate immediately
> >> inside the thread), but under Linux it doesn't until the timeout
> >> expires, and that's not very convenient, especially if I'm trying to
> >> quickly close the program and I have many of those threads running.
> >> Is there a way to make the pending call exit immediately?
> >>
> >> Bye
> >
> > _______________________________________________
> > fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> > http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal



More information about the fpc-pascal mailing list