360 likes | 495 Views
Lecture 10 TCP/IP Application Layer (2). 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. Internet hosts, routers:
E N D
Lecture 10TCP/IP Application Layer (2) Khaled Mahbub, IICT, BUET, 2008
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
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. DNS: Domain Name System Khaled Mahbub, IICT, BUET, 2008
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
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
a Verisign, Dulles, VA c Cogent, Herndon, VA (also Los Angeles) d U Maryland College Park, MD g US DoD Vienna, VA h ARL Aberdeen, MD j Verisign, ( 11 locations) k RIPE London (also Amsterdam, Frankfurt) i Autonomica, Stockholm (plus 3 other 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 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 13 root name servers worldwide Khaled Mahbub, IICT, BUET, 2008
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
Example • 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
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
DNS: distributed db storing resource records (RR) RR format: (name, value, type, ttl) DNS Records • 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=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) • Type=MX • value is name of mail server associated with name • e.g. (foo.com. mail.bar.foo.com, MX) Khaled Mahbub, IICT, BUET, 2008
DNS Protocol, Messages DNS protocol :queryand 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
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
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
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 socket 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: • unreliable datagram • reliable, byte stream-oriented Khaled Mahbub, IICT, BUET, 2008
controlled by application developer process process controlled by application developer TCP with buffers, variables TCP with buffers, variables controlled by operating system controlled by operating system internet host or server host or server socket socket Socket-Programming using TCP Socket: a door between application process and end-end-transport protocol (UCP or TCP) TCP service: reliable transfer of bytesfrom one process to another Khaled Mahbub, IICT, BUET, 2008
TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint Socket Programming with TCP • 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 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
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
Socket Programming with TCP Example client-server application: 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) Client process client TCP socket Khaled Mahbub, IICT, BUET, 2008
Server (running on hostid) Client create socket, connect to hostid, port=x create socket, port=x, for incoming request: clientSocket = Socket() welcomeSocket = ServerSocket() TCP connection setup wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to connectionSocket read reply from clientSocket close connectionSocket close clientSocket Client/Server Socket Interaction: TCP Khaled Mahbub, IICT, BUET, 2008
Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket Khaled Mahbub, IICT, BUET, 2008
Example: Java client (TCP) (cont..) Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } } Send line to server Read line from server Khaled Mahbub, IICT, BUET, 2008
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); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket Khaled Mahbub, IICT, BUET, 2008
Example: Java Server (TCP) (cont..) DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } } } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection Khaled Mahbub, IICT, BUET, 2008
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
UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server application viewpoint 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 Khaled Mahbub, IICT, BUET, 2008
Client create socket, port=x, for incoming request: serverSocket = DatagramSocket() create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read request from serverSocket write reply to serverSocket specifying client host address, port number read reply from clientSocket close clientSocket Client/Server Socket Interaction: UDP Server (running on hostid) Khaled Mahbub, IICT, BUET, 2008
Example: Java Client (UDP) Client process Input: receives packet (TCP received “byte stream”) Output: sends packet (TCP sent “byte stream”) client UDP socket Khaled Mahbub, IICT, BUET, 2008
Example: Java Client (UDP) import java.io.*; import java.net.*; 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(); Create input stream Create client socket Translate hostname to IP address using DNS Khaled Mahbub, IICT, BUET, 2008
Example: Java Client (UDP), cont. Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } Send datagram to server Read datagram from server Khaled Mahbub, IICT, BUET, 2008
Example: Java Server (UDP) import java.io.*; import java.net.*; 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) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram Khaled Mahbub, IICT, BUET, 2008
Example: Java Server (UDP), cont String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram Khaled Mahbub, IICT, BUET, 2008
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
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
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
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
Reading Material • Chapter 2 – text3 (Kurose) • Chapter 27, 28 – text1 (Stevens) Khaled Mahbub, IICT, BUET, 2008