Re: TCP Socket keepalive
Jeff Rosenwald <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
Hi Haitao:
Supposing that your client looks something like this:
client_stream(IPAddress, In, Out) :-
tcp_socket(S),
tcp_connect(S, IPAddress, In, Out).
If client_stream/3 succeeds then In and Out will be unified with the appropriate Prolog streams. If it succeeds, then safety requires that you eventually close both streams using "close(Stream, [force(true)])," and you must not release the socket via tcp_close_socket/1 since it is no longer valid.
BUT, it is perfectly reasonable to expect that occasionally the above will throw an exception due the failure to connect. In that case you *must not* close the streams since they are unbound and you must release the socket using tcp_close_socket/1. If you don't then the socket is leaked.
Your client_stream/3 needs to look something like:
client_stream(IPAddress, In, Out) :-
setup_call_catcher_cleanup(tcp_socket(Socket),
tcp_connect(Socket, IPAddress, In, Out),
exception(_),
tcp_close_socket(Socket)).
(Code not tested)
Please see Section 6.2 of the clib docs to see how the socket should be managed at this critical time.
Regards,
Jeff R.
-----Original Message-----
From: Haitao Zhang <[email protected]>
To: Jan Wielemaker <[email protected]>
Cc: swi-prolog <[email protected]>
Sent: Wed, Dec 11, 2013 3:08 am
Subject: Re: [SWIPL] TCP Socket keepalive
Hi Jan,
Thanks for the response. I will take a look at tcp_setopt/setsockopt.
On a related note, after the send error (as the socket closed unexpectedly)
f I call close/1 on the stream, I get the same error. Now that is a little
onfusing. Shouldn't close/1 just silently ignore the error?
cp_close_socket on the other hand seems to gracefully handle the
ituation. When I close down a channel, should I
A) just call tcp_close_socket on the socket, and don't call close on the
tream?
or
B) do both, but wrap the close on the stream to trap the error condition?
t seems weird that one needs to wrap the close/1 since it should be in the
leanup clause of an exception handler.
Haitao
n Tue, Dec 10, 2013 at 12:57 AM, Jan Wielemaker <[email protected]> wrote:
> On 12/10/2013 08:26 AM, Haitao Zhang wrote:
> I have a problem with the TCP socket, where SWI Prolog is client,
> disappearing after some time (presumably some inactivity timer expired). I
> have set my server side to keepalive, but that does not seem to help.
>
> The message I get from trying to use the socket again after a long idle
> time:
> ERROR: format/3: I/O error in write on stream <stream>(000000000834B260)
> (Socket operation on non-socket)
>
> Any idea on how keep the socket alive?
>
There is tcp_setopt/2, which binds to setsockopt(). It doesn't support
the SO_KEEPALIVE option yet. Should be easy enough to add. If you use
a source version, see packages/clib. Try it and submit a patch with
additional options you see fit.
Hope this helps
--- Jan
> Thanks,
> Haitao
> -------------- next part --------------
> HTML attachment scrubbed and removed
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
>
------------- next part --------------
TML attachment scrubbed and removed
______________________________________________
WI-Prolog mailing list
[email protected]
ttps://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
-------------- next part --------------
HTML attachment scrubbed and removed