[racket-dev] Implementation question

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Apr 20 14:39:03 EDT 2014

Here is a more Racket-y version of this: 

#lang racket

(define (ping hostname port-no personalip)
  (define c (make-custodian))
  (define t
    (parameterize ((current-custodian c))
      (thread
       (lambda ()
         (with-handlers ((exn:fail:network? 
                          (lambda (x)
                            (printf "~a:~a ~a\n" hostname port-no " NO"))))
           (define-values (in out) (tcp-connect hostname port-no))
           (write "'ping" out)
           (write personalip out)
           (flush-output out)
           (printf "~a:~a ~a\n" hostname port-no (read in)))))))
  (sync/timeout 0.01 t)
  (custodian-shutdown-all c))




On Apr 20, 2014, at 10:41 AM, nicolas carraggi wrote:

> Hey guys,
> 
> Thanks a lot for your quick answers!
> 
> My ping method now:
> 
> (define (ping hostname port-no personalip)
>   (define t (thread
>              (lambda ()
>                (with-handlers ((exn:fail:network? (lambda (x) (begin (displayln (string-append hostname ":" (number->string port-no) " NOOOOOOOOOO")) #f))));(displayln (exn-message x)))))
>                  (define-values (in out) (tcp-connect hostname port-no))
>                  (write "'ping" out)
>                  (write personalip out)
>                  (flush-output out)
>                  (display (string-append hostname ":" (number->string port-no) " "))
>                  (displayln (read in))
>                  (close-input-port in)
>                  (close-output-port out)
>                  #t))))
>   (sync/timeout 0.01 t)
>   (kill-thread t))
> 
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> This is the result when I do a multicast from 192.168.1.0 to 192.168.1.200. ( a lot of these don't exist and that's why tcp-connect was taking ages to throw an error.) Those returning a NOOOOO are existing network nodes where no server is running. Pong is the answer from the server running on my laptop.
> 
> > (multicast-ping "192.168.1.100" 8080 0 200)
> 192.168.1.0:8080 NOOOOOOOOOO
> 192.168.1.105:8080 NOOOOOOOOOO
> 192.168.1.108:8080 pong
> -------------------
> Multicast finished!
> 
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> 
> It's still test-code but it's working fine now, maybe the timout time is too small but now it works well!
> 
> Greetings,
> Nicolas
> 
> Subject: Re: [racket-dev] Implementation question
> From: matthias at ccs.neu.edu
> Date: Sat, 19 Apr 2014 09:25:27 -0400
> CC: nicocarraggi at hotmail.com; dev at racket-lang.org
> To: laurent.orseau at gmail.com
> 
> 
> Let me recommend events instead: 
> 
> #lang racket
> 
> ;; Nat -> Void 
> ;; wait for t seconds before connecting to google.com, then stop
> (define (do-work t)
>   (thread
>    (lambda ()
>      (with-handlers ((exn:fail:network? (lambda (x) (displayln (exn-message x)))))
>        (sleep t)
>        (define-values (in out) (tcp-connect "google.com" 80)) 
>        'done))))
> 
> ;; returns #f if 3 seconds pass w/o the thread shutting down 
> (sync/timeout 3 (do-work (random 6)))
> 
> 
> 
> On Apr 19, 2014, at 8:34 AM, Laurent wrote:
> 
> One simpler possibility is to use `tcp-connect/enable-break` and run a timer in parallel to break it after a shorter delay.
> For example:
> 
> (define-values (in out) (values #f #f))
> 
> (define connect-thread
>   (thread
>    (λ()(set!-values (in out) 
>                     (tcp-connect "www.google.com" 80)))))
> 
> (sleep 3)
> (unless in
>   (displayln "Connection not established. Breaking thread.")
>   (break-thread connect-thread))
> 
> The timer can also be place into its own thread if you need to set up several connections in parallel.
> 
> Hope this helps,
> Laurent
> 
> 
> On Thu, Apr 17, 2014 at 6:48 PM, nicolas carraggi <nicocarraggi at hotmail.com> wrote:
> Hello,
> 
> I am actually using "Racket/tcp" for a project in Racket.
> I'm creating a peer-to-peer network but now I encountered a small problem.
> For a multicast I want to connect with each server on the same network. 
> For this I use "tcp-connect", but when i try to connect to an ip address which is not hosting a server, throwing the error only happens after more than 1 minute. So I would like to use a modified "tcp-connect" with a smaller time-out.
> 
> Where can I find the implementation of "tcp-connect"?
> 
> I already found "tcp.rkt" but there it gets the "tcp-connect" method from "'#%network" but I can't find this one...
> 
> Greetings!
> Nicolas
> 
> _________________________
>   Racket Developers list:
>   http://lists.racket-lang.org/dev
> 
> 
> _________________________
>  Racket Developers list:
>  http://lists.racket-lang.org/dev
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/dev/archive/attachments/20140420/2ce080c8/attachment.html>

Posted on the dev mailing list.