Download Transport Layer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Networks
Transport layer (last part)
Application layer
1
Transport layer
• Done except for TCP Reno fast recovery
• This class
– Finish TCP Reno fast recovery
– Start application layer
• Socket programming
• HTTP
2
TL: Reno fast recovery example
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
15 16 17 18 19 20 21 22 23 24
cwnd=8
23
base
S
22
21
20
19
18
17
16
D
Ack
16
(15)
3
TL: Reno fast recovery example
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
cwnd=8
23
base
S
22
21
15 16 17 18 19 20 21 22 23 24
20
19
18
17
16
X
D
4
TL: Reno fast recovery example
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
23
cwnd=8
15 16 17 18 19 20 21 22 23 24
22
21
20
19
18
base
D
S
Ack
16
(17)
D
S
Ack
16
(17)
Ack
16
(18)
Ack
16
(19)
Ack
16
(20)
Ack
16
(21)
Ack
16
(22)
Ack
16
(23)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
15 16 17 18 19 20 21 22 23 24
cwnd=8
base
5
TL: Reno fast recovery example
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
15 16 17 18 19 20 21 22 23 24
3rd Dup. Ack 13
cwnd=8
base
D
S
Ack
16
(17)
Ack
16
(18)
Ack
16
(19)
Ack
16
(20)
Ack
16
(21)
Ack
16
(22)
Ack
16
(23)
16
D
S
Ack
16
(19)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
cwnd_to_use_after_recovery=4
inflated_cwnd=4+3=7
base
Ack
16
(20)
Ack
16
(21)
Ack
16
(22)
Ack
16
(23)
15 16 17 18 19 20 21 22 23 24
6
TL: Reno fast recovery example
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
15 16 17 18 19 20 21 22 23 24
16
cwnd_to_use_after_recovery=4
inflated_cwnd=8
base
D
S
Ack
16
(20)
Ack
16
(21)
Ack
16
(22)
Ack
16
(23)
16
24
D
S
Ack
16
(21)
Ack
16
(22)
Ack
16
(23)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
cwnd_to_use_after_recovery=4
inflated_cwnd=9
base
7
TL: Reno fast recovery example
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
15 16 17 18 19 20 21 22 23 24
cwnd_to_use_after_recovery=4
inflated_cwnd=12
base
27
26
25
24
D
S
Ack
24
(16)
27
S
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
26
25
24
D
15 16 17 18 19 20 21 22 23 24
8
cwnd=4
base
TL: Reno fast recovery example
• What if the retransmission is lost?
– Window inflation to support sending at halved rate until
eventual RTO
• Reference
– http://www.rfc-editor.org/rfc/rfc2001
9
Application layer
• Network programming
– API to transport layer
• BSD socket API
• java.net API
• Application layer functions
• Specific application layers
–
–
–
–
http
ftp
smtp
dns
10
AL: Network programming
• How can applications programmatically access
TCP/UDP/IP?
– Many possible interfaces
– Too many to teach adequately in detail
– Socket APIs
• BSD socket API
• Java socket API
– Other APIs
• Client-side Java URLconnections
• Server-side Java servlets
• RPC, CORBA, Java RMI (not covered)
11
AL: Socket programming
Socket API
• introduced in BSD4.1 UNIX, 1981
• explicitly created, used, released by
apps
• client/server paradigm
• three main types of transport service
via socket API:
– unreliable datagram
– reliable, byte stream-oriented
– raw IP datagrams
socket
a host-local, applicationcreated/owned,
OS-controlled interface
(a “door”) into which
application process can
both send and
receive messages to/from
another (remote or
local) application process
12
AL: Socket-programming using TCP
Socket: a door between application process and end-endtransport protocol (UCP or TCP)
TCP service: reliable transfer of bytes from one process to
another
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
host or
server
internet
socket
TCP with
buffers,
variables
controlled by
application
developer
controlled by
operating
system
host or
server
13
AL: Socket programming with TCP
Client must contact server
• server process must first
be running
• server must have created
socket (door) that
welcomes client’s contact
Client contacts server by:
• creating client-local TCP
socket
• specifying IP address,
port number of server
process
• When client creates socket:
client TCP establishes
connection to server TCP
• When contacted by client, server
TCP creates new socket for
server process to communicate
with client
– allows server to talk with
multiple clients (using
multiple sockets)
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
14
AL: BSD sockets in C/Unix
• Socket API (socket.h)
• socket(): create unnamed socket (data structure)
– UDP (SOCK_DGRAM), TCP (SOCK_STREAM)
– IP (SOCK_RAW)
• bind(): name socket (bind local address to socket)
• listen(): enable socket to accept connections
• accept(): get connect() request from listen queue, allocate file
descriptor for new socket
• connect(): initiate connection on a socket (TCP handshake)
• send(), sendto(), sendmsg(), writev(), write(): send data
• recv(), recvfrom(), recvmsg(), readv(), read(): receive data
• setsockopt(), getsockopt(): set socket options (such as buffer sizes,
flag fields)
• close(), shutdown(): teardown connection
15
AL: BSD sockets in action
UDP example
client
server
socket()
socket()
bind()
bind()
sendto()
TCP example
client
server
socket()
socket()
bind()
connect()
listen()
accept()
recvfrom()
write()
read()
sendto()
write()
recvfrom()
read()
16
AL: BSD example
• For reference only….
– http://www.cse.ogi.edu/class/cse524
17
AL: Java network programming
Java network applications
java.net package
System-dependent
implementations
18
AL: Java installation on church/state
• Programming assignment
• J2SE 1.3
– javac
• java compiler
– java
• java interpreter
• http://www.ibiblio.org/javafaq/javatutorial.html
• Example program
– http://www.cse.ogi.edu/class/cse524
19
AL: java.net classes
•
•
•
•
Low-level networking classes (Sockets and Packets)
High-level URL networking classes
http://java.sun.com/j2se/1.3/docs/api/index.html
java.lang.Object
– java.net.Socket
– java.net.ServerSocket
– java.net.DatagramSocket
• java.net.MulticastSocket
– java.net.DatagramPacket
– java.net.URL
– java.net.URLConnection
• java.net.HttpURLConnection
– java.net.URLencoder
– java.net.InetAddress
20
AL: java.net.Socket
• Constructors
– Socket(InetAddress, int)
– Socket(String, int)
– Socket(InetAddress, int, InetAddress, int)
• Some methods
–
–
–
–
–
–
–
getInputStream()
getOutputStream
getInetAddress()
getPort()
getLocalAddress()
getLocalPort()
get/set individual socket options
21
AL: java.net.ServerSocket
• Constructors
– Socket(int)
– Socket(int, int) // backlog specified
– Socket(int, int, InetAddress) // local address and backlog specified
• Some methods
– accept()
– getInetAddress()
– getLocalPort()
22
AL: Socket programming with TCP
Example client-server app:
• client reads line from standard
input (inFromUser stream) ,
sends to server via socket
(outToServer stream)
Input stream: sequence of bytes
into process
Output stream: sequence of bytes
out of process
• server reads line from socket
• server converts line to uppercase,
sends back to client
• client reads, prints modified line
from socket (inFromServer
stream)
client socket
23
AL: Java client/server socket interaction: TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
wait for incoming
connection request connection
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
24
AL: Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
Create
input stream
Create
client socket,
connect to server
Create
output stream
attached to socket
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
25
AL: Example: Java client (TCP), cont.
Create
input stream
attached to socket
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
Send line
to server
outToServer.writeBytes(sentence + '\n');
Read line
from server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
26
AL: Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Create
welcoming socket
at port 6789
Wait, on welcoming
socket for contact
by client
Create input
stream, attached
to socket
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
27
AL: Example: Java server (TCP), cont
Create output
stream, attached
to socket
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
Write out line
to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
End of while loop,
loop back and wait for
another client connection
28
AL: Socket programming with UDP
UDP: no “connection” between
client and server
• no handshaking
• sender explicitly attaches IP
address and port of
destination
• server must extract IP
address, port of sender from
received datagram
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
UDP: transmitted data may be
received out of order, or lost
29
AL: java.net.DatagramSocket
• Constructors
– DatagramSocket()
– DatagramSocket(int) // bind to specific port
– DatagramSocket(int, InetAddress) // specify local address
• Some methods
–
–
–
–
–
getLocalAddress()
getLocalPort()
receive(DatagramPacket)
send(DatagramPacket)
get/set individual socket options
30
AL: java.net.DatagramPacket
• Constructors
– DatagramPacket(byte[], int) // receiving packets
– DatagramPacket(byte[], int, InetAddress, int) // sending packets
• Some methods
–
–
–
–
–
–
–
–
getAddress() // remote address
getPort() // remote port
getLength() // get packet length
getData() // return data received or to be sent
setAddress(InetAddress) // set remote address
setData(byte[]) // set packet data
setLength(int) // set packet length
setPort(int) // set remote port
31
AL: Client/server socket interaction: UDP
Server (running on hostid)
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port umber
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read reply from
clientSocket
close
clientSocket
32
AL: Example: Java client (UDP)
import java.io.*;
import java.net.*;
Create
input stream
Create
client socket
Translate
hostname to IP
address using DNS
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("hostname");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
33
AL: Example: Java client (UDP), cont.
Create datagram
with data-to-send,
length, IP addr, port
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
Send datagram
to server
clientSocket.send(sendPacket);
Read datagram
from server
clientSocket.receive(receivePacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
34
AL: Example: Java server (UDP)
import java.io.*;
import java.net.*;
Create
datagram socket
at port 9876
class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
received datagram
Receive
datagram
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
35
AL: Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Get IP addr
port #, of
sender
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
Create datagram
to send to client
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Write out
datagram
to socket
serverSocket.send(sendPacket);
}
}
}
End of while loop,
loop back and wait for
another datagram
36
AL: High-level Java networking classes
• Socket/Packet
– Low level building blocks
– Must implement all application-level logic
• Many protocols based on URLs and/or tunneled in http
– Program at a higher-level to hide underlying protocol details
– Do not re-implement http, URL parsing, mime handling for
each application
37
AL: High-level client-side Java networking
classes
• java.net.URL
– Represent a URL object
• java.net.URLConnection
– Represent a connection to a URL which can be read and
written from
• java.net.HttpURLConnection
– Subclass of URLConnection for http:// URLs
38
AL: High-level server-side Java networking
classes
• Servlets
– Dynamically generate content
– Implement common protocol header logic
• Example http servlets
– Cookies
– Content-type
– Content-length
• Servlet classes
– javax.servlet.Servlet
• init()
• service()
• destroy()
–
–
–
–
–
javax.servlet.ServletRequest
javax.servlet.ServletResponse
javax.servlet.HttpServlet
javax.servlet.HttpServletRequest
javax.servlet.HttpServletResponse
39
Related documents