[plt-scheme] w00t!

From: Pedro Pinto (ppinto at cs.cmu.edu)
Date: Sat Feb 26 20:57:10 EST 2005

For reasons that are unimportant right now, I found myself in need of a 
tcp re-director, i.e. a program that can re-direct traffic from a local 
port to a foreign host/port. Not knowing  enough  Unix to conjure up the 
appropriate command line, and more importantly, certain that playing 
with Scheme would be far more enjoyable than deciphering badly written 
man pages, I decided to fire up MzScheme. To my delight it took me less 
than 10 minutes (I am a bit slow) to write and test the script below. 
The relevant core of script implements a robust, multi-threaded 
redirecting function. In 7 lines of code.

Try that in Java, C# or any other billion dollar framework.

Thanks guys, for a glimpse into what programming would look like if we 
had proper tools.

-pp



#!/usr/bin/mzscheme -r

; syntax: redirect <source-port> <target-port> <target-host>
;      - Redirects tcp traffic from <source-por> into <target-port> 
<target-host>

(require (lib "cmdline.ss") (lib "thread.ss"))

(define (redirect source-port target-port target-host)
  (run-server source-port
              (lambda (s-in s-out)              
                (let-values (((t-in t-out) (tcp-connect target-host 
target-port)))
                            (thread (lambda() (copy-port s-in t-out)))
                            (copy-port t-in s-out)))
               #f))

(command-line "redirect" (current-command-line-arguments)
              (help-labels "Redirects connections to <source-port> to 
<target-port> <target-host>")
              (args (source-port target-port target-host)
                    (redirect (string->number source-port)
                              (string->number target-port)
                              target-host)))







Posted on the users mailing list.