Download Slide 1

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
Lecture 10
TCP/IP Application Layer (2)
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Outline (Application Layer)
• Principles of network applications
• Web and HTTP
• FTP
• Electronic Mail
– SMTP, POP3, IMAP
• DNS
• Socket programming with TCP
• Socket programming with UDP
• Building a Web server
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
DNS: Domain Name System
Internet hosts, routers:
– IP address (32 bit) - used for addressing datagrams
– “name”, e.g., ww.yahoo.com - used by humans
• Hostname to IP address translation
• Host aliasing (canonical hostname)
– A host with a complicated hostname can have one or more alias names, E.g.
relay1.west-coast.enterprise.com could have, say, two aliases such as
enterprise.com and www.enterprise.com. Alias hostnames, are typically
more mnemonic than a canonical hostname. DNS can be invoked by an
application to obtain the canonical hostname for a supplied alias hostname
as well as the IP address of the host.
• Mail server aliasing
– Similar to host aliasing, e.g. the canonical hostname for hotmail might be
something like relay1.west-coast.hotmail.com
• Load distribution
– Replicated Web servers: set of IP addresses for one canonical name. DNS
server responds with the entire set of IP addresses, but rotates the ordering
of the addresses within each reply.
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
DNS: Domain Name System
• DNS uses a large number of name servers, organized in a
hierarchical fashion and distributed around the world.
• No one name server has all of the mappings for all of the
hosts in the Internet. Instead, the mappings are distributed
across the name servers.
• There are three types of name servers:
– local name servers,
– root name servers, and
– authoritative name servers.
Why not centralize DNS?
•
•
•
•
single point of failure
traffic volume
distant centralized database
maintenance
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Local Name Server
• Each ISP (residential ISP, company, university)
has one.
– Also called “default name server”
• When a host makes a DNS query, query is sent
to its local DNS server
– The IP address of the local name server is typically
configured by hand in a host.
– If a host requests a translation for another host that is
part of the same local ISP, then the local name server
will be able to immediately provide the requested IP
address.
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Root Name Servers
• contacted by local name server that can not resolve name
• root name server:
– If the root name server has a record for the hostname, it
sends a DNS reply message to the local name server
– contacts authoritative name server if name mapping not
known, gets mapping and returns mapping to local name
server
a Verisign, Dulles, VA
c Cogent, Herndon, VA (also Los Angeles)
d U Maryland College Park, MD
k RIPE London (also Amsterdam,
g US DoD Vienna, VA
Frankfurt) Stockholm (plus 3
i Autonomica,
h ARL Aberdeen, MD
other locations)
j Verisign, ( 11 locations)
m WIDE Tokyo
e NASA Mt View, CA
f Internet Software C. Palo Alto,
CA (and 17 other locations)
b USC-ISI Marina del Rey, CA
l ICANN Los Angeles, CA
Khaled Mahbub, IICT, BUET, 2008
13 root name
servers worldwide
ICT 6621 : Advanced Networking
Authoritative Servers
• The authoritative name server for a host is a name
server in the host's local ISP
• A name server is authoritative for a host if it always has a
DNS record that translates the host's hostname to that
host's IP address.
DNS: Caching and Updating Records
• once (any) name server learns mapping, it caches
mapping
– cache entries timeout (disappear) after some time
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Example
1.
2.
3.
4.
5.
6.
the host surf.eurecom.fr first sends a
DNS query message to its local name
server, dns.eurecom.fr. The hostname
to be translated, is gaia.cs.umass.edu.
The local name server forwards the
query message to a root name server
the root name server forwards a query
for a host with hostname ending with
umass.edu to the name server
dns.umass.edu.
This name server forwards all queries
with hostnames ending with
.cs.umass.edu to the name server
dns.cs.umass.edu, which is
authoritative for all hostnames ending
with .cs.umass.edu.
The authoritative name server sends
the desired mapping to the intermediate
name server, dns.umass.edu
And so on….
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
DNS Query Types
recursive query:
• puts burden of name
resolution on
contacted name
server
iterated query:
• contacted server
replies with name of
server to contact
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
DNS Records
DNS: distributed db storing resource records (RR)
RR format: (name,
• Type=A
– name is hostname
– value is IP address
– e.g. (relay1.bar.foo.com,
145.37.93.126,A)
• Type=NS
– name is domain (e.g.
foo.com)
– value is IP address of
authoritative name server for
this domain
– e.g. (foo.com, dns.foo.com,
NS)
Khaled Mahbub, IICT, BUET, 2008
value, type, ttl)
• Type=CNAME
– name is alias name for some
“cannonical” (the real) name
www.ibm.com is really
servereast.backup2.ibm.com
– value is cannonical name
– e.g. (foo.com, relay1.bar.foo.com,
CNAME)
• Type=MX
– value is name of mail server
associated with name
– e.g. (foo.com. mail.bar.foo.com, MX)
ICT 6621 : Advanced Networking
DNS Protocol, Messages
DNS protocol : query and reply messages, both with same
message format
message header
• identification: 16 bit #
for query, reply to query
uses same #
• flags:
– query or reply (0/1)
– recursion desired
– recursion available
– reply is authoritative
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
DNS Protocol, Messages
Name, type fields
for a query
RRs in response
to query
records for
authoritative servers
additional “helpful”
info that may be used
Programs for Exploring DNS
• There are various client programs available for exploring the contents of name
servers in the Internet, e.g. nslookup : allows to enter a hostname and they
return an IP address. Visit
http://namespace.pgmedia.net/nslookup/
http://www.infobear.com/nslookup-form.cgi
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Outline (Application Layer)
• Principles of network applications
• Web and HTTP
• FTP
• Electronic Mail
– SMTP, POP3, IMAP
• DNS
• Socket programming with TCP
• Socket programming with UDP
• Building a Web server
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Socket Programming
Goal: learn how to build client/server application that
communicate using sockets
Socket API
• introduced in BSD4.1
UNIX, 1981
• explicitly created, used,
released by applications
• client/server paradigm
• two types of transport
service via socket API:
socket
a host-local,
application-created,
OS-controlled interface
(a “door”) into which
application process can
both send and
receive messages to/from
another application
process
– unreliable datagram
– reliable, byte stream-oriented
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Socket-Programming using TCP
Socket: a door between application process and
end-end-transport 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
Khaled Mahbub, IICT, BUET, 2008
internet
socket
TCP with
buffers,
variables
controlled by
application
developer
controlled by
operating
system
host or
server
ICT 6621 : Advanced Networking
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
Khaled Mahbub, IICT, BUET, 2008
• When contacted by client,
server TCP creates new
socket for server process to
communicate with client
– allows server to talk with
multiple clients
– source port numbers used
to distinguish clients
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
ICT 6621 : Advanced Networking
Stream Jargon
• A stream is a sequence of characters that flow into or out
of a process.
• An input stream is attached to some input source for the
process, e.g., keyboard or socket.
• An output stream is attached to an output source, e.g.,
monitor or socket.
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Socket Programming with TCP
Khaled Mahbub, IICT, BUET, 2008
output
stream
inFromUser
Client
Process
process
input
stream
monitor
outToServer
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)
keyboard
inFromServer
Example client-server
application:
input
stream
client
TCP
clientSocket
socket
to network
TCP
socket
from network
ICT 6621 : Advanced Networking
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
Khaled Mahbub, IICT, BUET, 2008
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
ICT 6621 : Advanced Networking
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());
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
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();
}
}
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
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
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Create input
stream, attached
to socket
Khaled Mahbub, IICT, BUET, 2008
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
ICT 6621 : Advanced Networking
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);
}
}
}
Khaled Mahbub, IICT, BUET, 2008
End of while loop,
loop back and wait for
another client connection
ICT 6621 : Advanced Networking
Outline (Application Layer)
• Principles of network applications
• Web and HTTP
• FTP
• Electronic Mail
– SMTP, POP3, IMAP
• DNS
• Socket programming with TCP
• Socket programming with UDP
• Building a Web server
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Socket Programming with UDP
UDP: no “connection” between client and server
• no handshaking
• sender explicitly attaches IP address and port of
destination to each packet
• server must extract IP address, port of sender from
received packet
UDP: transmitted data may be received out of order, or lost
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
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 number
Khaled Mahbub, IICT, BUET, 2008
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read reply from
clientSocket
close
clientSocket
ICT 6621 : Advanced Networking
Example: Java Client (UDP)
input
stream
Client
process
monitor
inFromUser
keyboard
Process
Input: receives
packet (TCP
received “byte
stream”)
UDP
packet
receivePacket
packet (TCP sent
“byte stream”)
sendPacket
Output: sends
client
UDP
clientSocket
socket
to network
Khaled Mahbub, IICT, BUET, 2008
UDP
packet
UDP
socket
from network
ICT 6621 : Advanced Networking
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();
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
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();
}
}
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
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
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
Receive
datagram
Khaled Mahbub, IICT, BUET, 2008
serverSocket.receive(receivePacket);
ICT 6621 : Advanced Networking
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);
}
}
}
Khaled Mahbub, IICT, BUET, 2008
End of while loop,
loop back and wait for
another datagram
ICT 6621 : Advanced Networking
Outline (Application Layer)
• Principles of network applications
• Web and HTTP
• FTP
• Electronic Mail
– SMTP, POP3, IMAP
• DNS
• Socket programming with TCP
• Socket programming with UDP
• Building a Web server
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Building a Simple Web Server
•
•
•
•
•
handles one HTTP request
accepts the request
parses header
obtains requested file from server’s file system
creates HTTP response message:
– header lines + file
• sends response to client
• after creating server, you can request file using a
browser (e.g. IE explorer)
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Example: Web Server
import java.io.*;
import java.net.*;
import java.util.*;
class WebServer{
public static void main(String argv[]) throws Exception {
String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(6789);
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine = new
StringTokenizer(requestMessageLine);
if (tokenizedLine.nextToken().equals("GET")){
fileName = tokenizedLine.nextToken();
if (fileName.startsWith("/") == true )
fileName = fileName.substring(1);
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Example: Web Server
File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream (fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
if (fileName.endsWith(".jpg"))
outToClient.writeBytes("Content-Type: image/jpeg\r\n");
if (fileName.endsWith(".gif"))
outToClient.writeBytes("Content-Type: image/gif\r\n");
outToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else System.out.println("Bad Request Message");
}
}
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking
Reading Material
• Chapter 2 – text3 (Kurose)
• Chapter 27, 28 – text1 (Stevens)
Khaled Mahbub, IICT, BUET, 2008
ICT 6621 : Advanced Networking