Download presentation
Presentation is loading. Please wait.
Published byFrancine Marsh Modified over 8 years ago
1
Access to Remote Data (2009.12.27) Frank Ducrest
2
Access to Remote Data 2 Generic Connection Framework hierarchy of interfaces and classes for managing remote connections and I/O includes ➢ Connector – connection factory class ➢ Connection – generic interface for connections ➢ DatagramConnection – interface that defines datagram connections (UDP) ➢ StreamConnection – interface that defines stream connections (TCP) http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/Connector.html http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/Connection.html http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/DatagramConnection.html http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/StreamConnection.html
3
Generic Connection Framework (continued) includes (continued) ➢ StreamConnectionNotifier – interface for stream connection listeners ➢ ContentConnection – interface for support of transmission of content ➢ Datagram – interface for a single datagram ● reduces connections with remote objects to ➢ creating a URL with the socket, address and name ➢ invoking the Connector.open method to receive a Connection subclass object ➢ use that object to work with the connection http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/StreamConnectionNotifier.html http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/ContentConnection.html http://java.sun.com/javame/reference/apis/jsr139/javax/microedition/io/Datagram.html Access to Remote Data 3
4
URL Uniform Resource Locator; subset of URI (Uniform Resource Identifier) used to specify a location of an available resource a string that looks like this scheme://user:password@host:port/path;parameters ➢ scheme specifies the protocol to be used ➢ user and :password@ are optional and used only if necessary ➢ :port is optional; protocol default used if absent ➢ /path is optional; listening service default used if absent ➢ ;parameters is optional and used only if necessary Access to Remote Data 4
5
Connection Schemes all Java ME platforms must support HTTP and HTTPS see text page 298, table 12-1 for a listing Connection Implementations see text page 298, table 12-2 for Java Me connection implementations Access to Remote Data 5
6
Reading a File Over a Network very similar to input from a local file uses ContentConnection and Connector instead of FileConnection Connector is used to create a ContentConnection object, which is used to create the input stream object example ContentConnection cc = (ContentConnection)Connector.open(fileURL); DataInputStream dis = cc.openDataInputStream(); http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/Connector.html http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/ContentConnection.html Access to Remote Data 6
7
Example of Reading a File Across a Network import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class ReadOverNetworkMidlet extends MIDlet { private static final String fileURL = "http://fidelio.cacs.louisiana.edu/fdd5501/359java/mydata.txt"; private ContentConnection cc = null; private DataInputStream dis = null; public void startApp() { connectToFile(); readFromFile(); closeFile(); destroyApp(true); notifyDestroyed(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } Access to Remote Data 7
8
Example of Reading a File Across a Network (continued) private void connectToFile() { try { cc = (ContentConnection)Connector.open(fileURL); dis = cc.openDataInputStream(); System.out.println("Connected"); } catch (Exception e) { System.out.println("Connection exception"); e.printStackTrace(); } private void closeFile() { try { dis.close(); cc.close(); System.out.println("Closed"); } catch (Exception e) { System.out.println("Closing exception"); e.printStackTrace(); } Access to Remote Data 8
9
Example of Reading a File Across a Network (continued) private void readFromFile() { System.out.println("in readFromfile"); try { System.out.println("Numbers in:"); int len = (int)cc.getLength(); if (len > 0) { byte[] data = new byte[len]; dis.readFully(data);// read entire file String s = new String(data); System.out.println(s); // output all read Access to Remote Data 9
10
Example of Reading a File Across a Network (continued) // parse integers from string read in and sum int sum = 0; int i = 0, j = 0; while (j < s.length()-1) { j = s.indexOf("\n", i); try { int n = Integer.parseInt(s.substring(i, j)); sum += n; i = j+1; } catch (Exception e) { j = s.length(); } System.out.println("sum = " + sum); } } catch(Exception e) { System.out.println("readFromfile exception"); e.printStackTrace(); } System.out.println("out readFromfile"); } Access to Remote Data 10
11
Example of Reading a File Across a Network (continued) mydata.txt 34 28 19 87 45 99 1 26 55 66 example run Connected in readFromfile Numbers in: 34 28 19 87 45 99 1 26 55 66 sum = 460 out readFromfile Closed Access to Remote Data 11
12
Sockets a socket is one end of a link between two computers link is bi-directional link usually in the form of a client / server pair example: normal http port is 80 http://www.yahoo.comhttp://www.yahoo.com is sent by the client browser as http://www.yahoo.com:80 http://java.sun.com/docs/books/tutorial/networking/sockets/ http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html http://homepages.uel.ac.uk/2795l/pages/javaapps.htm http://www-106.ibm.com/developerworks/edu/j-dw-javasocks-i.html Access to Remote Data 12
13
Socket Time Client Example /* Some Public Time Servers: time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington nist1.datum.com 63.149.208.50 Datum, San Jose, California nist1.dc.glassey.com 216.200.93.8 Abovenet, Virginia nist1.ny.glassey.com 208.184.49.9 Abovenet, New York City nist1.sj.glassey.com 207.126.103.204 Abovenet, San Jose, California nist1.aol-ca.truetime.com 207.200.81.113 True Time, Sunnyvale, California nist1.aol-va.truetime.com 205.188.185.33 True Time, Virginiat */ import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class TimeMidlet extends MIDlet { public void pauseApp() { } public void destroyApp(boolean unconditional) { } Access to Remote Data 13
14
Socket Time Client Example (continued) public void startApp() { String url = "socket://time-a.nist.gov:13"; StreamConnection c = null; InputStream i = null; byte[] b = new byte[50]; try { c = (StreamConnection)Connector.open(url); i = c.openInputStream(); int bytes = i.read(b); String s = new String(b); System.out.println("All info received: " + s); int start = s.indexOf(" ")+1; System.out.println("Date: " + s.substring(start,start+8)); System.out.println("Time: " + s.substring(start+9,start+17) + " UTC"); i.close(); c.close(); } catch (Exception e) { e.printStackTrace(); } Access to Remote Data 14
15
Socket Time Client Example (continued) destroyApp(true); notifyDestroyed(); } Example Run All info received: 55192 09-12-27 16:43:09 00 0 0 935.4 UTC(NIST Date: 09-12-27 Time: 16:43:09 UTC Access to Remote Data 15
16
Socket Time Client Example /* Some Public Time Servers: time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington nist1.datum.com 63.149.208.50 Datum, San Jose, California nist1.dc.glassey.com 216.200.93.8 Abovenet, Virginia nist1.ny.glassey.com 208.184.49.9 Abovenet, New York City nist1.sj.glassey.com 207.126.103.204 Abovenet, San Jose, California nist1.aol-ca.truetime.com 207.200.81.113 True Time, Sunnyvale, California nist1.aol-va.truetime.com 205.188.185.33 True Time, Virginiat */ import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class TimeMidlet extends MIDlet { public void pauseApp() { } public void destroyApp(boolean unconditional) { } Access to Remote Data 16 Socket Email Client Example import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class EmailMidlet extends MIDlet { StreamConnection c = null; InputStream i = null; OutputStream o = null; byte[] b = new byte[200]; int bytes = 0; private void read() { try { bytes = i.read(b); System.out.println("First Read: " + new String(b)); } catch (Exception e) { System.out.println("Read Problem: "); e.printStackTrace(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { }
17
Socket Email Client Example (continued) public void startApp() { String url = "socket://mailer.louisiana.edu:25"; try { c = (StreamConnection)Connector.open(url); i = c.openInputStream(); o = c.openOutputStream(); read(); byte[] mailfrom = ("mail from: fdd5501@gmail.com\n").getBytes(); o.write(mailfrom); read(); byte[] addressee = ("rcpt to: fducrest@gmail.com\n").getBytes(); o.write(addressee); read(); byte[] data = ("data\n").getBytes(); o.write(data); read(); Access to Remote Data 17 Socket Time Client Example import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class EmailMidlet extends MIDlet { StreamConnection c = null; InputStream i = null; OutputStream o = null; byte[] b = new byte[200]; int bytes = 0; private void read() { try { bytes = i.read(b); System.out.println("First Read: " + new String(b)); } catch (Exception e) { System.out.println("Read Problem: "); e.printStackTrace(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { }
18
Socket Time Client Example (continued) public void startApp() { String url = "socket://mailer.louisiana.edu:25"; try { c = (StreamConnection)Connector.open(url); i = c.openInputStream(); o = c.openOutputStream(); byte[] mailfrom = ("mail from: fdd5501@louisiana.edu\n").getBytes(); o.write(mailfrom); read(); byte[] addressee = ("rcpt to: fducrest@gmail.com\n").getBytes(); o.write(addressee); read(); byte[] data = ("data\n").getBytes(); o.write(data); read(); Access to Remote Data 18 Socket Email Client Example (continued) byte[] subject = ("subject: Test Email\n").getBytes(); o.write(subject); byte[] toaddressee = ("To: " + addressee + "\n").getBytes(); o.write(toaddressee); byte[] message = ("This is a test\nemail over 3\nlines.\n").getBytes(); o.write(message); byte[] eom = (".\n").getBytes(); o.write(eom); read(); i.close(); o.flush(); o.close(); c.close();
19
Socket Email Client Example (continued) } catch (Exception e) { System.out.println("general problem"); e.printStackTrace(); } destroyApp(true); notifyDestroyed(); } Example Run Access to Remote Data 19 Socket Email Client Example (continued) byte[] subject = ("subject: Test Email\n").getBytes(); o.write(subject); byte[] toaddressee = ("To: " + addressee + "\n").getBytes(); o.write(toaddressee); byte[] message = ("This is a test\nemail over 3\nlines.\n").getBytes(); o.write(message); byte[] eom = (".\n").getBytes(); o.write(eom); read(); i.close(); o.flush(); o.close(); c.close(); } catch (Exception e) { System.out.println("general problem"); e.printStackTrace(); } destroyApp(true); notifyDestroyed(); }
20
Datagrams used for short messages no sequencing or flow control Connector class is used to make connections Datagram class objects encapsulate the datagram DatagramConnection or UDPDatagramConnection objects are used to send and receive Datagram objects http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/Connector.html http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/Datagram.html http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/DatagramConnection.html http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/io/UDPDatagramConnection.html Access to Remote Data 20
21
Datagram Client Example in the the example: ➢ the client sends a datagram to a datagram server; the data in the datagram is a pronoun (“Dud”) ➢ the server uses the data in the datagram to form a reply to the client in the form “Yo, “ plus the data from the client's datagram and a quote read from a file import javax.microedition.midlet.*; import javax.microedition.io.*; public class DatagramClientMidlet extends MIDlet { public void pauseApp() { } public void destroyApp(boolean unconditional) { } Access to Remote Data 21
22
Datagram Client Example (continued) public void startApp() { byte[] b = new byte[256]; String url = "datagram://...ip-or-url...:4445"; DatagramConnection dgc = null; byte[] m = ("Dud\n").getBytes(); try { dgc =(DatagramConnection)Connector.open(url); Datagram d = dgc.newDatagram(m, m.length); dgc.send(d); d = dgc.newDatagram(b, 256); dgc.receive(d); String s = new String(d.getData()); System.out.println("incomming datagram data: " + s.substring(0, d.getLength()-1)); System.out.println( "incomming datagram buffer length: " + d.getLength()); } catch (Exception e) {} Access to Remote Data 22 Socket Email Client Example import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class EmailMidlet extends MIDlet { StreamConnection c = null; InputStream i = null; OutputStream o = null; byte[] b = new byte[200]; int bytes = 0; private void read() { try { bytes = i.read(b); System.out.println("First Read: " + new String(b)); } catch (Exception e) { System.out.println("Read Problem: "); e.printStackTrace(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { }
23
Datagram Client Example public void startApp() { byte[] b = new byte[256]; String url = "datagram://...ip-or-url...:4445"; DatagramConnection dgc = null; byte[] m = ("Dud\n").getBytes(); try { dgc =(DatagramConnection)Connector.open(url); Datagram d = dgc.newDatagram(m, m.length); dgc.send(d); d = dgc.newDatagram(b, 256); dgc.receive(d); String s = new String(d.getData()); System.out.println("incomming datagram data: " + s.substring(0, d.getLength()-1)); System.out.println( "incomming datagram buffer length: " + d.getLength()); } catch (Exception e) {} Access to Remote Data 23 Datagram Client Example (continued) finally { try { if (dgc == null) dgc.close(); } catch (Exception e) { e.printStackTrace(); } destroyApp(true); notifyDestroyed(); } Example Run incomming datagram data: Yo, Dud, Life is wonderful. Without it we'd all be dead. incomming datagram buffer length: 57
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.