Table of Contents

~~SLIDESHOW~~

Network Programming

Identifying a Machine

static InetAddress.getByName()

Who Am I?

Finds out your network address when you're connected to the Internet.

1 | Example 1: Examine the java.net.InetAddress object (at-m42/Examples/lecture10/whoAmI.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/whoAmI.groovy

<cli prompt='$'> $ java WhoAmI Usage: WhoAmI MachineName $ java WhoAmI www.swan.ac.uk www.swan.ac.uk/137.44.1.7 </cli> May be useful if your ISP dynamically allocates IP addresses for your dial-up connection.

Clients and Servers

Testing without a Network

InetAddress addr = InetAddress.getByName(null)
InetAddress.getByName("localhost")
InetAddress.getByName("127.0.0.1")

Port

Sockets

Just Like Files

Simple Client and Server

The Java Server


1| Example 2: very simple server that just echoes whatever the client sends (at-m42/Examples/lecture10/javaServer.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/javaServer.groovy

Explanation: references to listing …

  1. Line 7 – create server socket at ClientServer.PORT and wait for a connection
  2. Lines 10 and 14 – attach I/O to connection and act as a server
  3. Lines 16-22 – act as an echo server
  4. Lines 24 and 28 – finally: always close the two sockets

The Client

The Java Client


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

Explanation: references to code …

  1. Line 3 – open loop-back address
  2. Line 6 – create socket and connect to ClientServer.PORT
  3. Lines 11 and 15 – attach I/O to connection and act as a client
  4. Lines 17–21 – write to server and echo reply
  5. Line 23 – finally: close client socket

The First Run

The first server run


This slide shows a run of these programs.

Server started first: <cli prompt='>'> $ groovy javaServer.groovy </cli> client started in a second command shell window. <cli prompt='>'> $ groovy javaClient.groovy </cli>

Groovy-Socket

Groovy-server

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

Here we see how the use of closures simplify the logic of the server application, and also makes it much more readable. The necessary try-catch-finally code that Java needs can be hidden inside the definition of the server.accept(Closure) and socket.withStreams(Closure) methods. Similar usability improvements are to be found throughout the Groovy-I/O system. Most commonly used cases are supported, but if you need tighter control, you can always go back to the full Java patterns.

Groovy-client

1|Example 4: A simpler Groovy client (at-m42/Examples/lecture10/groovyClient.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/groovyClient.groovy

Similarly, the client is made much more understandable by use of the socket.withStreams method. You will also have noted the similarity of the actual code of the client/server logic.

Serving Multiple Clients via Threads

Since the Groovy closure implements the Runnable interface, to create a server that can handle multiple threads, we simply wrap the server.accept closure in a while(true) loop.

1|Example 5: A multi-threaded server (at-m42/Examples/lecture10/groovyMultiServer.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture10/groovyMultiServer.groovy

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.

Creating Multiple Clients via Threads

6: Client that tests the multiClientServer by starting up multiple clients (at-m42/Examples/lecture10/groovyMultiClient.groovy)
// ...
def address = InetAddress.getByName("127.0.0.1")
while (true) {
	if (threadCount < MAX_THREADS) {
		Thread.start() {
			// ...
			def socket = new Socket(address, ClientServer.PORT)
			socket.withStreams { input, output -> 
				// client-server code
			}
			// ...
		}
	}
	Thread.currentThread().sleep(100)
}

Full listing in the notes.


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

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.

The Second Run

The second server run


This slide shows a run of these programs.

As before the server started first: <cli prompt='>'> $ groovy groovyMultiServer.groovy </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>).

User Datagram Protocol (UDP)

Datagrams

Communicating with Datagrams

For Sending and Receiving

DatagramPacket(buf, length)
DatagramPacket(buf, length, inetAddress, port)

Examples (online for Self Study)

Even Simpler Groovy Servers

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:

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

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

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!

Listing is in the notes.

It really works: <cli prompt='>'> e:\dev\at-m42-2009\Examples\lecture10>groovy -l 80 SimpleWebServer.groovy </cli>


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

Firewall Warning

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 | Previous Lecture | Lectures | Next Lecture

1)
Windows firewall may not allow connections!