TCPIP - Department of Computer Science
Download
Report
Transcript TCPIP - Department of Computer Science
Computer Networks 364
Internet Architecture
John Morris
Computer Science/Electrical Engineering
University of Auckland
Email: [email protected]
URL: http:/www.cs.auckland.ac.nz/~jmor159
Internet Architecture
FTP
HTTP
NV
TCP
TFTP
UDP
IP
NET1
NET2
…….
NET3
Network protocols implemented in a
combination of hardware
and software, eg
Ethernet,
Fibre Distributed Data Interface (FDDI)
Internet Architecture
FTP
HTTP
NV
TCP
TFTP
UDP
Internet protocol supports interconnection of
multiple networking technologies
into a single, logical network
IP
NET1
NET2
…….
NET3
Internet Architecture
FTP
Transmission Control
Protocol reliable byte-stream
channel
HTTP
NV
TCP
TFTP
UDP
IP
NET1
NET2
…….
TCP, UDP: Transport protocols
(end-to-end protocols)
User Datagram
Protocol unreliable datagram
delivery channel
NET3
Sockets
Application Programming Interface (API)
►
Socket interface
Network interface
Provided by most OS’s
Originally provided by the Berkeley Unix distribution
►
►
API defines services provided by a protocol
Socket
Major abstraction
Point where a local application process attaches to the network
Operations
► Create a socket
► Attach the socket to the network
► Send / receive messages via the socket
► Close the socket
Sockets
Sockets are the “endpoints” of Internet connections
0
nnn
►
0
Internet
Ports
Machine
A
Ports
►
nnn
Machine
B
Socket constructors require Internet addresses
Socket( String host, int port ) throws
UnknownHostException,
IOException;
Socket(InetAddress address, int port) throws
IOException;
... and a host of other alternatives, see the API documentation!
►
Note: these constructors throw exceptions
Use them in a try { } catch .. block
Sockets
►
Note: these constructors throw exceptions
Use them in a try { } catch .. Block
String name = new String(“ciips.ee.uwa.edu.au”);
try {
Socket t = new Socket( name, 80 );
...
}
catch( UnknownHostException e ) {
System.out.println(“Unknown host:” + name );
}
catch( IOException e ) {
...
}
Note: UnknownHostException is a subclass of
IOException
catch( IOException e ) alone would suffice
Talking to a server
static final int lut_port = 3062;
String name = new String(“ciips.ee.uwa.edu.au”);
static final String query = “?”;
try {
Socket server = new Socket( name, lut_port );
DataInputStream input =
new DataInputStream( server.getInputStream() );
PrintStream output = new PrintStream( server.getOutputStream() );
do {
output.println( query );
String resp = input.readLine();
System.out.println( name + “ response: “ + resp );
} while ( !( resp.equal( quit_string ) ) );
server.close();
}
catch( UnknownHostException e ) {
System.out.println(“Unknown host:” + name );
}
catch( IOException e ) {
...
}
Talking to a server
Socket
(and almost all the rest)
throws an exception,
static final int lut_port = 3062;
String name = new String(“ciips.ee.uwa.edu.au”);
so put it all in a
static final String query = “?”;
try { } block
try {
Socket server = new Socket( name, lut_port );
DataInputStream input =
new DataInputStream( server.getInputStream() );
PrintStream output = new PrintStream( server.getOutputStream() );
do {
Once the Socket
output.println( query );
has been created,
String resp = input.readLine();
System.out.println( name + “ response: “ +create
resp input
);
and output
} while ( !( resp.equal( quit_string ) ) ); streams on it
server.close();
}
catch( UnknownHostException e ) {
System.out.println(“Unknown host:” + name );
}
DataInputStream
Just part of the stream
catch( IOException
e ) {
DataOutputStream
...
zoo - there are 55 more!
PrintStream
}
Talking to a server
static final int lut_port = 3062;
String name = new String(“ciips.ee.uwa.edu.au”);
static final String query = “?”;
Now use methods on the
try {
streams just
Socket server = new Socket( name, lut_port
); like any other
DataInputStream input =
I/O device
new DataInputStream(
server.getInputStream()
);
println
on PrintStream
PrintStream output = new PrintStream( server.getOutputStream() );
readLine on DataInputStream
do {
output.println( query );
String resp = input.readLine();
System.out.println( name + “ response: “ + resp );
} while ( !( resp.equal( quit_string ) ) );
server.close();
}
catch( UnknownHostException e ) {
System.out.println(“Unknown host:” + name );
}
catch( IOException e ) {
...
}
Talking to a server
Finally add some code to handle exceptions!
static final intChoices
lut_port
= 3062;
(Java
usually gives you plenty )
String name = new String(“ciips.ee.uwa.edu.au”);
Separate catch blocks for different types
static final String query = “?”;
One “catch all” (IOException)
try {
{ } catch
} for each
Socket server
= try
new Socket(
name, {lut_port
); method call
DataInputStream
input =the exception from here
Throw
new DataInputStream( server.getInputStream() );
PrintStream output = new PrintStream( server.getOutputStream() );
do {
output.println( query );
String resp = input.readLine();
System.out.println( name + “ response: “ + resp );
} while ( !( resp.equal( quit_string ) ) );
server.close();
}
catch( UnknownHostException e ) {
System.out.println(“Unknown host:” + name );
}
catch( IOException e ) {
...
}
Talking to a server - variant 1
Exception Choices (Java usually gives you plenty )
static
final
= 3062;
tryint{lut_port
} catch
{ } for each method call
String name = new String(“ciips.ee.uwa.edu.au”);
Throw the exception from here
static final String query = “?”;
try {
Socket server = new Socket( name, lut_port );
}
catch( UnknownHostException e ) {
System.out.println(“Unknown host:” + name ); return error_flag;
try { }
}
catch( IOException e ) {
catch { }
/* process other errors .... */
return error_flag;
around each
}
exception
throwing call
try {
DataInputStream input =
new DataInputStream( server.getInputStream() );
PrintStream output = new PrintStream( server.getOutputStream() );
}
catch( IOException e ) {
System.out.println(“Cant form streams”); return error_flag;
}
do { /* do some real work ... */
Talking to a server - variant 2
Exception Choices (Java usually gives you plenty )
try { } catch { } for each method call
Throw the exception from here
public String ServerResp( String query )
This method throws
throws IOException {
any exceptions
static final int lut_port = 3062;
up a level
String name = new String(“ciips.ee.uwa.edu.au”);
Socket server = new Socket( name, lut_port );
DataInputStream input =
new DataInputStream( server.getInputStream() );
PrintStream output = new PrintStream( server.getOutputStream() );
output.println( query );
String resp = input.readLine();
server.close();
return resp;
}
Simple Server
static final int lut_port = 3062;
static final int queue_len = 10;
try {
Socket server = new ServerSocket( lut_port, queue_len );
}
catch( IOException e ) {
System.out.println(“Can’t establish server on port “ + lut_port );
}
while( true ) { // The server never dies!
// Wait for a connection attempt
try {
Socket s = server.accept();
PrintStream ps = new PrintStream( s.getOutputStream() );
DataInputStream in = new DataInputStream( s.getInputStream() );
while( true ) {
String q = in.readLine();
if ( q.equal( “?” ) )
ps.println( “Hello” );
else if ( q.equal( “quit” ) ) break;
else
ps.println( “??” );
}
s.close();
// Close this connection
}
catch ( IOException e ) { /* Do something about it! */ }
}
Simple Server
Set up a ServerSocket
- basically just a socket
with an accept method
static final int lut_port = 3062;
static final int queue_len = 10;
try {
Socket server = new ServerSocket( lut_port, queue_len );
}
catch( IOException e ) {
System.out.println(“Can’t establish server on port “ + lut_port );
}
while( true ) { // The server never dies!
Servers usually live
// Wait for a connection attemtp
forever!!
try {
Socket s = server.accept();
The code will
PrintStream ps = new PrintStream( s.getOutputStream()
);block
here until a
DataInputStream in = new DataInputStream( s.getInputStream()
);
while( true ) {
client attempts
String q = in.readLine();
a connection
if ( q.equal( “?” ) )
ps.println( “Hello” );
else if ( q.equal( “quit” ) ) break;
else
ps.println( “??” );
}
s.close();
// Close this connection
}
catch ( IOException e ) { /* Do something about it! */ }
}
Simple Server
static final int lut_port = 3062;
static final int queue_len = 10;
try {
Socket server = new ServerSocket( lut_port, queue_len );
}
catch( IOException e ) {
System.out.println(“Can’t establish server on port “ + lut_port );
Now we just
}
while( true ) { // The
server
dies!& output streams
createnever
the input
// Wait for a connection
attemtp
read
and write to them
try {
close the connection when we’re finished
Socket s = server.accept();
PrintStream ps = new PrintStream( s.getOutputStream() );
DataInputStream in = new DataInputStream( s.getInputStream() );
while( true ) {
String q = in.readLine();
if ( q.equal( “?” ) )
ps.println( “Hello” );
else if ( q.equal( “quit” ) ) break;
else
ps.println( “??” );
}
s.close();
// Close this connection
}
catch ( IOException e ) { /* Do something about it! */ }
}
Simple Server
static final int lut_port = 3062;
static final int queue_len = 10;
try {
Socket server = new ServerSocket( lut_port, queue_len );
}
But ...
catch( IOException e ) {
This server blocks whilst one user is using it!
System.out.println(“Can’t establish server on port “ + lut_port );
A second client can’t connect ‘til the first one finishes!
}
while( true ) { // The server never dies!
// Wait for a connection attemtp
try {
Socket s = server.accept();
PrintStream ps = new PrintStream( s.getOutputStream() );
DataInputStream in = new DataInputStream( s.getInputStream() );
while( true ) {
String q = in.readLine();
if ( q.equal( “?” ) )
ps.println( “Hello” );
else if ( q.equal( “quit” ) ) break;
else
ps.println( “??” );
}
Solution ...// Close this connection
s.close();
A thread for each client
}
catch ( IOException e ) { /* Do something about it! */ }
}
Multi-threaded Server
Make this parameter relevant!
static final int lut_port = 3062;
Number of queued requests
static final int queue_len = 10;
try {
Socket server = new ServerSocket( lut_port, queue_len );
}
catch( IOException e ) {
System.out.println(“Can’t establish server on port “ + lut_port );
}
As soon as we get a
while( true ) { // The server never dies!
// Wait for a connection attempt
connection,
try {
spawn a handler thread
Socket s = server.accept();
to talk to the client
// This thread will do the talking
ClientHandler ch = new ClientHandler( s );
ch.start();
}
catch ( IOException e ) { /* Do something about it! */ }
}
Start the thread
Multi-threaded Server - The Handler
This class is a Thread
class ClientHandler extends Thread {
Socket s;
Constructor public ClientHandler( Socket s ) {
just save a reference to the
this.s = s;
socket
}
public void run() {
PrintStream ps = new PrintStream( s.getOutputStream() );
DataInputStream in = new DataInputStream( s.getInputStream() );
while( true ) {
String q = in.readLine();
if ( q.equal( “?” ) )
ps.println( “Hello” );
else if ( q.equal( “quit” ) ) break;
else
ps.println( “??” );
}
s.close();
// Close this connection
}
Same operational code ..
}
Just embedded in the
thread’s run method
UDP - Datagrams
►
Use a DatagramSocket
Constructor
DatagramSocket( int port ) throws
creates a datagram socket on port
IOException;
Methods
void receive( DatagramPacket p ) throws IOException;
void send( DatagramPacket p ) throws IOException;
►
DatagramPacket
Constructor
DatagramPacket( byte[] buf, int length,
InetAddress add, int port );
creates a packet to be sent to port at add
Methods
► byte[] getData();
► void setData( byte[] buf );
► InetAddress getAddress();
Datagrams, ...
►
►
Use a DatagramSocket
Constructor
DatagramSocket( int port ) throws
creates a datagram socket on port
IOException;
Methods
Note:
void receive( DatagramPacket p ) throws IOException
;
byte[]
void send( DatagramPacket p ) throws IOException
not;
DatagramPacket
char[]...
See your Java text
Constructor
DatagramPacket( byte[] buf, int length,
InetAddress add, int port );
creates a packet to be sent to port at add
Methods
► byte[] getData();
► void setData( byte[] buf );
► InetAddress getAddress();
Datagrams, ...
►
Differences
UDP is connectionless
► DatagramSocket can be closed at any time
►
No ‘close’ message is sent
Any socket on any machine can send a datagram addressed to this port
This socket doesn’t ‘know’ who was sending messages to it!
Compare TCP sockets: if either end closes, the other is notified
Each DatagramPacket contains its own destination address
One DatagramSocket can send messages to many different receivers
► Change the address in the DatagramPacket
►
A single DatagramSocket can receive messages from many sources
Use getInetAddress() on the received DatagramPacket to determine
the sender’s address and
Use getPort() to determine the port number:
dg_socket = new DatagramSocket( portxx );
DatagramPacket dp = new DatagramPacket( … );
dg_socket.receive( dp ); // Blocks until a packet arrives
InetAddress sender_add = dp.getInetAddress();
int sender_port = dp.getPort();
Thus a response can be sent if needed
Datagrams, ...
►
Differences
UDP is connectionless
► Threading is not needed to handle multiple sessions
A server application can manage multiple connections
It keeps track of the state of the session with each individual client
►
Broadcast
Packets can be addressed to the ‘broadcast’ address: xx.xx.xx.255
Received by all listeners on the specified port on that subnet
Message is only transmitted once on ‘bus’ networks (eg Ethernet)
► All receivers read and extract it from the net at the same time
►
Multicast capability
Software is simpler
► Simple state machine - each port is either sending, receiving or idle
No reliability management (acks, retrys, etc)
No congestion control
Minimum send rate possible
► (Subject to hardware capabilities!)
► TCP’s congestion control can ‘throttle back’ transmission rate
Smaller packet overhead
► UDP - 8byte header
vs
TCP - 20 byte header
Applications
em
Application
Application-layer protocol
Transport
protocol
TCP
e mail
Remote terminal
SMTP
telnet
Web
HTTP
TCP
File transfer
FTP
TCP
Remote file server
NFS
typically UDP
Streaming multimedia
Proprietary
typically UDP
Network management
SNMP
typically UDP
Name translation
DNS
typically UDP
Routing protocol
RIP
typically UDP
Internet telephony
Proprietary
typically UDP
TCP
UDP vs TCP
►
email, terminal access, web serving, file transfer
Not time critical
Lost of packets not tolerable
►
Routing protocol (RIP)
Updates every ~5mins
► Lost updates replaced by newer ones
►
Network management (SNMP)
Messages needed when network is ‘stressed’
► Reliable transport difficult at these times
►
Name lookup (DNS)
TCP connection establishment delay multiplied
Use UDP
► Single request
► Simple try again on failure, timeout, etc
UDP vs TCP
►
Multimedia (video, telephony, etc)
Natural redundancy in information
Packet loss easily tolerated
Congestion control mechanisms
Unpredictable delivery times
Unacceptable for voice, video, etc
but
► UDP can’t guarantee bandwidth either!
Network congestion leads to
► delay
► complete loss of information
►
Internet Telephony, etc, using UDP
Controversial
Impossible to guarantee anything!
Internet is a shared ‘store-and-forward’ network
► Routers store messages for variable times before forwarding
Time-critical services may work
► When other users have little demand
► but ...
Multicast sockets ...
►
Messages to multiple hosts
Use a MulticastSocket
Constructor
MulticastSocket( int port ) throws IOException;
creates a datagram socket bound to port
Inherited from
Methods
DatagramSocket
► void receive( DatagramPacket p ) throws IOException
;
► void send( DatagramPacket p ) throws IOException;
► void joinGroup( InetAddress mcastaddr )
throws IOException;
► void leaveGroup( InetAddress mcastaddr )
throws IOException;
Hosts can join and leave multicast groups
Security feature:
► An applet is not allowed to use a multicast socket
Protocols - HTTP
►
HyperText Transfer Protocol (HTTP)
WWW application layer protocol
Client: browser (Netscape, Opera, that other one, … )
Server: a web server (source of Web pages - Apache, … )
Defines the language used by clients to request web pages
► RFC 2616 (HTTP/1.1)
RFC = Request for Comment
Now managed by the Internet Engineering Task Force (IETF)
Over 2000
Standards for the Internet
► Default
port is 80
Protocols - HTTP
►
HyperText Transfer Protocol (HTTP)
Web pages consist of a number of objects
► Basic page
► Embedded images, etc
► Each object is fetched from the server in a single session
Open TCP connection
GET message from client
Response from server with object
Close connection
► Obviously
rather inefficient
TCP connection establishment is expensive
Persistent connections
► TCP connection is left open for subsequent requests
► Further
efficiency from pipelining
Send additional requests before first response received
Allows browser to do useful work while server is fetching objects
► Parsing to discover embedded objects,
► Formatting and displaying pages, etc