Download 11 - Networking

Document related concepts
no text concepts found
Transcript
Network Programming in Java
©SoftMoore Consulting
Slide 1
A Network Is...
• node
– any device on the network
•
host
– a computer on the network
•
address
– computer-readable name for host
•
host name
– human-readable name for host
©SoftMoore Consulting
Slide 2
A Network Does...
• Packet (or datagram)
– little bundle of information
– sent from one node to another
– each packet is delivered separately (possibly by different routes)
• Protocol
– roles, vocabulary, rules for communication
– network protocols (e.g., IP) versus application protocol (e.g.,
HTTP)
• IP
– Internet Protocol
– enables different local area networks to communicate
– basis for connecting computers around the world
©SoftMoore Consulting
Slide 3
Two Types of Information
• Application data
– the information one computer wants to send to another
•
Network protocol data
– describes how to reach the intended computer
– describes how to check for errors in the transmission
©SoftMoore Consulting
Slide 4
TCP/UDP/IP
• IP (Internet Protocol)
– raw packets
– the “Internet Layer”
•
UDP (User Datagram Protocol)
– packets
– unreliable, unordered
– the “Transport Layer”
•
TCP (Transmission Control Protocol)
– data stream
– reliable, ordered
(retry on failure; notify sender of success or failure)
– the “Transport Layer”
©SoftMoore Consulting
Slide 5
TCP/IP: The Internet Protocol
Application Layer (HTTP, FTP, SMTP)
Transport Layer (TCP, UDP)
Internet Layer (IP)
Physical Network
©SoftMoore Consulting
Slide 6
TCP vs UDP
• UDP is unreliable
–
–
–
–
–
•
Packets can arrive out of order
Packets can be lost
Not acknowledged by recipient
No provisions for flow control
Packet size limited by IP
TCP addresses these problems
–
–
–
–
Ensures that all packets show up, in correct order
Connection oriented
More like a telephone connection than a letter
Con: uses more resources: memory, speed, bandwidth
©SoftMoore Consulting
Slide 7
The Three “I”s and an “E”
• internet
– any IP-based network
•
Internet
– the big, famous, world-wide IP network
•
intranet
– a corporate LAN-based IP network
•
extranet
– accessing corporate data across the Internet
©SoftMoore Consulting
Slide 8
IP Addresses
• IP Address
– identifies a host
– serves as a destination address for a packet
– IPv4 (most widely deployed)
•
•
4 bytes/32 bits for an IP address
Expressed using “dot” notation: 192.168.0.13
– IPv6 (the next generation)
•
•
16 bytes/128 bits for an IP address
Domain name
– easy-to-remember name for a host
– Example: www.google.com
•
Domain Naming Service (DNS)
– translates from domain names to IP addresses
Slide 9
©SoftMoore Consulting
Socket
• Two-way connection
• Provides inter-process communication using IP
• Derived from BSD (UNIX) sockets
• Read/write streams between hosts
– use either UDP and TCP protocols (layered on top of IP)
– uses IP addresses and port numbers to locate servers
©SoftMoore Consulting
Slide 10
Port
• Meeting place on a host
– one service per port
•
•
•
Port numbers identify services on a server
Range from 1 to 65535 (use type int)
Port numbers 1 through 255 are reserved for well-known
services
– HTTP is 80
– TELNET is 23
– etc.
•
Port numbers less than 1024 are privileged
©SoftMoore Consulting
Slide 11
Sockets and Ports (Diagram)
Client
Socket
port 13
Time Service
port 80
Web Service
Socket
Server
©SoftMoore Consulting
Slide 12
Well-Known Ports
• 13: time
• 20,21: FTP
• 23: telnet
• 25: SMTP
• 43: whois
• 80: HTTP
• 119: NNTP
• 443 HTTPS
• 989/990: SFTP
• 1099: RMI (Java)
For additional information on port numbers see
“List of TCP and UDP port numbers,” Wikipedia,
https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
©SoftMoore Consulting
Slide 13
Time of Day Service
• From the command prompt, enter the following
command:
telnet time-A.timefreq.bldrdoc.gov 13
•
Connects to the time-of-day service on a computer
operated by the National Institute of Standards and
Technology in Boulder, Colorado
•
Gives time based on a Cesium atomic clock
– result not completely accurate due to network delays
By default, the telnet client is not installed on Windows.
To install telnet
Start → Control Panel → Programs and Features
→ Turn Windows features on or off
→ Telnet Client option
©SoftMoore Consulting
Slide 14
HTTP Example
From command prompt, enter the following commands:
–
–
–
–
–
–
–
telnet
set localecho
open www.softmoore.com 80
GET /index.html HTTP/1.1
Host: www.softmoore.com:80
<enter>
<enter>
©SoftMoore Consulting
Slide 15
HTTP
• Protocol for the World Wide Web
• port 80 default
• Client:
GET path HTTP/1.1
Host: URL:port
Header: value
blank line
•
Server:
HTTP/1.1 OK 200
Header: value
blank line
data
©SoftMoore Consulting
Slide 16
Java and Networking
• Built into language
• One of the Java buzzwords
• Network ClassLoader
• Package java.net
• Based on TCP/IP, the Internet Protocol
©SoftMoore Consulting
Slide 17
The Socket Class
• Hides the complexities of establishing a network
connection and sending information across it
•
Provides the same programming interface used for
working with files
•
Primary constructor/methods
Socket(String host, int port)
InputStream getInputStream()
OutputStream getOutputStream()
void close()
©SoftMoore Consulting
Slide 18
Byte Streams
• A byte stream is a sequence of bytes.
• All other stream types are built on byte streams.
• I/O from disk, network, memory, etc. is handled in
exactly the same way.
•
Abstract class InputStream
abstract int read()
// reads next byte (-1 for EOF)
abstract void close()
• Abstract class OutputStream
abstract void write(int b)
abstract void close()
©SoftMoore Consulting
// (ignores 24 high-order bits)
Slide 19
Character Streams
• A character stream is a sequence of characters
• Abstract class Reader
abstract int read()
// reads next character (-1 for EOF)
abstract void close()
• Abstract class Writer
abstract void write(int c)
abstract write(String str)
// ignores 16 high-order bits
// writes a string
Java naming conventions for I/O classes:
…InputStream/…OutputStream – for reading/writing bytes
…Reader/…Writer – for reading/writing characters
©SoftMoore Consulting
Slide 20
Class PrintStream
• Method print() outputs an object, primitive, or string.
• Method println() does the same thing, but it adds a
newline at the end.
•
•
Object System.out is a PrintStream.
Example
OutputStream os = ...;
PrintStream ps = new PrintStream(os);
©SoftMoore Consulting
Slide 21
Class BufferedReader
• Provides method
String readLine()
•
Examples
InputStreamReader isReader
= new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isReader)
©SoftMoore Consulting
Slide 22
Example: Using Sockets
import java.io.*;
import java.net.*;
...
Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
InputStream is = s.getInputStream();
InputStreamReader isReader = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isReader);
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
in.close();
...
©SoftMoore Consulting
Slide 23
Example: Simple Browser Using Sockets
import java.net.*;
import java.io.*;
public class SimpleHttpBrowser
{
public static void main(String[] args) throws Exception
{
host and port
String host = args[0];
int
port = Integer.parseInt(args[1]); are command
line arguments
Socket s = new Socket(host, port);
InputStream inStream = s.getInputStream();
OutputStream outStream = s.getOutputStream();
PrintWriter out = new PrintWriter(
new OutputStreamWriter(outStream), true);
©SoftMoore Consulting
Slide 24
Example: Simple Browser Using Sockets
(continued)
BufferedReader in = new BufferedReader(
new InputStreamReader(inStream));
out.println("GET / HTTP/1.1");
out.println("Host: " + host + ":" + port);
out.println();
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
in.close();
out.close();
s.close();
}
}
©SoftMoore Consulting
Slide 25
Client/Server Overview
• Can communicate with remote file systems using a
client/server model
•
•
Server listens for connection requests from clients
•
Upon connection, server processes the request coming
across the network from the client
Clients know how to connect to the server via a port
number
• Connection can remain open or shut down after each
transaction
©SoftMoore Consulting
Slide 26
Client-Server
•
Client – initiates connection
–
–
–
–
•
•
retrieves data
displays data
responds to user input
requests more data
Examples
– web browser
– chat program
– PC accessing files
Server – responds to
connection
– receives data request
– formulates response
– sends response to client
•
Examples
–
–
–
–
web server
database server
email server
domain name server
Difference between client and server is
relative. It’s just peers talking to each other.
©SoftMoore Consulting
Slide 27
Creating Servers using TCP
• A server listens on a port and accepts connections.
– only one service per port for the entire host
– Java throws an exception if you try to open more than one.
•
Create a ServerSocket attached to port.
– port number of 0 creates a socket on any free port
– optional second parameter sets maximum queue length for
incoming connection requests (default is 50)
ServerSocket server = new ServerSocket(portNum);
• Wait for connections from clients requesting connections
to that port.
Socket s = server.accept();
©SoftMoore Consulting
Slide 28
Creating Servers using TCP
(continued)
• Get input and output streams associated with socket.
outStream = s.getOutputStream();
inStream = s.getInputStream();
•
Use filters if performing text I/O
PrintWriter out = new PrintWriter(
new OutputStreamWriter(outStream));
BufferedReader in = new BufferedReader(
new InputStreamReader(inStream));
©SoftMoore Consulting
Slide 29
Creating Clients using TCP
• Create a Socket object attached to host/port
Socket client = new Socket(host, portNum);
•
•
When the constructor returns, you have a connection
Get input and output streams associated with socket.
outStream = client.getOutputStream();
inStream = client.getInputStream();
•
Use filters if performing text I/O
PrintWriter out = new PrintWriter(
new OutputStreamWriter(outStream));
BufferedReader in = new BufferedReader(
new InputStreamReader(inStream));
©SoftMoore Consulting
Slide 30
What to Do Upon Connection?
• Can read/write to socket, thus, communicating with the
server or client
•
Server talks to client:
out.println("Watson! Come here...");
String data = in.readLine();
•
Client talks to server:
String data = in.readLine();
out.println("I heard you over socket!");
©SoftMoore Consulting
Slide 31
EchoServer.java
ServerSocket ss = new ServerSocket(1234);
Socket s = ss.accept();
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
int ch;
while ((ch = in.read()) != -1)
out.write(ch);
out.close();
in.close();
©SoftMoore Consulting
Slide 32
EchoServer Demo
• Connect
• Type some stuff
• Disconnect
• Problem:
– multiple simultaneous connections
– connections are made, but server doesn’t respond
– even if we loop, can still handle only one at a time
• Solution: Java Threads
©SoftMoore Consulting
Slide 33
Multiple Clients
• Must spawn thread for each connected client
• Pass input/output streams to client handler
• Client handler implements protocol between client and
server
©SoftMoore Consulting
Slide 34
ThreadedEchoServer.java
// in ThreadedEchoServer method main()
int port = 8189;
int count = 0;
try (ServerSocket server = new ServerSocket(port);)
{
System.out.println("EchoServer listening on port " + port);
while(true)
{
Socket socket = server.accept();
++count;
System.out.println("Spawning " + count);
Thread handler =
new ThreadedEchoHandler(socket, count);
handler.start();
}
}
catch (Exception e)
...
©SoftMoore Consulting
Slide 35
ThreadedEchoHandler.java
// in ThreadedEchoHandler method run() (within a try block)
InputStream is = socket.getInputStream();
InputStreamReader isReader = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isReader);
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os, true);
// autoFlush
out.println("Hello! Enter BYE to exit.");
String line;
while ((line = in.readLine()) != null)
{
out.println("Echo (" + count + "): " + line);
if (line.trim().equals("BYE"))
break;
}
socket.close();
...
©SoftMoore Consulting
Slide 36
Server Scaling Issues
• A threaded server works well as long as there aren’t too
many threads.
– Thread objects use a significant amount of memory.
– Allocating and deallocating many thread objects creates a
significant memory management overhead.
•
Problem: How to scale a server even more without
introducing too many threads?
•
Possible solutions:
– thread pools
– package nio (multiple data channels per thread)
©SoftMoore Consulting
Slide 37
Fixed Thread Pools
• A fixed thread pool always has a specified number of
threads running.
•
Tasks are submitted to the pool via an internal queue,
which holds extra tasks whenever there are more active
tasks than threads.
•
A simple way to create a fixed thread pool is to use the
capabilities provided in java.util.concurrent.
Specifically, class Executors has a factory method
named newFixedThreadPool().
©SoftMoore Consulting
Slide 38
Executors
• The java.util.concurrent package defines three
executor interfaces
– Executor – a simple interface that supports launching new tasks
– ExecutorService – a subinterface of Executor, which adds
features that help manage the lifecycle, both of the individual
tasks and of the executor itself.
– ScheduledExecutorService – a subinterface of
ExecutorService, supports future and/or periodic execution of
tasks.
• The Executor interface provides a single method,
execute(), designed to be a drop-in replacement for a
common thread-creation idiom.
©SoftMoore Consulting
Slide 39
Example: Using an Executor
Runnable r … ;
Executor e … ;
e.execute(r);
©SoftMoore Consulting
// replaces (new Thread(r)).start()
Slide 40
ExecutorEchoServer.java
// in EchoServer method main()
ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE);
int port = 8189;
int count = 0;
try (ServerSocket server = new ServerSocket(port);)
{
System.out.println("EchoServer listening on port " + port);
while(true)
{
Socket socket = server.accept();
++count;
System.out.println("Spawning " + count);
Runnable handler = new RunnableEchoHandler(socket, count);
pool.execute(handler);
}
}
catch (Exception e)
...
©SoftMoore Consulting
Slide 41
RunnableEchoHandler.java
… same as ThreadedEchoHandler except that
RunnableEchoHandler must “implement” interface
Runnable rather than “extend” class Thread.
©SoftMoore Consulting
Slide 42
Sending Objects
Use serialization
•
•
ObjectOutputStream to write objects
ObjectInputStream to read objects
©SoftMoore Consulting
Slide 43
Example: Sending Objects
// First, define an object to send:
public class Message implements Serializable
{
private int
senderID;
private String messageText;
public Message(int id, String text)
{
senderID
= id;
messageText = text;
}
public String getText()
{
return messageText;
}
}
©SoftMoore Consulting
Slide 44
Example: Sending Objects
(continued)
// Send the message across the socket:
Message msg = new Message(123456789, "Hello");
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(msg);
// On the other side of the socket, retrieve object:
in = new ObjectInputStream(socket.getInputStream());
Message messageObject = (Message) in.readObject();
String messageText = messageObject.getText();
©SoftMoore Consulting
Slide 45
URL
• Format
– protocol://host[:port][/path/][file][#anchor]
•
Examples
–
–
–
–
–
–
http://www.softmoore.com
http://www.softmoore.com/Bib-5.2-JavaAdvanced.html
http://java.sun.com/sfaq/index.html
http://java.sun.com/sfaq/
ftp://ftp.cs.rpi.edu/pub/stl/
http://64.233.187.99:80/
(same as http://www.google.com)
©SoftMoore Consulting
Slide 46
java.net.URL
• URL(String)
– creates a URL object from the String representation
•
URL(String, String, int, String)
– creates a URL object from the specified protocol, host, port
number, and file
•
URL(URL context, String spec)
– creates a URL by parsing the specification spec within a
specified context.
• InputStream openStream()
– opens a connection to this URL and returns an InputStream for
reading from that connection
©SoftMoore Consulting
Slide 47
java.net.URL
(continued)
• URLConnection openConnection()
– Returns a URLConnection object that represents a connection to
the remote object referred to by the URL.
•
MalformedURLException
– thrown if you try to create a bogus URL
– usually means bad user input, so fail gracefully and informatively
•
accessor methods
– getProtocol()
– getPort()
– getRef()
©SoftMoore Consulting
– getHost()
– getFile()
Slide 48
java.net.URLConnection
• InputStream getInputStream()
– returns an input stream that reads from this open connection
– makes the connection if it has not already been made
•
URLConnection creates the socket and handles the
protocol (e.g. HTTP) for you
•
There are also handlers for other protocols
– ftp, gopher, file, mailto, or you can write your own
– getOutputStream()
•
Handles relative URLs via CodeBase/DocumentBase
©SoftMoore Consulting
Slide 49
java.net.URLConnection
(continued)
Object getContent()
•
•
Makes the connection if it hasn’t already been made
Returns an object that’s appropriate for this file’s MIME
type
• For text, returns an InputStream
• For images, returns an ImageObserver
• Only supports text/plain, text/Generic, image/gif,
and image/jpeg
•
You can add your own ContentHandler if you like
©SoftMoore Consulting
Slide 50
Example: Simple Browser Using URLs
import java.net.*;
import java.io.*;
public class SimpleBrowser
{
public static void main(String[] args) throws Exception
{
URL url = new URL(args[0]);
InputStream is = url.openStream();
InputStreamReader isReader = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isReader);
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
in.close();
}
}
©SoftMoore Consulting
Slide 51
java.net.HttpURLConnection
• Extends URLConnection with additional support for
HTTP connections.
•
Usage: Cast a URLConnection
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection =
(HttpURLConnection) url.openConnection();
•
Selected methods in HttpURLConnection
–
–
–
–
int getResponseCode()
String getResponseMessage()
String getHeaderField(int n)
String getHeaderFieldKey(int n)
Slide 52
©SoftMoore Consulting
java.net.URLEncoder
static String encode(String)
•
The ASCII characters ‘a’ through ‘z’, ‘A’ through ‘Z’, and
‘0’ through ‘9’ remain the same.
•
•
The space character is converted into a plus sign ‘+’.
All other characters are converted into the 3-character
string “%xy”, where xy is the two-digit hexadecimal
representation of the lower 8-bits of the character.
©SoftMoore Consulting
Slide 53
Applets
• An applet is a small application that is delivered to the
browser in the form of Java bytecode.
Applets are usually written in Java, but they can be written in any
programming language that compiles to Java bytecode.
• The applet is launched from a web page and is executed
within a Java Virtual Machine (JVM) in a process
separate from the web browser itself.
•
Applets have been available since the first version of
Java and were widely used in the beginning, but their
usage is now declining.
©SoftMoore Consulting
Slide 54
Networking Application: Checking URLs
URLConnection conn = url.openConnection();
if (conn instanceof HttpURLConnection)
{
HttpURLConnection httpConn = (HttpURLConnection) conn;
// response code 400 or higher is bad
if (httpConn.getResponseCode() >= 400)
printBadUrl(url, httpConn.getResponseCode(),
httpConn.getResponseMessage());
}
else
printFeedback("Skipping " + url + ":
©SoftMoore Consulting
not HTTP protocol");
Slide 55
Getting Stock Quotes from Yahoo
• You can use HTTP to obtain stock quotes from
yahoo.com in a comma-separated values (csv) file
format. The URL has the form
http://finance.yahoo.com/d/quotes.csv?s= <stock symbols
separated by "+“>&f=<special tags>
•
Selected special tags:
–
–
–
–
s : symbol
l : last trade with time
w: 52-week range
r : P/E ratio
Slide 56
–
–
–
–
o : open
c : change and percent change
n : name
x : stock exchange
©SoftMoore Consulting
Example: Getting Stock Quotes from Yahoo
• Example
http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=lcn
•
Format for returned line:
last trade time & price
change/percent
name
"3:21pm - <b>610.97</b>","+10.18 - +1.69%", "Google Inc."
Slide 57
©SoftMoore Consulting
Networking Application: Checking Stocks
public class Stock
{
private static final String QUOTE_FORMAT = "&f=lcwn";
private
private
private
private
private
private
String
String
String
String
String
String
symbol;
lastTradePrice;
lastTradeTime;
change;
range;
name;
public Stock(String symbol)
{
this.symbol = symbol.toUpperCase();
}
...
// continued on next several slides
}
©SoftMoore Consulting
Slide 58
Networking Application: Checking Stocks
(continued)
public void load() throws MalformedURLException, IOException
{
String line = null;
URL url = new
URL("http://finance.yahoo.com/d/quotes.csv?s="
+ symbol + QUOTE_FORMAT);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
line = in.readLine();
in.close();
...
// continued
}
©SoftMoore Consulting
Slide 59
Networking Application: Checking Stocks
(continued)
if (line != null)
{
// parse the line and remove quotes where necessary
String[] values = line.split(",");
change = values[1].substring(1, values[1].length() - 1);
range = values[2].substring(1, values[2].length() - 1);
name
= values[3].substring(1, values[3].length() - 1);
// parse last trade time and price
String lastTrade = values[0];
int start = 1; // skip opening quote
int end = lastTrade.indexOf(" - ");
lastTradeTime = lastTrade.substring(start, end);
start = lastTrade.indexOf(">") + 1;
end = lastTrade.indexOf("<", start);
lastTradePrice = lastTrade.substring(start, end);
}
©SoftMoore Consulting
Slide 60