Application I

Download Report

Transcript Application I

Network Applications:
TCP Socket Programming/
FTP/HTTP
1/31/2012
Outline
 Admin and recap
 Network application programming
 FTP
 HTTP
2
Admin
 Programming assignment 0 is posted online
3
Recap: UDP Socket Space
server
Public address: 128.36.59.2
Local address: 127.0.0.1
UDP socket space
address: {127.0.0.1:9876}
snd/recv buf:
address: {128.36.59.2:9876}
snd/recv buf:
address: {*:6789}
snd/recv buf:
InetAddress sIP1 =
InetAddress.getByName(“localhost”);
DatagramSocket ssock1 = new
DatagramSocket(9876, sIP1);
InetAddress sIP2 =
InetAddress.getByName(“128.36.59.2”);
DatagramSocket ssock2 = new
DatagramSocket(9876,sIP2);
DatagramSocket serverSocket = new
DatagramSocket(6789);
address: {128.36.232.5:53}
snd/recv buf:
4
Recap: UDP Demultiplexing
server
Public address: 128.36.59.2
Local address: 127.0.0.1
UDP socket space
address: {127.0.0.1:9876}
snd/recv buf:
client
IP: A
P1
SP: x
DP: 9876
S-IP: A
D-IP: 127.0.0.1
address: {128.36.59.205:9876}
snd/recv buf:
P2
SP: y
DP: 9876
S-IP: B
D-IP: 128.36.59.2
address: {128.36.232.5:53}
snd/recv buf:
UDP demutiplexing is based on matching (dst address, dst port)
client
IP: B
5
Recap: Processing
import java.io.*;
import java.net.*;
getData() returns a pointer to
an underlying buffer array;
for efficiency, don’t assume
receive() will reset the rest of
the array
class UDPServer {
public static void main(String args[]) throws Exception {
…
// process data
String sentence = new String(receivePacket.getData(),
0, receivePacket.getLength());
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
getLength() returns how much
data is valid.
6
Recap: Data Encoding/Decoding
 Pay attention to encoding/decoding of data:
transport layer handles only a sequence of bytes
if not careful, query sent !=
query received (how?)
client
query
encoding
server
result
decoding
byte
array
7
Example Code: Query
See EncodingDecoding.java
Will we always get back the
same string?
client
String
(UTF-16)
String.getBytes()
server
String
(UTF-16)
String(rcvPkt,
0, rcvPkt.getLength());
byte
array
Depends on default local platform char set (why?) :
java.nio.charset.Charset.defaultCharset()
8
Recap: Example Code
 Please read chapter 4 of Java Network
Programming for more details
 Common mistake in many (textbook)
examples:

http://www.java2s.com/Code/Java/NetworkProtocol/UseDatagramSockettosendoutandrece
iveDatagramPacket.htm
9
Recap: UDP/DNS Implementation
Lessons learned:
 Standard UDP
demultiplexing (find out
return address by
src.addr/src.port of UDP
packet) does not always
work
 DNS solution:
identification: remember
the mapping
10
Outline
 Recap
 Network application programming
 UDP
 TCP
11
TCP Socket Design: Starting w/ UDP
server
Socket socket space
128.36.232.5
128.36.230.2
address: {*:9876}
snd/recv buf:
client1
P1
client2
P2
local address
local port
Issue: TCP is designed to provide a
pipe abstraction: server reads an
ordered sequence of bytes from each
connected client
sock.nextByte(client1);
Issue 2: How is the server notified
that a new client is connected?
newClient = sock.getNewClient()
BSD TCP Socket API Design
server
TCP socket space
128.36.232.5
128.36.230.2
address: {*:9876}
snd/recv buf:
socket for new
connected clients
client1
P1
address: {*:9876; client 1}
snd/recv buf:
client2
address: {*:9876; client 2}
snd/recv buf:
P2
Q: How to decide where to put a new packet?
A: Packet demutiplexing is based on four tuples:
(dst addr, dst port, src addr, src port)
TCP Connection-Oriented Demux
 TCP socket identified by 4-tuple:
 source IP address
 source port number
 dest IP address
 dest port number
 recv host uses all four values to direct segment to
appropriate socket

server can easily support many simultaneous TCP sockets:
different connections/sessions are automatically
separated into different sockets
Connection-Oriented Demux
P1
P4
P5
P2
P6
P1P3
SP: x
DP: 25
S-IP: B
D-IP: S
SP: x
client
IP: A
DP: 25
S-IP: A
D-IP: S
SP: y
server
IP: S
DP: 25
S-IP: B
D-IP: S
Client
IP:B
15
TCP Socket Big Picture
16
Summary: 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
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
Connection-oriented TCP: Big Picture
(C version)
server
welcomeSocket=socket(): create socket
client
clientSocket=socket(): create socket
bind(welcomeSocket, …): specify socket address/port bind(clientSocket): specify socket address
listen(welcomeSocket, …): specify that socket
welcomeSocket is a listening socket
optional
TCP
initialize TCP handshake to server; return
connection setup until TCP handshake is done
connectionSocket=accept(welcomeSocket, …):
get a connected connection
from the queue for socket welcomeSocket;
create a new socket identified by connectionSocket
connect(clientSocket, serverAddr, serverPort):
read()/write(): do IO on clientSocket
read()/write(): do IO on socket connectionSocket
close(clientSocket): done
close(connectionSocket): done
18
Client/server socket interaction: TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket(x)
TCP
create socket,
connect to hostid, port=x
connection setup clientSocket
=
wait for incoming
connection request
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
Server Flow
Create ServerSocket(6789)
connSocket = accept()
read request from
connSocket
-Welcome socket: the waiting room
-connSocket: the operation room
Serve the request
close connSocket
ServerSocket




ServerSocket()

creates an unbound server socket.

creates a server socket, bound to the specified port.

creates a server socket and binds it to the specified local port number, with the
specified backlog.
ServerSocket(int port)
ServerSocket(int port, int backlog)
ServerSocket(int port, int backlog, InetAddress bindAddr)




create a server with the specified port, listen backlog, and local IP address to bind to.
bind(SocketAddress endpoint)

binds the ServerSocket to a specific address (IP address and port number).

binds the ServerSocket to a specific address (IP address and port number).
bind(SocketAddress endpoint, int backlog)
Socket accept()

listens for a connection to be made to this socket and accepts it.
 close()
closes this socket.
21
(Client)Socket

Socket(InetAddress address, int port)

Socket(InetAddress address, int port, InetAddress localAddr, int localPort)

Socket(String host, int port)

bind(SocketAddress bindpoint)

connect(SocketAddress endpoint)

connect(SocketAddress endpoint, int timeout)

InputStreamget InputStream()

OutputStreamgetOutputStream()

close()
creates a stream socket and connects it to the specified port number at the specified IP
address.
creates a socket and connects it to the specified remote address on the specified remote
port.
creates a stream socket and connects it to the specified port number on the named host.
binds the socket to a local address.
connects this socket to the server.
connects this socket to the server with a specified timeout value.
returns an input stream for this socket.
returns an output stream for this socket.
closes this socket.
22
OutputStream
 public abstract class OutputStream

public abstract void write(int b) throws IOException

public void write(byte[] data) throws IOException

public void write(byte[] data, int offset, int length) throws IOException

public void flush( ) throws IOException

public void close( ) throws IOException
23
InputStream
 public abstract class InputStream

public abstract int read( ) throws IOException

public int read(byte[] input) throws IOException

public int read(byte[] input, int offset, int length) throws IOException

public long skip(long n) throws IOException

public int available( ) throws IOException

public void close( ) throws IOException
24
TCP Example
Example client-server app:
1) client reads line from
standard input (inFromUser
stream) , sends to server via
socket (outToServer
stream)
2) server reads line from socket
3) server converts line to
uppercase, sends back to
client
4) client reads, prints modified
line from socket
(inFromServer stream)
25
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));
sentence = inFromUser.readLine();
Socket clientSocket = new Socket(“server.name", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Example: Java client (TCP), cont.
Send line
to server
outToServer.writeBytes(sentence + '\n');
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
Create
input stream
attached to socket
modifiedSentence = inFromServer.readLine();
Read line
from server
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Create
welcoming socket
at port 6789
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()));
Under the Hood: TCP Multiplexing
server
TCP socket space
128.36.232.5
128.36.230.2
state: listening
address: {*:6789, *:*}
completed connection queue:
sendbuf:
recvbuf:
local port
local addr
client
TCP socket space
198.69.10.10
state: starting
address: {198.69.10.10:1500, *:*}
sendbuf:
recvbuf:
remote port
remote addr
state: listening
address: {*:25, *:*}
completed connection queue:
sendbuf:
recvbuf:
%netstat –p tcp –n -a
state: listening
address: {*:25, *:*}
completed connection queue:
sendbuf:
recvbuf:
Example: Client Initiates Connection
server
TCP socket space
128.36.232.5
128.36.230.2
state: listening
address: {*:6789, *.*}
completed connection queue:
sendbuf:
recvbuf:
state: listening
address: {*.25, *.*}
completed connection queue:
sendbuf:
recvbuf:
client
TCP socket space
198.69.10.10
state: connecting
address: {198.69.10.10:1500, 128.36.232.5:6789}
sendbuf:
recvbuf:
state: listening
address: {*.25, *.*}
completed connection queue:
sendbuf:
recvbuf:
Example: TCP Handshake Done
server
TCP socket space
128.36.232.5
128.36.230.2
state: listening
address: {*:6789, *:*}
completed connection queue:
{128.36.232.5.6789, 198.69.10.10.1500}
sendbuf:
recvbuf:
state: listening
address: {*:25, *:*}
completed connection queue:
sendbuf:
recvbuf:
client
TCP socket space
198.69.10.10
state: connected
address: {198.69.10.10:1500, 128.36.232.5:6789}
sendbuf:
recvbuf:
state: listening
address: {*:25, *:*}
completed connection queue:
sendbuf:
recvbuf:
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
Wait, on welcoming
socket for contact
by client
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
Example: Server accept()
server
TCP socket space
128.36.232.5
128.36.230.2
state: listening
address: {*.6789, *:*}
completed connection queue:
sendbuf:
recvbuf:
client
TCP socket space
198.69.10.10
state: connected
address: {198.69.10.10.1500, 128.36.232.5:6789}
sendbuf:
recvbuf:
state: established
address: {128.36.232.5:6789, 198.69.10.10.1500}
sendbuf:
recvbuf:
state: listening
address: {*.25, *:*}
completed connection queue:
sendbuf:
recvbuf:
state: listening
address: {*.25, *:*}
completed connection queue:
sendbuf:
recvbuf:
Packet demutiplexing is based on (dst addr, dst port, src addr, src port)
Packet sent to the socket with the best match!
Example: Java server (TCP):
Processing
Create input
stream, attached
to socket
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
Read in line
from socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(capitalizedSentence);
}
}
}
Example: Java server (TCP): Output
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
Create output
stream, attached
to socket
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
Write out line
to socket
outToClient.writeBytes(capitalizedSentence);
}
}
}
End of while loop,
loop back and wait for
another client connection
Analysis
 Assume that client requests arrive at a
rate of lambda/second
 Assume that each request takes 1/mu
seconds
 Some basic questions
How long is the queue at the welcome socket?
 What is the response time of a request?

Welcome
Socket
Queue
36
Analysis
 Is there any interop issue in the sample
program?
37
Discussion
 Is there any interop issue in the sample
program?

DataOutputStream writeBytes(String)
truncates
– http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOu
tputStream.html#writeBytes(java.lang.String)
38
FTP: the File Transfer Protocol
user
at host
FTP
FTP
user
client
interface
file transfer
local file
system
FTP
server
remote file
system
 Transfer files to/from remote host
 Client/server model

client: side that initiates transfer (either to/from
remote)
 server: remote host
 ftp: RFC 959
 ftp server: port 21/20 (smtp 25, http 80)
39
FTP Commands, Responses
Sample commands:
Sample return codes
 sent as ASCII text over control
 status code and phrase






channel
USER username
PASS password
LIST return list of file in current
directory
RETR filename retrieves
(gets) file
STOR filename stores file
PORT h1,h2,h3,h4,p1,p2
specifies the IP address and port
the client receives its data
 331 Username OK,
password required
 125 data connection
already open;
transfer starting
 425 Can’t open data
connection
 452 Error writing
file
40
FTP Protocol Design
 What is the simplest design?
 See FTP.pdf
41
FTP: A Client-Server Application with
Separate Control, Data Connections
 Two parallel TCP
connections opened:
 control: exchange
commands, responses
between client,
server.
“out of band
control”
 data: file data
to/from server
FTP
server
FTP
client
TCP control connection
port 21 at server
PORT clientip:cport
RETR file.dat
TCP data connection
server:20
clientip:cport
Discussion: why separate control/data connections?
42
Question to Think
 Although they are all C-S apps, they
contain different features
Email
 DNS
 FTP

43
Outline
 Recap
 FTP
 Web
44
The Web: Some Jargon
 Web page:
 consists of “objects”
 addressed by a URL
 Most Web pages
consist of:


base HTML page, and
several referenced
objects
 URL has two
components: host
name, port number and
path name:
 User agent for Web is
called a browser, e.g.


Mozilla Firefox
MS Internet Explorer
 Server for Web is
called Web server:


Apache
MS Internet
Information Server
http://www.cs.yale.edu:80/index.html
45
The Web: the HTTP Protocol
HTTP: hypertext transfer
protocol
 Web’s application layer
protocol
 HTTP uses TCP as transport
service
 client/server model
 client: browser that
requests, receives,
“displays” Web objects
 server: Web server sends
objects in response to
requests
 http1.0: RFC 1945
PC running
Explorer
Server
running
Apache Web
server
Linux running
Firefox
 http1.1: RFC 2068
46
HTTP 1.0 Message Flow
 Client initiates TCP connection (creates socket) to




server, port 80
Server waits for requests from clients
Client sends request for a document
Web server sends back the document
TCP connection closed
 Client parses the document to find embedded
objects (images)
 repeat above for each image
47
HTTP 1.0 Message Flow (more detail)
Suppose user enters URL
www.cs.yale.edu/index.html
1a. http client initiates TCP
connection to http server
(process) at www.cs.yale.edu.
Port 80 is default for http
server.
0. http server at host
www.cs.yale.edu waiting
for TCP connection at port
80.
1b. server “accepts”
connection, ack. client
2. http client sends http
request message
(containing URL) into
TCP connection socket
time
3. http server receives request
message, forms response
message containing requested
object (index.html), sends
message into socket (the
sending speed increases slowly,
which is called slow-start)
48
HTTP 1.0 Message Flow (cont.)
4. http server closes TCP
connection.
5. http client receives response
message containing html file,
parses html file, finds
embedded image
time
6. Steps 1-5 repeated for each
of the embedded images
49
HTTP Request Message: General Format
 ASCII (human-readable format)
50
HTTP Request Message Example: GET
request line
(GET, POST,
HEAD commands)
GET /somedir/page.html HTTP/1.0
Host: www.somechool.edu
Connection: close
header User-agent: Mozilla/4.0
lines Accept: text/html, image/gif, image/jpeg
Accept-language: en
Carriage return,
line feed
(extra carriage return, line feed)
indicates end
of message
51
HTTP Response Message
status line
(protocol
status code
status phrase)
header
lines
data, e.g.,
requested
html file
HTTP/1.0 200 OK
Date: Wed, 23 Jan 2008 12:00:15 GMT
Server: Apache/1.3.0 (Unix)
Last-Modified: Mon, 22 Jun 1998 …...
Content-Length: 6821
Content-Type: text/html
data data data data data ...
52
HTTP Response Status Codes
In the first line of the server->client response message. A few
sample codes:
200 OK

request succeeded, requested object later in this message
301 Moved Permanently

requested object moved, new location specified later in
this message (Location:)
400 Bad Request

request message not understood by server
404 Not Found

requested document not found on this server
505 HTTP Version Not Supported
53
Trying out HTTP (client side) for yourself
1. Telnet to your favorite Web server:
telnet www.yale.edu 80
Opens TCP connection to port 80
(default http server port) at www.yale.edu.
Anything typed in sent
to port 80 at www.yale.edu
2. Type in a GET http request:
GET /index.html HTTP/1.0
By typing this in (hit carriage
return twice), you send
this minimal (but complete)
GET request to http server
3. Look at response message sent by the http server.
54