Networks & Distributed Systems ITEC 1630 • Computers are usually hooked up to a Week 11: Networking local area network (LAN) and/or the Internet • Many distributed applications run on Yves Lespérance networked computers Readings: Horstmann Ch. 24 • Computers on a network use protocols to communicate, e.g. IP, AppleTalk March 2007 ITEC 1630 - Yves Lesperance 1 March 2007 ITEC 1630 - Yves Lesperance 2 TCP/IP IP addresses • Internet Protocol (IP) is used to deliver • IP uses internet addresses, e.g. data across the Internet 64.233.167.147 • Domain names are more convenient, e.g. • In IP, data is broken up into smaller www.google.ca packets that may be sent along different • Domain Naming Service (DNS) translates routes between the two • Transmission Control Protocol (TCP) • IP also uses port numbers (short), e.g. 80 for runs on top of IP and ensures reliable web servers transmission • Telnet can be used to open a connection and display data sent & received March 2007 ITEC 1630 - Yves Lesperance 3 March 2007 ITEC 1630 - Yves Lesperance 4 1
Application Level Protocols HTTP • Application level protocols run on top of • Hypertext Transfer Protocol (HTTP) is TCP/IP and support communication for used by web browsers and servers specific applications • HTTP uses Uniform Resource Locators • E.g. Post Office Protocol (POP) to (URL) to identify web server hosts and retrieve email files on them, e.g. http://www.cse.yorku.ca/~lesperan/cours • E.g. Simple Mail Transfer Protocol es/ITEC1630/index.html (SMTP) to send email March 2007 ITEC 1630 - Yves Lesperance 5 March 2007 ITEC 1630 - Yves Lesperance 6 HTTP Commands Clients & Servers • GET file HTTP/1.0 retrieves file • A client is a program that connects to a server program over the net and uses • PUT stores item on server TCP/IP to communicate • DELETE removes item from server • A server is a program that accepts • POST sends input to server side connections from clients command and gets results • Each uses a socket to send and receive • HEAD , OPTIONS , TRACE , etc. data March 2007 ITEC 1630 - Yves Lesperance 7 March 2007 ITEC 1630 - Yves Lesperance 8 2
Using Sockets in a Client Sending & Receiving Text • To read from socket: • To create a socket: Socket s = new Socket( hostname, port ); Scanner in = new Scanner(ins); • To obtain its input and output streams: String input = in.nextLine(); • To send a command through socket: InputStream ins = s.getInputStream(); OutputStream ous = s.getOutputStream(); PrintWriter out = new PrintWriter(ous); • To close connection: s.close(); out.print( command ); out.flush(); • E.g. WebGet, BankClient March 2007 ITEC 1630 - Yves Lesperance 9 March 2007 ITEC 1630 - Yves Lesperance 10 Designing a Server Handling Clients • Decide on a protocol for clients to • Usually, want to spawn new thread to interact with the server handle each client who connects, e.g. • To listen for incoming connections, while(true) create a server socket: ServerSocket { Socket s = serv.accept(); Runnable run = new HandleClient(s); serv = new ServerSocket( port ); Thread t = new Thread(run); • To wait for connection and get socket: t.start(); Socket s = serv.accept(); } • Then obtain I/O streams and • When done, kill server send/receive data as seen before • E.g. BankServer March 2007 ITEC 1630 - Yves Lesperance 11 March 2007 ITEC 1630 - Yves Lesperance 12 3
URL Connections URL Connections • URLConnection class provides a more • Can also set request properties & convenient way to interact with web retrieve response parameters servers • To retrieve only if modified since: • To open connection: c.setIfModifiedSince( date ); InputStream ins = c.getInputStream(); URL u = new URL(“http:www.itec.yorku.ca”); URLConnection c = u.openConnection(); InputStream ins = c.getInputStream(); • Then create a Scanner and read page March 2007 ITEC 1630 - Yves Lesperance 13 March 2007 ITEC 1630 - Yves Lesperance 14 URL Connections • To get response code and message: HttpURLConnection hc = (HttpURLConnection) c; int code = hc.getResponseCode(); String msg = hc.getResponseMessage(); • To get content length and type: InputStream ins = c.getInputStream(); int length = c.getContentLength(); String type = c.getContentType(); • E.g. URLGet March 2007 ITEC 1630 - Yves Lesperance 15 4
Recommend
More recommend