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 11:07] eechrisat-m42:lecture10 [2011/01/14 12:45] (current) – external edit 127.0.0.1
Line 102: Line 102:
 ---- ----
  
-<code groovy l| Example 3: Very simple client that just writes to server and echoes what's returned (at-m42/Examples/lecture10/javaClient.groovy)>+<code groovy 1| Example 3: Very simple client that just writes to server and echoes what's returned (at-m42/Examples/lecture10/javaClient.groovy)>
 extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/javaClient.groovy extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/javaClient.groovy
 </code> </code>
Line 137: Line 137:
 ===== Groovy-server ===== ===== Groovy-server =====
  
-<code groovy l|Example 3: A simpler Groovy server (at-m42/Examples/lecture10/groovyServer.groovy)>+<code groovy 1|Example 3: A simpler Groovy server (at-m42/Examples/lecture10/groovyServer.groovy)>
 extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/groovyServer.groovy extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/groovyServer.groovy
 </code> </code>
Line 167: Line 167:
 When a new client request comes into the server, the ''accept'' closure is activated in a new thread and the server can immediately go back to waiting.  When a new client request comes into the server, the ''accept'' closure is activated in a new thread and the server can immediately go back to waiting. 
  
-===== ServeOneJabber =====+===== Creating Multiple Clients via Threads =====
  
-An inner classextends Thread (see Concurrencyand implements the behaviour of JabberServer in its run method+<code groovy|Example 6Client that tests the multiClientServer by starting up multiple clients (at-m42/Examples/lecture10/groovyMultiClient.groovy)
-Constructor: +// ..
-Takes a socket s as a parameter (Line 26+def address = InetAddress.getByName("127.0.0.1"
-Adds the necessary upstream and downstream IO decorators (Line 28+while (true{ 
-Calls start() to start the thread [start() calls run()] (Line 36)+ if (threadCount < MAX_THREADS
-Run method: + Thread.start() 
-Implements the echo behaviour (Line 41) + // ... 
-Finally: closes socket s (Line 52+ def socket = new Socket(address, ClientServer.PORT) 
-===== MultiJabberServer =====+ socket.withStreams { input, output ->  
 + // client-server code 
 +
 + // ... 
 +
 +
 + Thread.currentThread().sleep(100
 +
 +</code>
  
-Typical of a real server process! +Full listing in the notes
-Line 65: Creates a ServerSocket s+----
-Inside a while-forever loop +
-s.accept() method blocks until a client attempts to connect (line 70) +
-when client connects, s.accept() returns a new Socket socket. +
-Server sends socket to a new ServeOneJabber (creates a new thread to handle this connection – line 72) +
-Server goes back to waiting for next client connection. +
-Finally (will only be called if server is interrupted) closes the ServerSocket – line 80.+
  
-===== Client that tests the MultiJabberServer =====+<code groovy 1|Example 6: Client that tests the multiClientServer by starting up multiple clients (at-m42/Examples/lecture10/groovyMultiClient.groovy)> 
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/groovyMultiClient.groovy 
 +</code>
  
-===== MultiJabberClient =====+As before, we use the power of closures this time to continually create client threads, 
 +connect to the server, and echo some numbers before sending the end message and shutting down.
  
-Creates lots of client threads (JabberClientThread objects) to test the ability of the Server to handle multiple requests+===== The Second Run ===== 
-Constructor (Line 34) takes an internet address+{{:at-m42:server-run2.png|The second server run}} 
-Opens a socket to the server (line 38) +---- 
-Adds IO Decoration to upstream and downstream links (line 45) + 
-Class the start() method (line 50) +This slide shows a run of these programs.  
-Various finally clauses recover from errors (line 52)+ 
-Run method (line 65) +As before the server started first: 
-Sends numbers to server +<cli prompt='>'> 
-Closes socket when done +$ groovy groovyMultiServer.groovy 
-===== MultiJabberClient =====+</cli> 
 +client started in a second command shell window
 +<cli prompt='>'> 
 +$ groovy groovyMultiClient.groovy 
 +</cli> 
 + 
 +The client and server will continue to run until you kill the processes (with windows <key>C-c</key>).
  
-Creates a JabberClientThread objects every 100 ms. 
-Each JabberClientThread opens a connection to the server and “jabbers” 
-Up to 40 connections will be made 
  
 ===== User Datagram Protocol (UDP) ===== ===== User Datagram Protocol (UDP) =====
  
-Previous examples used Transmission Control Protocol (TCP). +  * Previous examples used //Transmission Control Protocol// (TCP). 
-Very high reliability, message will always get there. +  Very high reliability, message will always get there. 
-Also high overhead +  Also high overhead. 
-User Datagram Protocol (UDP) is an unreliable” protocol which is much faster, but the messages won’t always get there +  * //User Datagram Protocol// (UDP) is an "unreliableprotocol which is much faster, but the messages won’t always get there. 
-Datagrams + 
-You make your own packets and write the address on the outside, send it +===== Datagrams ===== 
-Based on packet contents, recipient decides whether they got everything, sends a message if they didn’t, you retransmit + 
-Reliability must be enforced by your program, not by the UDP protocol+  You make your own packets and write the address on the outside, send it. 
 +  Based on packet contents, recipient decides whether they got everything, sends a message if they didn’t, you retransmit. 
 +  Reliability must be enforced by your program, not by the UDP protocol
 +  
 ===== Communicating with Datagrams ===== ===== Communicating with Datagrams =====
  
-DatagramSocket on both server and client +  * ''DatagramSocket'' on both server and client. 
-Sends and receives DatagramPackets +  Sends and receives ''DatagramPacket''s. 
-No connection: datagram just shows up +  No "connection": datagram just shows up. 
-Only server must have fixed IP & port number +  Only server must have fixed IP & port number 
-DatagramPacket contains message of any length up to 64K bytes, and IP address and socket # of destination +  * ''DatagramPacket'' contains message of any length up to 64K bytes, IP address and socket # of destination. 
-DatagramPacket objects are used for holders of both sent and received datagrams+  * ''DatagramPacket'' objects are used for holders of both sent and received datagrams 
 ===== For Sending and Receiving ===== ===== For Sending and Receiving =====
  
-For receiving, needs a buffer: +  * For receiving, needs a buffer: 
- DatagramPacket(buf, length) +<code groovy> 
-For sending, needs buffer, IP address and port number: +DatagramPacket(buf, length) 
- DatagramPacket(buf, length, inetAddress, port) +</code> 
-Here, buf already contains data to be sent, length is amount of buffer you want in datagram +  * For sending, needs buffer, IP address and port number: 
-In the example, the server just echoes datagrams, client threads send datagrams & wait for echo: +<code groovy> 
-===== Examples (for Self Study) =====+DatagramPacket(buf, length, inetAddress, port) 
 +</code> 
 +  * Here, ''buf'' already contains data to be sent, ''length'' is amount of buffer you want in datagram. 
 +  In the example, the server just echoes datagrams, client threads send datagrams & wait for echo
 +   
 +===== Examples (online for Self Study) =====
  
-Dgram.java – defines a UPD datagram +  * [[http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/Dgram.groovy|Dgram.groovy]] defines a UPD datagram 
-ChatterServer.java – a version of MultiJabberServer using Dgram objects. +  * [[http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/chatterServer.groovy|chatterServer.groovy]] a version of groovyMultiServer using ''Dgram'' objects. 
-ChatterClient.java – a version of MultiJabberClient using Dgram objects.+  * [[http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/chatterClient.groovy|chatterClient.groovy]] a version of groovyMultiClient using ''Dgram'' objects.
  
-===== Firewall Warning =====+===== Even Simpler Groovy Servers =====
  
-Problem: program using port 8080 works fine, except when client is behind firewall +  * The ''-l'' option lets you run ''Groovy'' script in client-server mode.  
-Firewall doesn’t like to see anything but ordinary network connections to WWW server via its standard HTTP port 80 +  * 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)
-A solution in is to use CGI programming +  * You can then connect to that port using a suitable client (e.g. Telnet)
-Java servlet server solves this problem as it communicates via HTTP port 80+
-===== Lab Exercises =====+
  
-1. Compile and run the JabberServer and JabberClient programs in this chapter. Now edit the files to remove all the buffering for the input and output, then compile and run them again to observe the results. +===== whoAmI again =====
-2. Create a server that asks for a password, then opens a file and sends the file over the network connection. Create a client that connects to this server, gives the appropriate password, then captures and saves the file. Test the pair of programs on your machine using the localserver (the local loopback IP address 127.0.0.1 produced by calling InetAddress.getByName(null)). +
-3. Modify the server in exercise 2 so that it uses multithreading to handle multiple clients. +
-4. Modify JabberClient so that it doesn’t explicitly flush( ) its output and observe the effect.+
  
 +<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>
  
-<code groovy 1| Example 1Everything is an object (at-m42/Examples/lecture02/example1.groovy)> +===== Ridiculously simple echo server ===== 
-extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture02/example1.groovy+ 
 +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 7a really simple echo server (at-m42/Examples/lecture10/simpleServer.groovy)> 
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/simpleServer.groovy
 </code> </code>
----- 
  
-Notes ...+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.
  
-===== Heading 2 =====+===== A 75 Line Web Server ======
  
-  * [[#Sub head 1]] +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!
-  * [[#Sub head 2]]+
  
 +[[http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/examples/commandLineTools/SimpleWebServer.groovy|Listing]] is in the notes.
  
-===== Summary of this Lecture ==== +It really works: 
-  +<cli prompt='>'> 
-The ....+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>
  
-  * [[#Heading 1]] +===== Firewall Warning =====
-  * [[#Heading 2]] +
-  * [[#heading 3]] +
- +
-===== Lab Exercises =====+
  
-  * [[eg-m42:labs:lab1|Lab 1]] exercises 1-7.+You may find that the examples scripts will not run in the PC lab because either Groovy or Java be prevented from openning a port by Windows Firewall. You won't have the necessary privileges to allow. Should work on your own machine.
  
 ---- ----
  
-[[Home]] | [[lecture1|Previous Lecture]] | [[Lectures]] | [[lecture3|Next Lecture]] +[[Home]] | [[lecture9|Previous Lecture]] | [[Lectures]] | [[lecture11|Next Lecture]] 
  
at-m42/lecture10.1239880075.txt.gz · Last modified: 2011/01/14 12:21 (external edit)