Presentation is loading. Please wait.

Presentation is loading. Please wait.

9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003.

Similar presentations


Presentation on theme: "9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003."— Presentation transcript:

1 9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003

2 9/23/2003-9/25/2003 Assignments Turn in Lab 1 Project 1 –Get started ASAP Finish reading chapter 2 Start on chapter 3 for next week

3 9/23/2003-9/25/2003 Socket Programming How do you create a network application? –in Java –http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/ TCP or UDP process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet

4 9/23/2003-9/25/2003 Socket Programming with TCP Client/Server 1.Server starts 2.Server creates a socket 3.Client starts 4.Client creates a socket (what must be specified?) and initiates TCP connection 5. Server creates a new socket for the connection

5 9/23/2003-9/25/2003 Stream Jargon Stream – a sequence of characters that flow into or out of a process. Input Stream – attached to some input source for the process –keyboard, socket, ??? Output Stream – attached to an output source –monitor, socket, ???

6 9/23/2003-9/25/2003 TCP Client Architecture 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) Client process client TCP socket

7 9/23/2003-9/25/2003 TCP Client/Server Interaction wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup

8 9/23/2003-9/25/2003 TCPClient.java 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

9 9/23/2003-9/25/2003 TCPClient.java 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(); } Create input stream attached to socket Send line to server Read line from server

10 9/23/2003-9/25/2003 TCPServer.java 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

11 9/23/2003-9/25/2003 TCPServer.java DataOutputStream outToClient = new DataOutputStream (connectionSocket.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 for another client connection

12 9/23/2003-9/25/2003 Java API http://java.sun.com/j2se/1.4.2/docs/api/

13 9/23/2003-9/25/2003 TCP Client/Server Interaction wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup

14 9/23/2003-9/25/2003 Socket programming with 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 Transmitted data may be received out of order, or lost

15 9/23/2003-9/25/2003 UDP Client/Server Interaction close clientSocket Server (running on hostid ) read reply from clientSocket create socket, clientSocket = DatagramSocket() Client Create, address ( hostid, port=x, send datagram request using clientSocket create socket, port= x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port number

16 9/23/2003-9/25/2003 UDP Client Architecture Output: sends packet (TCP sent “byte stream”) Input: receives packet (TCP received “byte stream”) Client process client UDP socket

17 9/23/2003-9/25/2003 UDPClient.java 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

18 9/23/2003-9/25/2003 UDPClient.java 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 Send datagram to server Read datagram from server

19 9/23/2003-9/25/2003 UDPServer.java 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

20 9/23/2003-9/25/2003 UDPServer.java 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 Write out datagram to socket End of while loop, loop back and wait for another datagram Create datagram to send to client

21 9/23/2003-9/25/2003 Building a Simple Web Server http://www.mtholyoke.edu/courses/srollins/cs325/web/code/WebServer.java 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

22 9/23/2003-9/25/2003 Exceptions What happens when errors occur? Java throws exceptions – but it’s up to you to deal with them http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

23 9/23/2003-9/25/2003 Threads We would like to be able to service more than one request at a time Create a new thread for each request received http://java.sun.com/docs/books/tutorial/essential/threads/index.html

24 9/23/2003-9/25/2003 Assignments Turn in Homework 2 Read chapter 3 – sections 3.1-3.4 for next week Get started on the review questions

25 9/23/2003-9/25/2003 DNS: Domain Name System How do I identify a computer? Hostname –ID for a computer –Examples? IP Address –Why is an IP needed? –Examples? Mapping from hostname to IP address?

26 9/23/2003-9/25/2003 DNS: Domain Name System Name servers –Store hostname to IP mapping –Organized in a hierarchy –Act as a distributed database Application-layer protocol –Defines communication between hosts and name servers

27 9/23/2003-9/25/2003 Usage Scenario Which apps use DNS? HTTP –Browser extracts hostname –Sends hostname to DNS –DNS does lookup and returns IP address –Browser sends HTTP GET to IP address

28 9/23/2003-9/25/2003 Name Servers Why not have one master name server? –single point of failure –traffic volume –distant centralized database –maintenance 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 host’s IP address, name –can perform name/address translation for that host’s name

29 9/23/2003-9/25/2003 b USC-ISI Marina del Rey, CA l ICANN Marina del Rey, CA e NASA Mt View, CA f Internet Software C. Palo Alto, CA i NORDUnet Stockholm k RIPE London m WIDE Tokyo a NSI Herndon, VA c PSInet Herndon, VA d U Maryland College Park, MD g DISA Vienna, VA h ARL Aberdeen, MD j NSI (TBD) Herndon, VA Root Name Servers contacted by local name server that can not resolve name contacts authoritative name server if name mapping not known gets mapping returns mapping to local name server 13 root name servers worldwide

30 9/23/2003-9/25/2003 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 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 local name server dns.eurecom.fr 1 2 3 4 5 6

31 9/23/2003-9/25/2003 DNS Example Root name server may not know authoritative name server May know intermediate name server who to contact to find authoritative name server requesting host surf.eurecom.fr gaia.cs.umass.edu root name server local name server dns.eurecom.fr 1 2 3 4 5 6 authoritative name server dns.cs.umass.edu intermediate name server dns.umass.edu 7 8

32 9/23/2003-9/25/2003 Iterated Queries Recursive Query –puts burden of name resolution on contacted name server –heavy load? Iterated Query –contacted server replies with name of server to contact –“I don’t know this name, but ask this server” requesting host surf.eurecom.fr gaia.cs.umass.edu root name server local name server dns.eurecom.fr 1 2 3 4 5 6 authoritative name server dns.cs.umass.edu intermediate name server dns.umass.edu 7 8 iterated query

33 9/23/2003-9/25/2003 Caching and Updating Once (any) name server learns mapping, it caches mapping –cache entries timeout (disappear) after some time Why is a timeout period needed? UPDATE –Use DNS messages to update entries –Compare to static updates?

34 9/23/2003-9/25/2003 DNS Records Type=A –name is hostname –value is IP address Type=NS –name is domain (e.g. foo.com) –value is IP address of authoritative name server for this domain Type=CNAME –name is alias name for some “canonical” (the real) name www.ibm.com is really servereast.backup2.ibm.com –value is canonical name Type=MX –value is name of mailserver associated with name RR format: (name, value, type,ttl)

35 9/23/2003-9/25/2003 Java Review/Project 1 Questions about Java and/or Project 1?


Download ppt "9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003."

Similar presentations


Ads by Google