Access to Remote Data (2009.12.27) Frank Ducrest.

Slides:



Advertisements
Similar presentations
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Advertisements

Network Programming Chapter 11 Lecture 6. Networks.
Prepared By E. Musa Alyaman1 URLs, InetAddresses, and URLConnections Chapter 9.
Cosc 4755 Mobile networking Overview and Stream, Socket, and Datagram.
Socket Programming.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
28-Jun-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Process-to-Process Delivery:
An Introduction to the Java ME Project Jens A Andersson.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
The Inter-network is a big network of networks.. The five-layer networking model for the internet.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 15 IO Using http Rob Pooley
ECEN “Internet Protocols and Modeling”, Spring 2012 Course Materials: Papers, Reference Texts: Bertsekas/Gallager, Stuber, Stallings, etc Class.
MIDP Programming Networking. Chapter Objectives The CLDC Streams Model Generic Connection Framework (GCF) Supported Protocols Creating a Connection Review.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Java’s networking capabilities are declared by the classes and interfaces of package java.net, through which Java offers stream-based communications that.
1 Network Communications A Brief Introduction. 2 Network Communications.
Distributed Computing & Embedded Systems Chapter 4: Remote Method Invocation Dr. Umair Ali Khan.
Network Programming. These days almost all devices.
The Record Store ( ) Frank Ducrest. The Record Store 2 The Record Store provides persistence of data between MIDlet runs not quite a DBMS API.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Socket Programming Client/Server.
Echo Networking COMP
Computing with C# and the .NET Framework
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
Networking with Java 2.
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Basic Protocols 24-Nov-18.
Process-to-Process Delivery:
Basic Protocols 19-Feb-19.
Process-to-Process Delivery: UDP, TCP
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Access to Remote Data ( ) Frank Ducrest

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)

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 Access to Remote Data 3

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 specifies the protocol to be used ➢ user and 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

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

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(); Access to Remote Data 6

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 = " 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

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

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

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

Example of Reading a File Across a Network (continued) mydata.txt example run Connected in readFromfile Numbers in: sum = 460 out readFromfile Closed Access to Remote Data 11

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 is sent by the client browser as Access to Remote Data 12

Socket Time Client Example /* Some Public Time Servers: time-a.nist.gov NIST, Gaithersburg, Maryland time-b.nist.gov NIST, Gaithersburg, Maryland time-a.timefreq.bldrdoc.gov NIST, Boulder, Colorado time-b.timefreq.bldrdoc.gov NIST, Boulder, Colorado time-c.timefreq.bldrdoc.gov NIST, Boulder, Colorado utcnist.colorado.edu University of Colorado, Boulder time.nist.gov NCAR, Boulder, Colorado time-nw.nist.gov Microsoft, Redmond, Washington nist1.datum.com Datum, San Jose, California nist1.dc.glassey.com Abovenet, Virginia nist1.ny.glassey.com Abovenet, New York City nist1.sj.glassey.com Abovenet, San Jose, California nist1.aol-ca.truetime.com True Time, Sunnyvale, California nist1.aol-va.truetime.com 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

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

Socket Time Client Example (continued) destroyApp(true); notifyDestroyed(); } Example Run All info received: :43: UTC(NIST Date: Time: 16:43:09 UTC Access to Remote Data 15

Socket Time Client Example /* Some Public Time Servers: time-a.nist.gov NIST, Gaithersburg, Maryland time-b.nist.gov NIST, Gaithersburg, Maryland time-a.timefreq.bldrdoc.gov NIST, Boulder, Colorado time-b.timefreq.bldrdoc.gov NIST, Boulder, Colorado time-c.timefreq.bldrdoc.gov NIST, Boulder, Colorado utcnist.colorado.edu University of Colorado, Boulder time.nist.gov NCAR, Boulder, Colorado time-nw.nist.gov Microsoft, Redmond, Washington nist1.datum.com Datum, San Jose, California nist1.dc.glassey.com Abovenet, Virginia nist1.ny.glassey.com Abovenet, New York City nist1.sj.glassey.com Abovenet, San Jose, California nist1.aol-ca.truetime.com True Time, Sunnyvale, California nist1.aol-va.truetime.com 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 Client Example import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class Midlet 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) { }

Socket 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: o.write(mailfrom); read(); byte[] addressee = ("rcpt to: 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 Midlet 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) { }

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: o.write(mailfrom); read(); byte[] addressee = ("rcpt to: o.write(addressee); read(); byte[] data = ("data\n").getBytes(); o.write(data); read(); Access to Remote Data 18 Socket Client Example (continued) byte[] subject = ("subject: Test \n").getBytes(); o.write(subject); byte[] toaddressee = ("To: " + addressee + "\n").getBytes(); o.write(toaddressee); byte[] message = ("This is a test\n over 3\nlines.\n").getBytes(); o.write(message); byte[] eom = (".\n").getBytes(); o.write(eom); read(); i.close(); o.flush(); o.close(); c.close();

Socket Client Example (continued) } catch (Exception e) { System.out.println("general problem"); e.printStackTrace(); } destroyApp(true); notifyDestroyed(); } Example Run Access to Remote Data 19 Socket Client Example (continued) byte[] subject = ("subject: Test \n").getBytes(); o.write(subject); byte[] toaddressee = ("To: " + addressee + "\n").getBytes(); o.write(toaddressee); byte[] message = ("This is a test\n 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(); }

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 Access to Remote Data 20

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

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 Client Example import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; public class Midlet 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) { }

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