Re: [racket-users] Re: Detecting broken inbound TCP connections

2022-07-01 Thread George Neuner

Hi Jeff,

Note that most network problems result in an exception ... which your 
code is not catching and which you might have missed seeing in the 
output.  You need to catch *exn:fail:network* and examine the *errno* 
field to figure out what happened. *


errno* is a cons: *( integer . symbol )*  of the error code and a symbol 
identifying the platform for which the error has meaning.  The codes are 
(somewhat) platform dependent so you will need other references to 
decode them.


For more, see:

 * 
https://docs.racket-lang.org/reference/exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn%29%29
 * 
https://docs.racket-lang.org/reference/exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3anetwork~3aerrno%29%29


Hope this helps,
George

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/31a25463-eb1e-2f05-920a-7cfa49e33f99%40comcast.net.


[racket-users] Re: Detecting broken inbound TCP connections

2022-07-01 Thread Jeff Henrikson

Hi Tony,

Thanks for offering your interpretation of the racket reference manual.  
Below is my attempt to test whether or not the input port does indeed 
become closed when the TCP connection is broken.


The broken TCP connection is provided by combining curl with GNU 
coreutils timeout.


I try testing the input port for closedness two different ways. The 
default code tests with port-closed-evt. The commented out code tests by 
calling port-closed? in a loop.  Neither method succeeds in detecting 
the broken TCP connection.


Thanks for any help.

Regards,


Jeff Henrikson


#lang racket

;; Tony Garnock-Jones, 
https://groups.google.com/g/racket-users/c/H43jr8QuM-4
;;   "... if the connection breaks, the ports will be closed as usual. . 
. ."


(define (handle-by-event in out k)
  ; Discard the HTTP request header (up to blank line):
  (regexp-match #rx"(\r\n|^)\r\n" in)
  (displayln "about to sleep")
  (flush-output (current-output-port))
  (let ((evt (sync/timeout 10 (port-closed-evt in
    (if evt
  (begin
    (displayln "got close evt\n")
    (k))
  (begin
    (displayln "done sleeping")
    (flush-output (current-output-port))
    (display "HTTP/1.0 200 Okay\r\n" out)
    (display "Server: k\r\nContent-Type: text/html\r\n\r\n" out)
    (display "Hello, world!" out)
    (k)

;; same as handle-by-event but using port-closed? instead of port-closed-evt
(define (handle-by-polling in out k)
  (define (iter i)
    (when (> i 0)
    (printf "closed=~a\n" (port-closed? in))
    (sleep 1)
    (iter (- i 1
  ; Discard the request header (up to blank line):
  (regexp-match #rx"(\r\n|^)\r\n" in)
  (displayln "about to sleep")
  (flush-output (current-output-port))
  (iter 10)
  (displayln "done sleeping")
  (flush-output (current-output-port))
  (display "HTTP/1.0 200 Okay\r\n" out)
  (display "Server: k\r\nContent-Type: text/html\r\n\r\n" out)
  (display "Hello, world!" out)
  (k))


(define (accept-and-handle listener)
  (define-values (in out) (tcp-accept listener))
  ;; alternatively: (handle-by-polling in out (lambda ()
  (handle-by-event in out (lambda ()
    (displayln "handle finished")
  ))
  (close-input-port in)
  (close-output-port out))

(define (serve port-no)
  (define listener (tcp-listen port-no 5 #t))
  (define (loop)
    (accept-and-handle listener)
    (loop))
  (loop))

(serve 8000)

#|
    Test with curl and GNU coreutils timeout:
    timeout --foreground 3s curl http://127.0.0.1:8000
    actual output (handle-by-event version):
    about to sleep
    done sleeping
    handle finished
    expected output  (handle-by-event version):
    about to sleep
    got close evt
    handle finished

    Tested on:
    Racket v8.0 cs
    on Ubuntu 20.04.4 LTS
|#



Hi Jeff,

You can use `tcp-listen` and `tcp-accept` [0] to accept connections. 
Once accepted, the connection appears as a matched pair of ports, one 
for input and one for output, and if the connection breaks, the ports 
will be closed as usual. In some circumstances, you will get an 
exception such as "connection reset" (see `exn:fail:network`). If you 
need to reliably distinguish closed-by-peer, all-ok from 
closed-by-weird-network-weather, your application protocol has to 
handle that, there's nothing TCP can do for you there. HTTP does this 
via content-length and/or chunking, for example.


Cheers,
  Tony

[0]: https://docs.racket-lang.org/reference/tcp.html

On 6/30/22 11:34 AM, Jeff Henrikson wrote:

Racket users,

How do I accept an inbound TCP connection so that once I am 
processing it, if the connection is broken by the client or network, 
my program promptly finds out?


Regards,


Jeff Henrikson



--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/e3445bfa-8d75-bbf5-1900-226a67e71599%40gmail.com.


Re: [racket-users] commenting / uncommenting in DrRacket IDE

2022-07-01 Thread Laurent
On Fri, Jul 1, 2022 at 9:25 AM Hrvoje Blazevic  wrote:

> Thanks for replying. However, I was not asking for instructions on how to
> use DrRacket. Have been using it since the days when HTDP1 was first
> printed, and when it was called DrScheme.
> But to answer your question in Racket 8.5 and Fedora 36 with Gome WM
> uncommenting does not work, not with ';' and not with box.
>
> Funny enough, I just installed full Racket 8.5 on my daughter's raspberry
> pi 4B "toy" computer, running Raspberry Pi OS (Debian), and there both
> commenting and uncommenting works!
>

It works for me on Ubuntu/Debian with Gnome or Cinnamon with Racket 8.5 on
several machines. If you run DrRacket from the command line, do you see an
exception being raised when you click on Uncomment?

Maybe also try a fresh install of Racket with a new user, in case your
racket config (either in .racket or in .config/racket) is flawed somehow.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABNTSaEEtDMfB6yaFKnZ%2BoX4kiwogSYG%3Di7Qz1EZhwHXKkJe%3Dw%40mail.gmail.com.


[racket-users] Re: Detecting broken inbound TCP connections

2022-07-01 Thread Tony Garnock-Jones
Hi Jeff,

On Thursday, June 30, 2022 at 8:34:44 PM UTC+2 Jeff Henrikson wrote:

> How do I accept an inbound TCP connection so that once I am processing 
> it, if the connection is broken by the client or network, my program 
> promptly finds out? 
>

You can use `tcp-listen` and `tcp-accept` [0] to accept connections. Once 
accepted, the connection appears as a matched pair of ports, one for input 
and one for output, and if the connection breaks, the ports will be closed 
as usual. In some circumstances, you will get an exception such as 
"connection reset" (see `exn:fail:network`). If you need to reliably 
distinguish closed-by-peer, all-ok from closed-by-weird-network-weather, 
your application protocol has to handle that, there's nothing TCP can do 
for you there. HTTP does this via content-length and/or chunking, for 
example.

Cheers,
  Tony

[0]: https://docs.racket-lang.org/reference/tcp.html

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/d0a1ace1-e14f-4062-8d83-f160165ef9a5n%40googlegroups.com.


Re: [racket-users] commenting / uncommenting in DrRacket IDE

2022-07-01 Thread Hrvoje Blazevic
Thanks for replying. However, I was not asking for instructions on how to
use DrRacket. Have been using it since the days when HTDP1 was first
printed, and when it was called DrScheme.
But to answer your question in Racket 8.5 and Fedora 36 with Gome WM
uncommenting does not work, not with ';' and not with box.

Funny enough, I just installed full Racket 8.5 on my daughter's raspberry
pi 4B "toy" computer, running Raspberry Pi OS (Debian), and there both
commenting and uncommenting works!

Hrvoje Blazevic



On Fri, Jul 1, 2022 at 4:02 PM Laurent  wrote:

> After commenting out with a box, place the cursor inside the box then
> click on the "Racket" menu then on the "Uncomment" item. Now the box should
> be uncommented without losing anything. Does this not work for you?
>
> HTH,
> Laurent
>
> On Fri, Jul 1, 2022 at 5:39 AM Hrvoje Blazevic 
> wrote:
>
>> Hi, I have reported this problem a few years ago (this on Fedora Linux
>> using Gnome), and looks like something was done, but not all.
>> When I first reported, DrRacket was not commenting at all. Not with ;;,
>> nor with a box. Now it does comment with both, but it does not uncomment.
>> Ok, I can uncomment the region manually if I commented with ;; but if I
>> used a box, I can only delete the whole thing. BTW I'm using Racket 8.5 on
>> Fedora 36.
>> Anything being done with this?
>>
>> Hrvoje Blazevic
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/racket-users/b9a257c3-08b1-4f10-ada7-fb1f50156554n%40googlegroups.com
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CA%2BuSn8LvrNWFiXbq1Y%2BhfGMxZ8xb9oJkDNBFQvTj8x-LKXFmew%40mail.gmail.com.


Re: [racket-users] commenting / uncommenting in DrRacket IDE

2022-07-01 Thread Laurent
After commenting out with a box, place the cursor inside the box then click
on the "Racket" menu then on the "Uncomment" item. Now the box should be
uncommented without losing anything. Does this not work for you?

HTH,
Laurent

On Fri, Jul 1, 2022 at 5:39 AM Hrvoje Blazevic  wrote:

> Hi, I have reported this problem a few years ago (this on Fedora Linux
> using Gnome), and looks like something was done, but not all.
> When I first reported, DrRacket was not commenting at all. Not with ;;,
> nor with a box. Now it does comment with both, but it does not uncomment.
> Ok, I can uncomment the region manually if I commented with ;; but if I
> used a box, I can only delete the whole thing. BTW I'm using Racket 8.5 on
> Fedora 36.
> Anything being done with this?
>
> Hrvoje Blazevic
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/b9a257c3-08b1-4f10-ada7-fb1f50156554n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABNTSaGK3MT8PwQt%3D_ve7%3D1ekgw-eYt57fZRsfsEyC-JgOeq%2Bg%40mail.gmail.com.