1、2: Application Layer,1,DNS: Domain Name System,People: many identifiers: SSN, name, Passport # Internet hosts, routers: IP address (32 bit) - used for addressing datagrams “name”, e.g., gaia.cs.umass.edu - used by humans Q: map between IP addresses and name ?,Domain Name System: distributed database
2、 implemented in hierarchy of many name servers application-layer protocol host, routers, name servers to communicate to resolve names (address/name translation) note: core Internet function implemented as application-layer protocol complexity at networks “edge”,2: Application Layer,2,DNS name server
3、s,no server has all name-to-IP address mappings local name servers: each ISP, company has local (default) name server host DNS query first goes to local name server authoritative name server: for a host: stores that hosts IP address, name can perform name/address translation for that hosts name,Why
4、not centralize DNS? single point of failure traffic volume distant centralized database maintenancedoesnt scale!,2: Application Layer,3,DNS: Root name servers,contacted by local name server that can not resolve name root name server: contacts authoritative name server if name mapping not known gets
5、mapping returns mapping to local name server dozen root name servers worldwide,2: Application Layer,4,Simple DNS example,host surf.eurecom.fr wants IP address of gaia.cs.umass.edu 1. Contacts its local DNS server, dns.eurecom.fr 2. dns.eurecom.fr contacts root name server, if necessary 3. root name
6、server contacts authoritative name server, dns.umass.edu, if necessary,requesting host surf.eurecom.fr,gaia.cs.umass.edu,root name server,authorititive name server dns.umass.edu,1,2,3,4,5,6,2: Application Layer,5,DNS example,Root name server: may not know authoratiative name server may know intermed
7、iate name server: who to contact to find authoritative name server,requesting host surf.eurecom.fr,gaia.cs.umass.edu,root name server,1,2,3,4,5,6,authoritative name server dns.cs.umass.edu,7,8,2: Application Layer,6,DNS: iterated queries,recursive query: puts burden of name resolution on contacted n
8、ame server heavy load? iterated query: contacted server replies with name of server to contact “I dont know this name, but ask this server”,requesting host surf.eurecom.fr,gaia.cs.umass.edu,root name server,1,2,3,4,5,6,authoritative name server dns.cs.umass.edu,7,8,iterated query,2: Application Laye
9、r,7,DNS: caching and updating records,once (any) name server learns mapping, it caches mapping cache entries timeout (disappear) after some time update/notify mechanisms under design by IETF RFC 2136 http:/www.ietf.org/html.charters/dnsind-charter.html,2: Application Layer,8,DNS records,DNS: distrib
10、uted db storing resource records (RR),Type=NS name is domain (e.g. ) value is IP address of authoritative name server for this domain,Type=A name is hostname value is IP address,Type=CNAME name is an alias name for some “cannonical” (the real) name value is cannonical name,Type=MX value is hostname
11、of mailserver associated with name,2: Application Layer,9,DNS protocol, messages,DNS protocol : query and repy messages, both with same message format,msg header identification: 16 bit # for query, repy to query uses same # flags: query or reply recursion desired recursion available reply is authori
12、tative,2: Application Layer,10,DNS protocol, messages,Name, type fieldsfor a query,RRs in reponse to query,records for authoritative servers,additional “helpful” info that may be used,2: Application Layer,11,Socket programming,Socket API introduced in BSD4.1 UNIX, 1981 Now Industry standard Availabl
13、e on many operating systems explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented,Goal: learn how to build client/server application that communicate using sockets,2: Application Layer,12,Wh
14、at is “socket”?,A socket is a virtual connection between two applications Using a socket, two processes can communicate with each other The socket is the major communication tool for Internet applications A socket is bi-directional (full-duplex) transmission A socket can be created dynamically,2: Ap
15、plication Layer,13,Network,Socket as a virtual connection between two processes,(physical connection),Host B,Process 2,Host A,Process 1,Information Hiding,2: Application Layer,14,Socket as a client/server model,2: Application Layer,15,Socket,OS abstraction (not hardware) Created dynamically Persists
16、 only while application runs Referenced by a descriptor,2: Application Layer,16,Descriptor,Small integer One per active socket Used in all operations on socket Generated by OS when socket created Only meaningful to application that owns socket In UNIX, integrated with file descriptors,2: Application
17、 Layer,17,Creating a Socket,Application calls socket functionOS returns descriptor for socket Descriptor valid until application closes socket or exits Common: protofamily = PF_INET, type = SOCK_STREAM,SOCK_DGRAM or SOCK_RAW,desc = socket(protofamily,type,proto);,2: Application Layer,18,Socket Funct
18、ionality,Socket completely general Can be used By client By server With a CO transport protocol With a CL transport protocol To send data, receive data, or both Large set of operations,2: Application Layer,19,Socket Operations,Close Terminate use of socket Permanent,close(socket);,2: Application Lay
19、er,20,Socket Operations,Bind Specify protocol port for a socket Specify local IP address for a socket Can use INADDR_ANY for any IP address,bind(socket,localaddr,addrlen);,2: Application Layer,21,Socket Operations (continued),Listen Used by server Prepares socket to accept incoming connections,liste
20、n(socket,queuesize);,newsock = accept(socket,caddr,caddrlen);,Accept Used by server Waits for next connection and returns new socket,2: Application Layer,22,Socket Operations (continued),Connect Used by client Either Performs a TCP connection Fully specifies addresses for UDP,connect(socket,saddr,sa
21、ddrlen);,2: Application Layer,23,Socket Operations (continued),Send, sendto, and sndmsg Transfer outgoing data from application,send(socket,data,length,flags);,sendmsg(socket,msgstruct,flags);,sendto(socket,data,length,flags,destaddr,addrlen);,2: Application Layer,24,Format of msgstruct,struct msgst
22、ruct struct sockaddr *m_saddr; /*dest address*/struct datavec *m_dvec; /*message (vector)*/int mdvlength; /*size of vector*/struct access *m_rights; /*access rights*/int m_alength; /*size of access rights*/ ,2: Application Layer,25,Socket Operations (continued),Recv, recvfrom, and recvmsg Transfer i
23、ncoming data to application,recv(socket,buffer,length,flags);,recvmsg(socket,msgstruct,flags);,recvfrom(socket,buffer,length,flags,senderaddr,saddrlen);,2: Application Layer,26,Socket Operations (continued),Many additional functions Supply support and utility services Some implemented as library cal
24、ls,2: Application Layer,27,Examples of Socket Support Functions,Gethostbyname Maps domain name to IP address Example of argument book.cs.purdue.edu Getprotobyname Maps name of protocol to internal number Argument usually “tcp” or “udp”,2: Application Layer,28,SERVER,bind(),listen(),accept(),read(),w
25、rite(),close(),CLIENT,socket(),connect(),write(),close(),socket(),read(),Socket Programming Tutorial(1),2: Application Layer,29,Functions and parameter format (for server side)(1) create socket:socket_id = socket (AF_INET, SOCK_STREM, DEFAULT_PROTOCOL); (2) bind socket:bind (socket_id, server_addr,
26、server_len); (3) listen to socket:listen (socket_id, number_of_connection); (4) accept a connection:accept (socket_id, ,Socket Programming Tutorial(2),2: Application Layer,30,Functions and parameter format (for client side)(1) create socket: same as serversocket_id = socket (AF_INET, SOCK_STREM, DEF
27、AULT_PROTOCOL);(2) connect socket:connect (socket_id, serverINETaddress, server_len);(3) write (send) data:write (socket_id, buffer, buffer_len);(4) read (receive) data:read (socket_id, buffer, buffer_len); (5) close socket: same as serverclose(socket_id);,Socket Programming Tutorial(3),2: Applicati
28、on Layer,31,“*” indicates a blocking function call.,SERVER,bind(),listen(),accept(),read(),CLIENT,socket(),connect(),write(),socket(),*,*,*,1: Connection Request,2. Send a command,3. Receive the result,read(),We are not doing this.,*,Socket Programming Tutorial(4),2: Application Layer,32,Step 1: soc
29、ket(_) call,It declares a socket to be used.,After socket(_) call:,Socket Programming Tutorial(5),2: Application Layer,33,Step 2: bind(_) call,It connects a process to a specific port,After bind(_) call:,Server,Port Numbers:,01023: System ReservedPort 21: FTPPort 23: telnetPort 80: HTTP,1024 and abo
30、ve: available to users,Port,Socket Programming Tutorial(6),2: Application Layer,34,Step 3: listen(_) call,After listen(_) call:,6500,We need to specify how manyconnection requests should be held in the buffer when SERVERis busy (cant accept a request).,listen (socket_id, number_of_connection);,Serve
31、r,Socket Programming Tutorial(7),2: Application Layer,35,Step 4 - Part 1: accept(_) call,The server process accepts a request from a client,After accept(_) call:,Server,6500,Socket Programming Tutorial(8),2: Application Layer,36,Step 4 - Part 2: accept(_) call,The accept(_) call returns another port
32、 number and establish another connection,Client,7100,6500,Socket Programming Tutorial(9),2: Application Layer,37,Step 5: read(_) and write() call,Client,7100,The server and client communicate using the second socket,6500,Socket Programming Tutorial(10),2: Application Layer,38,Step 6: close (_) call,
33、Client,7100,Close the second socket and leave the first socket for next client,6500,Socket Programming Tutorial(11),2: Application Layer,39,Step 7: Go back to accept (_) call,The server process goes back to the accept call,6500,Socket Programming Tutorial(12),2: Application Layer,40,Socket-programmi
34、ng 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,host or server,controlled by application developer,controlled by
35、operating system,host or server,internet,2: Application Layer,41,Socket programming with TCP,Client must contact server server process must first be running server must have created socket (door) that welcomes clients contact Client contacts server by: creating client-local TCP socket specifying IP
36、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,2: Application Layer,42,Socket programm
37、ing with TCP,Example client-server app: client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream) server reads line from socket server converts line to uppercase, sends back to client client reads, prints modified line from socket (inFromServer strea
38、m),Input stream: sequence of bytes into process Output stream: sequence of bytes out of process,2: Application Layer,43,Client/server socket interaction: TCP,Server (running on hostid),Client,2: Application Layer,44,Example: Java client (TCP),import java.io.*; import .*; class TCPClient public stati
39、c 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();
40、,Create input stream,Create client socket, connect to server,Create output stream attached to socket,2: Application Layer,45,Example: Java client (TCP), cont.,BufferedReader inFromServer = new BufferedReader(newInputStreamReader(clientSocket.getInputStream(); sentence = inFromUser.readLine(); outToS
41、erver.writeBytes(sentence + n); modifiedSentence = inFromServer.readLine(); System.out.println(“FROM SERVER: “ + modifiedSentence); clientSocket.close(); ,Create input stream attached to socket,Send line to server,Read line from server,2: Application Layer,46,Example: Java server (TCP),import java.i
42、o.*; import .*; 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
43、(newInputStreamReader(connectionSocket.getInputStream();,Create welcoming socket at port 6789,Wait, on welcoming socket for contact by client,Create input stream, attached to socket,2: Application Layer,47,Example: Java server (TCP), cont,DataOutputStream outToClient = new DataOutputStream(connectio
44、nSocket.getOutputStream(); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + n; outToClient.writeBytes(capitalizedSentence); ,Read in line from socket,Create output stream, attached to socket,Write out line to socket,End of while loop, loop back and wait
45、for another client connection,2: Application Layer,48,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 UDP: transmitted data ma
46、y be received out of order, or lost,2: Application Layer,49,Client/server socket interaction: UDP,Server (running on hostid),2: Application Layer,50,Example: Java client (UDP),import java.io.*; import .*; class UDPClient public static void main(String args) throws Exception BufferedReader inFromUser
47、 = new BufferedReader(new InputStreamReader(System.in); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName(“hostname“); byte sendData = new byte1024; byte receiveData = new byte1024; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(
48、);,Create input stream,Create client socket,Translatehostname to IP address using DNS,2: Application Layer,51,Example: Java client (UDP), cont.,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(); ,Create datagram with data-to-send, length, IP addr, port,