1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to...

29
1 cs205: engineering software cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet (1999)

Transcript of 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to...

Page 1: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

1cs205: engineering software

cs205: engineering softwareuniversity of virginia fall 2006

Network Programming*

* Just enough to make you dangerous

Bill Cheswick’s map of the Internet (1999)

Page 2: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

2cs205: engineering software

Reminder

• Project progress reports due Monday after Thanksgiving

Page 3: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

3cs205: engineering software

Not just Buttons

http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

Component

JComponent

AbstractButton

JButtonJToggleButton

JCheckBox JRadioButton

Page 4: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

4cs205: engineering software

Menus Too...

http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html

JComponent

AbstractButton

JButtonJMenuItem

JMenu JCheckboxMenuItemJRadioButtonMenuItem

JMenuBar JPopupMenu

Awkward design:JMenu is a button – action is to popup JPopupMenuJMenu contains a list of JMenuItem’sWhy is JMenu a subtype of JMenuItem?

Page 5: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

5cs205: engineering software

Client-Server

Client Server

Listeningrequest

connection

Same host can be both a client anda server (peer-to-peer networking)

Page 6: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

6cs205: engineering software

Network Topologies

• Client-ServerClient Server

Server

Client Client

“Star”

Node

Node

Node

“Mesh”

Page 7: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

7cs205: engineering software

Ports

Client Server

Listeningrequest

connectionPortPort

Page 8: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

8cs205: engineering software

Port Numbers• Ports 0-1023: assigned by Internet

Assigned Numbers Authority– Privileged programs– 80: http, 110: pop3, 205: appletalk– http://www.iana.org/assignments/port-numbers

• Ports 1024-49151: registered– Any application can use these

• Ports 49152-65535: dynamic/private

Page 9: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

9cs205: engineering software

GUIs and Networks

• GUIs: great problem for subtyping and inheritance– OOP was invented to program GUIs (and

built simulations)

• Network programming: great problem for data abstraction

Why are GUIs great for OOP and networks great for data abstraction?

Page 10: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

10cs205: engineering software

Application

SnakesServer

Presentation

Session

Transport

NetworkSegments

Data

Transformed Data

“Dialog” Data

Packets

Data Link

Physical Bits

Frames

Application

Presentation

Session

Transport

Network

Data Link

Physical

Page 11: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

11cs205: engineering software

• OSI Abstraction Layers

• 7 Layers

Application

Presentation

Session

Transport

NetworkSegments

Data

Transformed Data

“Dialog” Data

Packets

Data Link

Physical Bits

Frames

Page 12: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

12cs205: engineering software

Java Sockets

Client Server

Listeningrequest

connectionPortPort

java.net.ServerSocketjava.net. Socket

Page 13: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

13cs205: engineering software

java.net.ServerSocket

public ServerSocket(int port) throws IOException EFFECTS: Initializes this to a new server socket on port. If the socket cannot be created, throws IOException.

Page 14: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

14cs205: engineering software

Server Socketsimport java.net.*;import java.io.IOException;

public class Network { static public void main(String args[]) { ServerSocket ss1, ss2; try { ss1 = new ServerSocket (8000); } catch (IOException ioe) { System.err.println ("Exception 1: " + ioe); } try { ss2 = new ServerSocket (8000); } catch (IOException ioe) { System.err.println ("Exception 2: " + ioe); }

Page 15: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

15cs205: engineering software

Running Server Socketsimport java.net.*;import java.io.IOException;

public class Network { static public void main(String args[]) { ServerSocket ss1, ss2; try { ss1 = new ServerSocket (8000); } catch (IOException ioe) { System.err.println ("Exception 1: " + ioe); } try { ss2 = new ServerSocket (8000); } catch (IOException ioe) { System.err.println ("Exception 2: " + ioe); }

Exception 2: java.net.BindException: Address already in use: JVM_Bind

Page 16: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

16cs205: engineering software

Accepting Connections

public Socket accept() throws IOException Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

Page 17: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

17cs205: engineering software

Serverpublic class Server { static public void main(String args[]) { ServerSocket listener; try { listener = new ServerSocket (8000); } catch (IOException ioe) { System.err.println ("Cannot open server socket: " + ioe); return; }

System.out.println("Server: waiting for connection..."); try { Socket sock = listener.accept(); ... } catch (IOException ioe) { System.err.println ("Cannot accept: " + ioe); }

Page 18: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

18cs205: engineering software

Sockets

Socket(String host, int port) throws IOException, UnknownHostException     EFFECTS: Creates a stream socket and connects it to the specified port number on the named host. If the specified host is null it is the loopback address.

public void close () throws IOException EFFECTS: Closes this socket.

Page 19: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

19cs205: engineering software

Creating a Socketimport java.net.Socket;import java.io.*;import java.net.UnknownHostException;

public class Client { public static void main(String[] args) { Socket connect; try { connect = new Socket ("www.microsoft.com", 80); System.out.println ("Connected: " + connect); } catch (UnknownHostException uhe) { System.err.println("Cannot find host: " + uhe); } catch (IOException ioe) { System.err.println ("Cannot open connection: " + ioe); } }}

Port 80 = http (web server)

Page 20: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

20cs205: engineering software

Connected to Microsoftimport java.net.Socket;import java.io.*;import java.net.UnknownHostException;

public class Client { public static void main(String[] args) { Socket connect; try { connect = new Socket ("www.microsoft.com", 80); System.out.println ("Connected: " + connect); } catch (UnknownHostException uhe) { System.err.println("Cannot find host: " + uhe); } catch (IOException ioe) { System.err.println ("Cannot open connection: " + ioe); } }}

Connected: Socket[addr=www.microsoft.com/207.46.19.30, port=80,localport=1359]

Connected: Socket[addr=www.microsoft.com/207.46.225.60, port=80,localport=1371]

Page 21: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

21cs205: engineering software

CommunicatingSocket methods:public OutputStream getOutputStream() throws IOException

EFFECTS: Returns an output stream for this socket.

public InputStream getInputStream() throws IOException

EFFECTS: Returns an input stream for this socket.

Page 22: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

22cs205: engineering software

Input Streams

public abstract class InputStream abstract int read() EFFECTS: Returns the next byte in the input stream and advances the stream.

other methods: close, available, skip, etc.

Why an int not a byte?

Page 23: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

23cs205: engineering software

Readers

• java.io.InputStreamReader extends Reader– Higher level abstraction on an

InputStreamInputStreamReader(InputStream in) EFFECTS: Initializes this to an InputStreamReader on stream in.int read() EFFECTS: Read a single character

Page 24: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

24cs205: engineering software

Buffering

BufferedReader extends Reader { BufferedReader(Reader r) int read () String readLine () EFFECTS: Returns the next line and advances the reader.

Page 25: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

25cs205: engineering software

Cookbook...

BufferedReader in = new BufferedReader ( new InputStreamReader ( connect.getInputStream()));

PrintWriter out = new PrintWriter (connect.getOutputStream(), true);

autoflushing

Page 26: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

26cs205: engineering software

A Simple Web Browserimport java.net.Socket;import java.io.*;import java.net.UnknownHostException;

public class Client { public static void main(String[] args) { Socket connect; try { connect = new Socket("www.microsoft.com", 80); System.out.println("Connected"); } catch (UnknownHostException uhe) { ... return; } catch (IOException ioe) { System.err.println("Cannot open ... "); return; }

try { PrintWriter out = new PrintWriter (connect.getOutputStream(), true); BufferedReader in = new BufferedReader (new InputStreamReader( connect.getInputStream())); out.println("GET / HTTP/1.0\r\n"); String inString; while ((inString = in.readLine()) != null) { System.out.println (inString); } } catch (IOException ioe) { System.err.println("IO Exception: " + ioe);}System.out.println ("Closing connection...");try { connect.close();} catch (IOException ioe) { System.err.println("Error closing: " + ioe);}

Page 27: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

27cs205: engineering software

Higher Abstraction Levels

java.net.HttpURLConnectionURLConnection (URL url) Constructs a URL connection to the specified URL. void connect()Object getContent(Class [] classes)

http://java.sun.com/docs/books/tutorial/networking/urls/

Page 28: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

28cs205: engineering software

public class Server { static public void main(String args[]) { ServerSocket listener; try { listener = new ServerSocket(8000); } catch (IOException ioe) { System.err.println("Cannot open server socket: " + ioe); return; } while (true) { try { System.out.println("Server: waiting for connection..."); Socket connect = listener.accept(); PrintWriter out = new PrintWriter(connect.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( connect.getInputStream())); String inString; while ((inString = in.readLine()) != null) { // HTTP request: GET <file> <protocol> ... // Process request by printing to out } connect.close(); } catch (IOException ioe) { // log error }...

What would need tobe different in a real web server?

Page 29: 1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.

29cs205: engineering software

Charge

• Sun’s Networking tutorial:http://java.sun.com/docs/books/tutorial/networking/

• Enjoy your Thanksgiving!– Project progress reports due Monday

after