User Tools

Site Tools


at-m42:lecture10

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
at-m42:lecture10 [2009/04/16 14:21] eechrisat-m42:lecture10 [2011/01/14 12:45] (current) – external edit 127.0.0.1
Line 257: Line 257:
  
 ===== Even Simpler Groovy Servers ===== ===== Even Simpler Groovy Servers =====
 +
 +  * The ''-l'' option lets you run a ''Groovy'' script in client-server mode. 
 +  * You execute a script (using ''-e'' or by specifying a file) and groovy starts a TCP server on port 1960((Or another port if you wish to override it).
 +  * You can then connect to that port using a suitable client (e.g. Telnet)
 +
 +===== whoAmI again =====
 +
 +<cli prompt=">">
 +e:\dev\at-m42-2009\Examples\lecture10> groovy -l 5000 -e "println 'ip address: ' + InetAddress.getByName(line).hostAddress"
 +</cli>
 +Now in another command window:
 +<cli prompt=">">
 +e:\dev\at-m42-2009\Examples\lecture10> telnet localhost 5000
 +localhost
 +ip address: 127.0.0.1
 +java.sun.com
 +ip address: 72.5.124.55
 +www.swan.ac.uk
 +ip address: 137.44.1.7
 +</cli>
 +
 +===== Ridiculously simple echo server =====
 +
 +When a script is started in listening mode, standard-output is attached to the socket's output stream and the messages from the socket's input stream are available line-by-line to the programmer in variable ''line''. Thus our echo server can be reimplemented as:
 +<code groovy|Example 7: a really simple echo server (at-m42/Examples/lecture10/simpleServer.groovy)>
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/simpleServer.groovy
 +</code>
 +
 +Run this as 
 +<cli prompt='>'>
 +e:\dev\at-m42-2009\Examples\lecture10> groovy -l 12345 simpleServer.groovy
 +</cli>
 +Then run any of the TCP clients developed earlier.
  
 ===== A 75 Line Web Server ====== ===== A 75 Line Web Server ======
 +
 +To demonstrate the power of Groovy, Jeremy Rayner, one of the core Groovy developers, wrote a simple HTTP server in less than 75 lines of code!
 +
 +[[http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/examples/commandLineTools/SimpleWebServer.groovy|Listing]] is in the notes.
 +
 +It really works:
 +<cli prompt='>'>
 +e:\dev\at-m42-2009\Examples\lecture10>groovy -l 80 SimpleWebServer.groovy
 +</cli>
 +
 +----
 +<code groovy 1|Example 8: a web server in 74 lines of code>
 +extern> http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/examples/commandLineTools/SimpleWebServer.groovy
 +</code>
  
 ===== Firewall Warning ===== ===== Firewall Warning =====
at-m42/lecture10.1239891709.txt.gz · Last modified: 2011/01/14 12:21 (external edit)