Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
COMP1681 / SE15
Introduction
to Programming
Fast Track Session 2
Java Network Programming
Today’s Learning Objectives
For you to learn the various ways in which Java supports
the development of networked applications
For you to see simple examples of client and server
programs written in Java
SE15 Fast-Track: Java Network Programming
FT1–2
Lecture Outline
Overview
Accessing resources by URL
Low-level protocol support
Address handling
Sockets
Writing servers
Non-standard libraries
SE15 Fast-Track: Java Network Programming
FT1–3
Java & Networking
Java was marketed as a programming language for
‘network-centric computing’…
…so it provides good support in standard library for
writing networked applications
HTTP
TCP & UDP
Relevant packages are
java.net
java.io, java.nio
javax.net, javax.net.ssl
SE15 Fast-Track: Java Network Programming
FT1–4
Treating URLs Like Filenames
Create a java.net.URL object, given the URL of a
resource expressed as a string
Call its openStream method to get an InputStream
from which we can read data…
…or, for more control, call openConnection to get a
URLConnection object
getContentLength gives document size
getContentType gives doc type (HTML, GIF image…)
getInputStream gives stream that we can read
SE15 Fast-Track: Java Network Programming
FT1–5
Example
URL url = new URL(urlString);
URLConnection conn = new URLConnection(url);
if (conn.getContentType().equals("text/html")) {
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = in.readLine();
while (line != null) {
System.out.println(line);
line = in.readLine();
}
in.close();
}
SE15 Fast-Track: Java Network Programming
FT1–6
IP, TCP & UDP
Data travel over the network as IP datagrams
Datagram payload conforms to TCP, UDP or ICMP
Transmission Control Protocol (TCP)
Provides a reliable connection between endpoints
Endpoints defined by port numbers
Uses sequence numbers to order datagrams
Retransmits datagrams if necessary
User Datagram Protocol (UDP)
Simpler than TCP
No promise of reliable delivery
SE15 Fast-Track: Java Network Programming
FT1–7
Hostnames & IP Addresses
Networked machines are identified by IP address
IPv4 address is a 32-bit integer, typically expressed in
‘dotted quad’ notation
Human-readable hostname is resolved to an IP address
using the domain name system (DNS)
Example: 129.11.144.9
Example: cslin-gps.leeds.ac.uk → 129.11.144.9
Java encapsulates hostnames and IP addresses in the
InetAddress class
SE15 Fast-Track: Java Network Programming
FT1–8
InetAddress Class
Provides static factory methods for obtaining
InetAddress objects
getByName
getByAddress
getAllByName (all available addresses for host)
getLocalHost (address of local machine)
InetAddress objects support
getHostName (→ hostname as string)
getAddress (→ IP address as array of bytes)
getHostAddress (→ IP address as string)
SE15 Fast-Track: Java Network Programming
FT1–9
Sockets
Provide an abstraction of a TCP connection
Much easier to use in Java than in C
Much less set-up code
No platform variation (e.g. Unix vs. Windows)
Object-oriented abstraction
Socket must be bound to a local endpoint and
connected to a remote endpoint
Done automatically by Socket constructor
Usually we specify remote address and port and let the
system choose an ephemeral port on local machine
SE15 Fast-Track: Java Network Programming
FT1–10
Example of Socket Code
Socket socket = new Socket(address, port);
BufferedReader input = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = input.readLine();
while (line != null) {
System.out.println(line);
line = input.readLine();
}
input.close();
socket.close();
SE15 Fast-Track: Java Network Programming
FT1–11
An HTTP Client Using Sockets
Socket socket = new Socket(address, port);
BufferedReader input = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()));
output.print("GET " + document + " HTTP/1.0\n\n");
output.flush();
String line = input.readLine();
while (line != null) {
System.out.println(line);
line = input.readLine();
}
...
SE15 Fast-Track: Java Network Programming
FT1–12
Writing Servers
Socket alone isn’t sufficient to write a server
We need something to ‘sit by the phone, waiting for
incoming calls’…
Java provides ServerSocket class to
Listen on a particular port
Negotiate connection with client
Open a Socket connection between hosts
SE15 Fast-Track: Java Network Programming
FT1–13
Using ServerSocket
1. Create ServerSocket object
2. Call accept to listen for incoming connection attempts
Blocks until client attempts to connect
Returns Socket object when connection succeeds
3. Call getInputStream and/or getOutputStream on
Socket to get streams that communicate with client
4. Interact with client according to agreed protocol
5. Server, client or both close connection
6. Return to Step 2
SE15 Fast-Track: Java Network Programming
FT1–14
Beyond The Standard Library
JavaMail API
Jakarta Commons, jakarta.apache.org/commons/
Commons Net
Commons HttpClient
Support for FTP, NNTP, SMTP, POP3, Telnet…
Authentication, HTTPS, cookie handling…
Commons Email
Simplified interface to JavaMail
SE15 Fast-Track: Java Network Programming
FT1–15
Summary
We have
Seen how easy it is to read data from the web, given the
URL of a resource
Examined how sockets can be used in Java to created
network clients of various kinds
Considered how a simple server can be written in Java
Looked at some of the advanced applications that are
possible using non-standard libraries
SE15 Fast-Track: Java Network Programming
FT1–16
Exercise
Implement a very simple game (e.g., number guessing,
Hangman or Pontoon) as a networked application
Devise a protocol for server and client to follow
Write a class to act as a server + one to act as a client
Get help at http://java.sun.com/docs/books/tutorial/networking/
SE15 Fast-Track: Java Network Programming
FT1–17