at-m42:lecture10
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at-m42:lecture10 [2009/04/16 11:07] – eechris | at-m42:lecture10 [2011/01/14 12:45] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 102: | Line 102: | ||
---- | ---- | ||
- | <code groovy | + | <code groovy |
extern> http:// | extern> http:// | ||
</ | </ | ||
Line 137: | Line 137: | ||
===== Groovy-server ===== | ===== Groovy-server ===== | ||
- | <code groovy | + | <code groovy |
extern> http:// | extern> http:// | ||
</ | </ | ||
Line 167: | Line 167: | ||
When a new client request comes into the server, the '' | When a new client request comes into the server, the '' | ||
- | ===== ServeOneJabber | + | ===== Creating Multiple Clients via Threads |
- | An inner class: extends Thread | + | <code groovy|Example 6: Client that tests the multiClientServer by starting up multiple clients |
- | Constructor: | + | // ... |
- | Takes a socket s as a parameter | + | def address = InetAddress.getByName(" |
- | Adds the necessary upstream and downstream IO decorators | + | while (true) { |
- | Calls start() to start the thread [start() | + | if (threadCount < MAX_THREADS) { |
- | Run method: | + | Thread.start() |
- | Implements the echo behaviour | + | // ... |
- | 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) | ||
+ | } | ||
+ | </ | ||
- | 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/ |
+ | extern> http:// | ||
+ | </ | ||
- | ===== 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. | + | {{: |
- | Opens a socket to the server | + | ---- |
- | 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 |
- | Sends numbers to server | + | <cli prompt='>'> |
- | Closes socket when done | + | $ groovy groovyMultiServer.groovy |
- | ===== MultiJabberClient ===== | + | </ |
+ | client started in a second command shell window. | ||
+ | <cli prompt='>'> | ||
+ | $ groovy groovyMultiClient.groovy | ||
+ | </ | ||
+ | |||
+ | The client and server will continue to run until you kill the processes (with windows < | ||
- | 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, | + | |
- | 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 "unreliable" |
- | 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 | + | |
+ | | ||
+ | | ||
+ | | ||
===== Communicating with Datagrams ===== | ===== Communicating with Datagrams ===== | ||
- | DatagramSocket on both server and client | + | * '' |
- | Sends and receives | + | |
- | No “connection”: datagram just shows up | + | |
- | Only server must have fixed IP & port number | + | |
- | DatagramPacket contains message of any length up to 64K bytes, | + | * '' |
- | 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, | + | <code groovy> |
- | For sending, needs buffer, IP address and port number: | + | DatagramPacket(buf, |
- | DatagramPacket(buf, | + | </ |
- | 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, |
+ | </ | ||
+ | * Here, '' | ||
+ | | ||
+ | | ||
+ | ===== Examples (online | ||
- | Dgram.java – defines a UPD datagram | + | * [[http:// |
- | ChatterServer.java – a version of MultiJabberServer | + | * [[http:// |
- | ChatterClient.java – a version of MultiJabberClient | + | * [[http:// |
- | ===== Firewall Warning | + | ===== Even Simpler Groovy Servers |
- | Problem: program using port 8080 works fine, except when client is behind | + | * The '' |
- | Firewall doesn’t like to see anything but ordinary network connections to WWW server | + | * You execute a script (using '' |
- | 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 |
- | 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: | ||
+ | </ | ||
+ | Now in another command window: | ||
+ | <cli prompt=">"> | ||
+ | e: | ||
+ | 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 | ||
+ | </ | ||
- | <code groovy | + | ===== Ridiculously simple echo server ===== |
- | extern> http:// | + | |
+ | When a script is started in listening mode, standard-output is attached to the socket' | ||
+ | <code groovy|Example | ||
+ | extern> http:// | ||
</ | </ | ||
- | ---- | ||
- | Notes ... | + | Run this as |
+ | <cli prompt='>'> | ||
+ | e: | ||
+ | </ | ||
+ | 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:// | ||
- | ===== Summary of this Lecture ==== | + | It really works: |
- | + | <cli prompt='>'> | |
- | The .... | + | e: |
+ | </ | ||
+ | ---- | ||
+ | <code groovy 1|Example 8: a web server in 74 lines of code> | ||
+ | extern> http:// | ||
+ | </ | ||
- | * [[#Heading 1]] | + | ===== Firewall Warning |
- | * [[#Heading 2]] | + | |
- | * [[#heading 3]] | + | |
- | + | ||
- | ===== Lab Exercises | + | |
- | * [[eg-m42: | + | 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)