CmpE 473 Internet Programming Pınar Yolum [email protected] Department of Computer Engineering Boğaziçi University Chapter 2 Java Networking 1 Java 2 Platforms • J2SE 1.5 – Core (JDBC, RMI, JNDI, …) – Desktop (Swing, JavaBeans) • J2EE 1.4 (Enterprise Edition) – Web Services Support – Java Message Service • J2ME (Micro Edition) – Wireless Messaging API – Mobile Media API Spring 2005— Pınar Yolum 3 Java Development Environments • Eclipse – Free, open-source IDE – Many additional plugins • Java NetBeans – Free, open-source IDE – Has mobility support • Borland JBuilder Spring 2005— Pınar Yolum 4 2 Java Programs • Applications – Stand-alone or distributed program – Bytecode on the running computer • Applets – Runs in a browser – Bytecode downloaded to the computer and run • Servlets – Runs in a browser – Bytecode resides on the server Spring 2005— Pınar Yolum 5 Java Basics • Exceptions – Throws – Try/Catch • Interfaces – Implements • Classes – Extends Spring 2005— Pınar Yolum 6 3 URL (1) • Uniform Resource Locator (URL) – – – – – • Specifies how to find a resource ProtocolName://ResourceName http://www.cmpe.boun.edu.tr/index.html file:///C:/tmp/mem-rdf.rdf Other protocols? General parts of a Resource Name – – – – Host name: www.cmpe.boun.edu.tr File name: index.html (last / means index.html) Port number (optional, assumed 80 for http) Reference to a html anchor (optional) 7 Spring 2005— Pınar Yolum URL (2) • Absolute URLs give the entire address – http://www.cmpe.boun.edu.tr • Relative URLs give part of the address relative to another URL – Used mostly in HTML pages – <a href=“publications.html">Publications</a> – It searches for publications.html in the directory of the file that contains the statement Spring 2005— Pınar Yolum 8 4 Java Networking • • • Write applications Java deals with TCP, UDP, etc. Useful classes (that use TCP) – – – – • URL URLConnection Socket ServerSocket Useful classes (that use UDP) – – – DatagramPacket DatagramSocket MulticastSocket 9 Spring 2005— Pınar Yolum Java URL (1) • Create a URL object from the absolute URL string – – • Create a URL object from the relative URL string – – – • URL(String absoluteString) URL cmpe = new URL("http://www.cmpe.boun.edu.tr/~pyolum/") URL(URL baseURL, String relativeAddress) URL pubs= new URL(cmpe, “publications.html”) URL thirdSection= new URL(pubs, “#THIRD) Create a URL from its parts – URL p = new URL("http", "www.cmpe.boun.edu.tr", 80, "publications.html"); Spring 2005— Pınar Yolum 10 5 Java URL (2) • Java URLs are write-once – You can’t modify its properties • • • • Compare two URLs for equality getPort, getHost, … Throws MalformedURLException Once you have a URL, open a connection to retrieve the contents (HTML commands) – java.io.InputStream URL.openStream() 11 Spring 2005— Pınar Yolum Reading from a URL import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL home = new URL("http://www.cmpe.boun.edu.tr/~pyolum"); BufferedReader in = new BufferedReader( new InputStreamReader( home.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } } Spring 2005— Pınar Yolum 12 6 Sample Output <!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9"> <!--windows-1254 --> <title>pInar Yolum</title> </head> <body text="#000000" link="#0000ff" vlink="#000080" bgcolor="#ffffcc“ alink="#ff0000"> <br <p><font face="Arial" size="+2"><b>P²nar Yolum</b></font></p> <br> <br>á <br>á <br> <ul> <li> <p><font face="Arial" size="+1"><a href="education.html">Education</a></font> <li> <p><font face="Arial" size="+1"><a href="publications.html">Publications</a></font> <li> <p><font face="Arial" size="+1"><a href="teaching.html">Teaching</a></font> <li> <p><font face="Arial" size="+1"><a href="professional.html">Professional Activities</a></font> <li> <p><font face="Arial" size="+1"><a href="491-projects.html">491/492 Projects</a></font> Spring 2005— Pınar Yolum 13 Connecting to a URL • Set up a connection to read and write to a URL try { URL home = new URL("http://www.cmpe.boun.edu.tr/~pyolum"); URLConnection homeConnection = home.openConnection(); } catch (MalformedURLException e) { // new URL() failed System.out.println(“No such URL”); } catch (IOException e) { // openConnection() failed System.out.println(“Connection could not be opened”); } Spring 2005— Pınar Yolum 14 7