[usocket-devel] socket-connect and hostnames resolved to multiple addresses

Chun Tian (binghe) binghe.lisp at gmail.com
Thu Mar 17 18:10:33 UTC 2011


Hi, Nikodemus

Thanks very much, your code give me a very good "template".

I think maybe such code should live outside of USOCKET library itself, because the connection logic various among different applications. For example, suppose there's a hostname which can be resolved into more than 10 different addresses, but the network application want to try at most 3 of those addresses before signal a error. (the theory may be this: if 3 of 10 was down, all was down, no time waste to try the other 7)

So, my job (as a USOCKET maintainer) is to make sure SOCKET-CONNECT on all supported CL platforms could report the same USOCKET-defined error type (i.e. USOCKET:CONNECTION-REFUSED-ERROR), so that something like following code could be written in user code:

(defun socket-connect-2 (host port)
  (let ((addresses (usocket::get-hosts-by-name host)))
    (tagbody
     :connect
     (let ((address (pop addresses)))
       (handler-bind ((usocket:connection-refused-error
                       #'(lambda (c)
                           (declare (ignore c))
                           (when addresses (go :connect)))))
         (usocket:socket-connect address port))))))

Regards,

Chun Tian (binghe)

在 2011-3-17,16:02, Nikodemus Siivola 写道:

> Here's what I would consider one idiomatic way to try all addresses on
> using SB-BSD-SOCKETS.
> 
> (defun connect-to-host (socket host port)
>  (let ((addresses (host-ent-addresses (get-host-by-name host))))
>    (tagbody
>     :connect
>       (let ((addr (pop addresses)))
>         (handler-bind ((socket-error
>                         (lambda (e) (when addresses (go :connect)))))
>           (socket-connect socket addr port))))))
> 
> Cheers,
> 
> -- Nikodemus





More information about the usocket-devel mailing list