1 / 125

Client/Server Distributed Systems

Client/Server Distributed Systems. 240-322, Semester 1, 2005-2006. Objectives describe basic networking in Java Web page retrieval and sockets programming. 11. Java Networking. Contents. 1. Networking Restrictions in Java 2. Basic Networking Classes 3. Sockets (Java Style)

Ava
Download Presentation

Client/Server Distributed Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Client/Server Distributed Systems 240-322, Semester 1, 2005-2006 • Objectives • describe basic networking in Java • Web page retrieval and sockets programming 11. Java Networking

  2. Contents 1. Networking Restrictions in Java 2. Basic Networking Classes 3. Sockets (Java Style) 4. Retrieving a Web Page • 4 different approaches 5. A Stream Socket Client/Server continued

  3. 6. A Datagram Socket Client/Server 7. Networked Tic-Tac-Toe 8. More Information

  4. 1. Networking Restrictions in Java • Many browsers place networking restrictions on applets • usually communication is limited to being between your machine and the one where the applet came from • There are no restrictions on Java applications or when using appletviewer

  5. Relaxing Security • Applets can be signed with trusted certificates • a browser can be configured to relax security depending on an applet’s signature • an advanced topic

  6. 2. Basic Networking Classes • The J2SE documentation for java.net lists over 30 classes. A few of the key ones: • InetAddress • the class that represents IP addresses and contains operations for manipulating them • see section 2.1 continued

  7. URL • used to retrieve the Web page at the given URL • URLConnection • also used to retrieve a Web page • allows extra parameters to be sent to the URL • e.g HTTP request headers continued

  8. Socket • the client-side socket class for TCP • ServerSocket • the server-side socket class for TCP • DatagramSocket • allows a client or server to send/receive UDP packets

  9. 2.1. Finding an IP Address • Java’s InetAddress class makes the mapping between hostnames and IP addresses much easier than in UNIX. • For details, look at the the documentation for java.net.InetAddress

  10. WhatIP.java import java.io.*;import java.net.*;public class WhatIP {public static void main(String args[]) throws IOException { InetAddress addr = InetAddress.getByName(args[0]);System.out.println("Inet address is "+ addr);} }

  11. Use Carried out from a DOS prompt on my Windows machine.

  12. 3. Sockets (Java Style) • 1. Stream Sockets • the client/server connection exists for the entire duration of the request and answer • similar to a telephone call • a connection-oriented service • corresponds to TCP • Java has separate classes for client and server stream sockets (see section 5)

  13. 2. Datagram Sockets • the client/server send messages (packets, datagrams) to each other • similar to the postal service • a connectionless service • corresponds to UDP • Java has classes for datagram sockets and packets (see section 6).

  14. 3.1. Pinging ping is a DOS command, not Java. • ping uses the ICMP protocol's ECHO_REQUEST datagram. The host’s answer is an ICMP ECHO_RESPONSE.

  15. ICMP packets can only be created via a socket of type SOCK_RAW. • but Java only supports SOCK_STREAM (TCP) and SOCK_DGRAM (UDP) sockets • Our solution: use a TCP connection to the daytime service to pretend to ping • the daytime server listens at port 13

  16. 3.2. DayPing.java JJ2, p.677 import java.io.*;import java.net.*;public class DayPing { public static void main(String args[]) throws Exception{if (args.length != 1) {System.out.println( "Usage: java DayPing <machine name>");System.exit(0); } :

  17. String machine = args[0];Socket so = new Socket(machine, 13); // daytime server listens at port 13BufferedReader br =new BufferedReader( new InputStreamReader( so.getInputStream() ) ); System.out.println( machine + " is alive at " + br.readLine());so.close(); } // end of main()} // end of DayPing class

  18. Notes • DayPing.java converts the socket connection into a BufferedReader • this allows readLine() to be used for socket input • Using layered streams to add I/O functionality on top of sockets is a powerful feature of Java.

  19. Use All the examples were tested on my Windows machine • C> javac DayPing.java • C> java DayPing calvincalvin is alive at Sat May 7 13:46:06 2005 • C> java DayPing fourdotsException in thread "main" java.net.ConnectException: Connection refused: no further information at java.net.PlainSocketImpl.socketConnect (Native Method) at java.net.PlainSocketImpl.doConnect... : at ping.main(DayPing.java:23) continued

  20. After a wait • C> java DayPing takasillaException in thread "main" java.net.UnknownHostException: takasilla at java.net.PlainSocketImpl. connect(Unknown Source) : at ping.main(DayPing.java:23) • C> java DayPing www.amazon.comException in thread "main" java.net.NoRouteToHostException: Operation timed out: connect at java.net.PlainSocketImpl. socketConnect(Native Method) : at ping.main(DayPing.java:23) After a long wait

  21. 3.3. Why Doesn't DayPing Work? • The examples that don't work show three different errors: • ConnectException • UnknownHostException • NoRouteToHostException • The "unknown host" exception is caused by an incorrect IP address. continued

  22. The "connection" exception probably means that the server is unavailable (switched off) • this can be confirmed for a TCP service by trying to connect to it with telnet: $ telnet fourdots 13Trying 172.30.255.4...telnet: Unable to connect to remote host: Connection refused$ executed from a different machine (fivedots) continued

  23. The "no route to host" exception usually means that there is a firewall preventing the connection from being made. • telnet response: $ telnet www.amazon.com 13Trying 207.171.163.90...telnet: Unable to connect to remote host: Connection timed out$ A long wait, and then... tried on (fivedots)

  24. 3.4. Firewalls at PSU (simplified) University ratree Departmental firewall (no external socket creation; URLs allowed) The Internet Univ. firewall (no external socket creation; URLs only throughthe ‘cache’ proxy) calvin takasila fivedots cache 8080 CoE Department

  25. Ordinary users (i.e. students) cannot write socket-based programs that communicate outside PSU • this is true in any language (Java, C, etc.) • But programs can retrieve Web pages (and other things) using the Java URL class • the URL request must go through PSU's cache machine

  26. URLs and Sockets • From your data communications course, you may recall that the Web protocol, HTTP, is built on top of TCP/IP. • Java's URL class uses stream sockets (TCP/IP) for its implementation • so why does it get past cache? continued

  27. cache accepts socket links to valid Web servers, but also checks that the HTTP GET (or POST) message includes additional valid HTTP headers. • all of this HTTP stuff is carried out by the URL class for us

  28. 4. Retrieving a Web Page • Five ways of obtaining a Web page: • 1. use a socket, and send a GET message to the server • see GetSocketPage.java • 2. use a URL object, and read the page via a stream • see GetURLPage.java continued

  29. 3. use a URL object, and display the page in a browser • see showPage.java • 4. display a URL in an JEditorPane • see ShowURLPage.java • 5. use a HTTP URL connection, and send a GET message • see Java’s networking tutorial

  30. 4.1. Sockets and GET • GetSocketPage.java retrieves the page: http://<host name>/index.html e.g.http://fivedots.coe.psu.ac.th/index.html • It prints the text of the page to stdout. continued

  31. It opens a socket at port 80 for the host, which is the usually place where the Web server is listening. • It sends the HTTP GET message: GET /index.html

  32. Diagram GET /index.html 80 Web server Web page (as text) GetSocketPage client host

  33. GetSocketPage.java import java.io.*;import java.net.*;public class GetSocketPage {public static void main(String args[]) throws IOException {Socket sock = new Socket(args[0],80); :

  34. BufferedReader dis = new BufferedReader( new InputStreamReader( sock.getInputStream() ));PrintStream ps = new PrintStream( sock.getOutputStream() );ps.println("GET /index.html");String line;while ((line = dis.readLine()) != null)System.out.println(line);sock.close();}} // end of GetSocketPage.java

  35. Notes • GetSocketPage.java converts the socket connection into a BufferedReader for input, and a PrintStream for output • uses readLine()for socket input • uses println() for socket output

  36. Use C> javac GetSocketPage.java C> java GetSocketPage fivedots<html><head><title>Un title page</title></head><meta http-equiv="Content-Type" content="text/html; charset=windows-874"><style type="text/css"> :C> text of Web page printed to stdout

  37. But... C> java GetSocketPage www.amazon.comException in thread "main" java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl. socketConnect(Native Method) :at GetSocketPage.main(GetSocketPage.java:18) The firewall around PSU prevents Web server access by sockets. continued

  38. cache rejected the GET message to the external site since the message didn't include additional HTTP headers. • These can be supplied by us, but it's easier to use the URL class.

  39. 4.2. URL Object • GetURLPage.java avoids the firewall problem with sockets by using a URL object. • A URL object allows a Web page to be retrieved as a stream of text • our program prints the text to stdout.

  40. GetURLPage.java import java.net.*;import java.io.*;public class GetURLPage {public static void main(String args[]) {try {URL url = new URL(args[0]);BufferedReader dis = new BufferedReader(new InputStreamReader( url.openStream() )); :

  41. String line;while ( (line=dis.readLine())!=null )System.out.println(line);dis.close();} catch (Exception e) { System.out.println(e); } }} // end of GetURLPage.java

  42. Notes • A stream for the URL object is obtained using openStream() • after that the same input stream layering technique is used as in GetSocketPage.java • there is no need to send a GET message

  43. Use after a long wait • C> javac GetURLPage.java • C> java GetURLPage http://www.amazon.co.ukjava.net.ConnectException: Connection timed out: connect • C> java -DproxySet=true -DproxyHost=cache.psu.ac.th -DproxyPort=8080 GetURLPage http://www.amazon.com/<html><head><title>Amazon Page</title></head><body bgcolor=#ffffff test=#000000><H1> :C> typed on one line

  44. Ordinary users can access outside PSU by using URLs, but they must route their requests via PSU cache machine.

  45. Batch Files for Long Commands • GetURLPage.bat contains: @echo offecho Executing GetURLPage...java -DproxySet=true -DproxyHost=cache.psu.ac.th -DproxyPort=8080 GetURLPage %1 • Use: c> GetURLPage http://www.amazon.co.uk : // same output as last slide

  46. Proxy Username/Password • A further level of firewall security is to require the user to enter a username and password • called proxy authorization • Java has network support for authorization • it allows a username and password to be sent by a client program to the firewall continued

  47. A good tutorial: • “Jump a Proxy/Firewall and Live to Tell About it”http://www.devx.com/upload/free/features/javapro/2000/03mar00/te0003/te0003.asp • Slightly modified code is in GetThroughProxy.javaat: http://fivedots.coe.psu.ac.th/ Software.coe/Cliserv/ Code%20Examples/Java%20Code/ Basic%20Networking/

  48. 4.3. Passing a URL to a Browser • If the Java code is in an applet, then the downloaded URL can be displayed in the browser. • showPage.html displays a user-specified Web page in the (Opera) browser, using the ShowPage.java applet.

  49. Usage The dialog box appears in front of the browser window. continued

  50. Loaded Page:

More Related