JPN - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

JPN

Description:

One of the reasons of the interest in Java is its delivery mechanism. ... THIS EMULATES A DIRECT STREAM OF DATA BETWEEN TWO M. OOP&M - laboratory lectures. 9 ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 29
Provided by: K3D
Category:
Tags: jpn | emulates | keyb

less

Transcript and Presenter's Notes

Title: JPN


1
JPN GUI and Applets
I am sure it wont fail as much as the one
before - Bill Gates about W98 -
2
JPN Java and the Net
Clients and Servers One of the reasons of the
interest in Java is its delivery mechanism. Java
applets do not reach the user as a fixed and
closed product into a floppy or CD. Nor they are
downloaded only once from the net to be used over
and over again. Instead, they are embedded within
a web page and downloaded anew with each loading
of the web page. Java applets are not only on
the Internet, but are creatures of the net as
well - Arnow/Weiss -
3
JPN Java and the Net
  • Internet Communication
  • Communication on the internet can be defined as
    the transfer of data from one machine to another.
  • Into this definition there are many different
    concepts that fit
  • Email
  • Remote logins
  • Download of web pages and applets
  • Chat rooms
  • Information is break into small pieces called
    packets and transferred in that form

4
JPN Java and the Net
Internet Communication The machine that is
sending the information is almost never connected
directly to the receiver, so these packets are
actually transferred from one machine to another
through the internet until they arrive at their
destination The machines responsible of this
transfer of data are called routers and are
dedicated only to this purpose. Therefore they
use a special software and have special form (and
are quite expensive go to www.cisco.com and
check it out) The transfer of packets is not
secure, they can get lost in the way. The
connection between machines is not good enough,
and the machines themselves have limited memory
if packets arrive at rate higher than machines
process rate, they could be lost
5
JPN Java and the Net
Internet Communication
R1
R3
R5
R4
R2
Computer 6
R6
Computer 5
Network B
Computer 10
Computer 7
Network C
Computer 4
Computer 8
Computer 9
6
JPN Java and the Net
Internet Communication
R1
R3
R5
R4
R2
Computer 6
Computer 5
  • Internet Communication is
  • One way
  • Broken into packets
  • Unreliable (packets can get lost)
  • Transmitted over many hops

Network B
Computer 4
7
JPN Java and the Net
Internet Communication
R1
R3
R5
R4
R2
Computer 6
Computer 5
  • With TCP it appears to be
  • Bidirectional
  • A continuous Stream
  • Reliable
  • Direct

Network B
Computer 4
8
JPN Java and the Net
Internet Communication Every router must be
connected, at least, to another router, so that
there will always be a path of routers between
any couple of machines residing in different
networks. The fact of the looses in terms of
packets makes the network a not useful tool for
almost any kind of applications. But there is a
set of tools, which are the communication
protocols, in order to make possible, the use of
the net a communication medium. First, the
Internet Protocol is running in all the
machines at the net. Our applications use the
Transfer Control Protocol. Both observe how the
packets are transferred through the network and
forces the sender to repeat if any loose of data
is detected. THIS EMULATES A DIRECT STREAM OF
DATA BETWEEN TWO M.
9
JPN input / output to programs
network
InputStream
keyboard
FileOutputStream
System.in
Java program
files
FileInputStream
System.out
InputStream
OutputStream
Java program
monitor
10
JPN input/output an overview
Input source
Java program
bytes of data
ewsc24rfds53Hej20Hur20m76ar20du20?rsf
We must build a bridge between the Java Program
and the input source!!
11
JPN input as example
  • This references could be of two types
  • FileInputStreamReader for files
  • BufferedInputStream for keyboard and the network
  • Construct a reference to an InputStream object,
    and with it
  • Construct an InputStreamReader object, and with
    it
  • Construct a BufferedReader object

Stages
  • Note that
  • InputStream inputs sequences of bytes into the
    program
  • InputStreamReader models the stream of input as
    characters, but doesnt recognize ends of line
    a.o.
  • BufferedReader has got a collection of methods
    that allow us to work in a similar way as we did
    with PrintStream

12
JPN a board with keys, the example
Java provides a predefined object to represent
the stream of input that comes from the keyboard.
System.in is a reference to this object contained
into the BufferedInputStream class
Unlike System.out, which refers to a PrintStream
object and therefore can be used right away to
write Strings to the monitor, System.in, a
reference to a BufferedInputStream object, cannot
be readily used to read Strings
keyboard
System.in
isr


existing InputStream
keyb


new InputStreamReader

new BufferedReader
13
JPN a board with keys, the example
Java provides System.in contained into the
BufferedInputStream class
the constructor for InputStreamReader accepts the
keyboard (a BufferedInputStream reference) as its
argument
new InputStreamReader (System.in)
the constructor for BufferedReader accepts a
InputStreamReader reference as its argument
new BufferedReader (ISR)
14
JPN a board with keys, the example
The total declaration would be
InputStreamReader isr BufferedReader
keyb isr new InputStreamReader(System.in)
keyb new BufferedReader(isr)
And now, for reading a line
InputStreamReader isr BufferedReader
keyb String inputline isr
new InputStreamReader(System.in) keyb new
BufferedReader(isr) inputline keyb.readLine()
.readLine() is a method from the BufferedReader
class
15
JPN a board with keys, the example
Example a program that writes the plural of a
word
import java.io. / This program writes the
plural of the word typed in the keyboard it
just adds an s / class plural public
static void main(String arg) throws Exception
InputStreamReader isr
BufferedReader keyb String
inputline isr new
InputStreamReader(System.in) keyb new
BufferedReader(isr) inputline
keyb.readLine() System.out.print(inputlin
e) System.out.println(s)
16
JPN come IN PUT files
Obtaining input from disk files is only a little
bit more complicated than from the keyboard. Our
starting point must be to find some sort of
InputStream object. As we said before, both
BufferedInputStream and FileInputStream belong to
that class
System.in is already defined into the class
BufferedInputStream, the problem is that the
FileInputStream class does not have defined
objects like that. Therefore we need to first
define an object that makes reference to a file.
fsr

files
isr


new FileInputStream
bsr


new InputStreamReader

new BufferedReader
17
JPN come IN PUT files
the constructor for FileInputStream accepts a
file (a File reference) as its argument
new FileInputStream (file)
the constructor for InputStreamReader accepts a
stream from a file (a FileInputStream reference)
as its argument
new InputStreamReader (FileInputStream)
the constructor for BufferedReader accepts a
InputStreamReader reference as its argument
new BufferedReader (ISR)
18
JPN come IN PUT files
The total declaration would be
File f FileInputStream
fsr InputStreamReader isr BufferedReader
bsr f new File(Big_parties_many_potat
is.txt) fsr new FileInputStream(f) isr new
InputStreamReader(fsr) bsr new
BufferedReader(isr)
19
JPN come IN PUT files
Lets read a couple of lines from the file and
print them
File f FileInputStream
fsr InputStreamReader isr BufferedReader
bsr String inputline f
new File(Big_parties_many_potatis.txt) fsr
new FileInputStream(f) isr new
InputStreamReader(fsr) bsr new
BufferedReader(isr) inputline
bsr.readLine() System.out.println(inputline) inp
utline bsr.readLine() System.out.println(inputl
ine)
20
JPN Streams from the internet
  • When we are try to read information from the Web
  • The Net as other communication media for our
    programs can be read using Streams
  • There is a difference between what we saw until
    now and what will happen with the Net files are
    in HTML code
  • The constructors are

String s URL u InputStream
ins InputStreamReader isr BufferedReader
link u new URL("http//www.domain.ext/") ins
u.openStream() isr new InputStreamReader(ins
) link new BufferedReader(isr) s
link.readLine()
As you see here, the structure is similar to the
one that we saw for the files. The difference is
at the constructor for the InputStream object,
which is special!! But the method for reading
the information is the same .readline()
21
JPN Streams from the internet
Example Just reading a file
import java.net. import java.io. class
MyFirstBrowser public static void
main(String arg) throws Exception URL
uNet InputStream insNet
InputStreamReader isrNet BufferedReader
linkNet String s uNet new
URL("http//webzone.k3.mah.se/k3dacu/oopm/week5/in
dex1.html") insNet uNet.openStream()
isrNet new InputStreamReader(insNet)
linkNet new BufferedReader(isrNet) s
linkNet.readLine() System.out.println(s)
s linkNet.readLine() System.out.println(s)
System.out.println("connection closed")

22
JPN internet
The Browser is much more than only that
  • Features needed
  • Interpret HTML TAGs
  • underline a text if it is a link (ltA HREF..gt)
  • jump line with ltPgt ltBRgt
  • show the name of the images (ltIMG SRC...gt)
  • try to show tables (ltTABLEgtltTRgtltTDgt...)
  • think how to ask to the user for the next jump to
    do ...
  • Interpret scripting languages
  • JavaScript
  • Visual Basic Script
  • Perl
  • Calls external applications
  • Office
  • Java Virtual Machine

23
JPN Java and the Net
Sockets A TCP connection between two programs in
many ways is like a phone connection. Data can
move in both directions simultaneously. But,
before the communication act takes place, the
connection must be established ? one party must
call the other Both parties need something
beside, like in a phone call, both speakers have
a telephone. The analogy between the telephone
and the programming world is the socket. It is to
the communication between computers like the
telephone between two persons In Java we model
the telephone with an object of the Socket class.
A socket is created for making a single TCP
connection
24
JPN Java and the Net
Sockets The arguments to the constructor of the
socket are the machine to contact and the port
where the communication will take place. The port
could be understood as the extension to the phone
number in a large phone system new
Socket(www.yahoo.com, 80) The socket provides
the methods .getInputStream() and
.getOutputStream(), from whose return values
BufferedReader objects and PrintStream objects
can be constructed. These objects use the TCP
connection as their input source or output target
25
JPN Java and the Net
Sockets The typical example would look
like import java.io. import
java.net. Socket s new Socket(hostname,
portnumber) BufferedReader br new
BufferedReader( new InputStreamReader( s.getInp
utStream())) PrintStream ps new
PrintStream( s.getOutputStream()) String line
br.readLine() //reads from the
host ps.println(hej) //writes to the host
26
JPN Java and the Net
Client-Server Computing Sockets and TCP/IP
connections allow programs to communicate throw
the internet, but they dont determine any
particular style of communication or relationship
between the programs. There is a widely used
design approach for Internet applications - the
so called client-server model In this model,
an application consist of one program running as
server, and another as a client
Server a program that provides a service
Client a program that requests a service
27
JPN Java and the Net
  • Client-Server Computing
  • Client an server converse, what means that they
    follow some communication rules. From the point
    of view of the client, what happens is
  • Creates a socket object with the machine address
    and port to the server
  • Sends a message to the server on the TCP
    connection associated to the socket
  • Waits for response from the server that will be
  • More information needed
  • Requested action performed
  • The requested data
  • Each communication is established (1) only once,
    but the conversation can be repeated (2-3) many
    times

28
JPN Java and the Net
Client-Server Protocols Each client-server
application has its own rules (or protocol)
governing the conversation between client and
server The web clients and servers use one
protocol called HyperText Transport Protocol
(HTTP) Email clients and servers use a
different protocol, called Simple Mail Transfer
Protocol (SMTP) if we send mail or POP if we
receive (there are other variants, like IMAP,
a.o.)
Write a Comment
User Comments (0)
About PowerShow.com