Modernas Programmeanas Tehnologijas Advanced Programming Technologies - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Modernas Programmeanas Tehnologijas Advanced Programming Technologies

Description:

... from a Web browser, such as Mozilla or Microsoft Internet Explorer, to see the ... Invoke JEditorPane method setPage to download the document specified by location ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 57
Provided by: Edg89
Category:

less

Transcript and Presenter's Notes

Title: Modernas Programmeanas Tehnologijas Advanced Programming Technologies


1
Modernas Programmeanas Tehnologijas (Advanced
Programming Technologies)
  • Edgars Celms, Martin Opmanis
  • (askola_at_mii.lu.lv)

Latvijas Universitates Matematikas un
informatikas instituts 2007, Riga, Latvija
2
Tikloanas iespejas valoda Java (networking)
  • Java How to Program, Sixth Edition
  • Chapter 24. Networking
  • Darboanas ar URL
  • Datu lasiana no Web Servera
  • Vienkara servera izveidoana izmantojot stream
    sockets
  • Vienkara klienta izveidoana izmantojot stream
    sockets
  • Klienta/Servera sadarbiba izmantojot stream
    sockets savienojumu
  • Bezsavienojuma (connectionless) Klienta/Servera
    sadarbiba izmantojot datagrams
  • Klienta/Servera Tic-Tac-Toe izmantojot
    daudzpavedienu (multithreading) serveri

3
Ievads
  • Pakotne java.net satur klases, kuras nodroina
    darbu ar tikla servisiem
  • Plusmas bazeta sadarbiba (stream-based
    communications)
  • Lietojumprogrammas tikloanos uztver ka datu
    plusmas (streams of data)
  • Konekciju bazeti protokoli
  • Izmanto TCP (Transmission Control Protocol)
  • Pakeu bazeta sadarbiba (packet-based
    communications)
  • Tiek sutitas individualas paketes
  • Bezkonekciju servisi (connectionless service)
  • Izmanto UDP (User Datagram Protocol)
  • Klienta-servera attieciba (client-server
    relationship)
  • Klients pieprasa veikt kadas darbibas
  • Serveris izpilda darbibas un atbild klientam
  • Pieprasijuma-atbildes modelis (request-response
    model)
  • Popularaka implementacija timekla
    parlukprogrammas (web browsers) un timekla
    serveri (web servers)

4
Daas piezimes I (Software Engineering
Observation)
  • Performance tip. Connectionless services
    generally offer greater performance but less
    reliability than connection-oriented services.
  • Portability tip. TCP, UDP and related protocols
    enable a great variety of heterogeneous computer
    systems (i.e., computer systems with different
    processors and different operating systems) to
    intercommunicate.

5
Darboanas ar URLiem (manipulating URLs)
  • HyperText Transfer Protocol (HTTP)
  • Izmanto URI (Uniform Resource Identifiers) lai
    identificetu datus
  • URL (Uniform Resource Locators)
  • URI specifice dokumentu atraanas vietu
  • Var referencet failus, folderus, ka ari daadus
    saliktus (complex) objektus.
  • HTML dokuments SiteSelector.html
  • applet elements
  • param birka (tag)
  • name atributs
  • value atributs

6
HTML document to load SiteSelector applet
7
(No Transcript)
8
When the user selects one of the Web sites listed
in siteChooser, the program calls method
valueChanged
Pass the selected site name (the key) to HashMap
method get, which locates and returns a reference
to the corresponding URL (the value)
Get a reference to an AppletContext object that
represents the applet container
Display in the current browser window the
resource associated with URL newDocument
9
Use Applet method getParameter to obtain a Web
site title
Use Applet method getParameter to obtain the Web
site location
Use the location as the value of a new URL object
If the location passed to the URL constructor is
invalid, the URL constructor throws a
MalformedURLException
10
(No Transcript)
11
Darboanas ar URLiem (manipulating URLs)
  • HTML freimi (frames)
  • Ir ari iespeja specificet freimu showDocument
    metode
  • _blank
  • _self
  • _top

12
Daas piezimes II (Software Engineering
Observation)
  • The applet in example must be run from a Web
    browser, such as Mozilla or Microsoft Internet
    Explorer, to see the results of displaying
    another Web page. The appletviewer is capable
    only of executing applets it ignores all other
    HTML tags. If the Web sites in the program
    contained Java applets, only those applets would
    appear in the appletviewer when the user selected
    a Web site. Each applet would execute in a
    separate appletviewer window.

13
Datu lasiana no Web Servera
  • Swing GUI komponente JEditorPane
  • Spej apstradat gan parastu tekstu (plain text),
    gan ari HTML-formatetu tekstu
  • Piemers, ReadServerFile.java darbojas ka
    vienkara timekla parlukprogramma (web browser)
  • Attelo failus no timekla servera pec uzdota URI
  • HyperlinkEvents notikumi
  • Notiek bridi, kad lietotajs uzklikkina uz
    hipersaites (hyperlink)
  • Tris notikumu tipi
  • HyperlinkEvent.EventType.ACTIVATED
  • HyperlinkEvent.EventType.ENTERED
  • HyperlinkEvent.EventType.EXITED
  • JEditorPane genere HyperlinkEvents tikai un
    vienigi tad, ja ta ir nemodificejama
    (uneditable).

14
(No Transcript)
15
Create JEditorPane using the no-argument
constructor
Set JEditorPane editable property to false
Register a HyperlinkListener to handle
HyperlinkEvents, which occur when the user clicks
a hyperlink in the JEditorPane
Method hyperlinkUpdate is called when a
HyperlinkEvent occurs
Use HyperlinkEvent method getEventType to
determine the type of the HyperlinkEvent
Use HyperlinkEvent method getURL to obtain the
URL represented by the hyperlink
16
(No Transcript)
17
(No Transcript)
18
Vienkara servera izveidoana izmantojot stream
sockets
  • Pieci soli vienkara servera izveidoanai valoda
    Java
  • 1. Solis Jaizveido ServerSocket objekts
  • ServerSocket server new ServerSocket(
    portNumber, queueLength )
  • Japieregistre pieejamais (brivais) ports
  • Jaspecifice maksimalais klientu skaits (queue
    length).
  • Porta numuru klients izmanto, lai atrastu servera
    aplikaciju uz servera, tiek saukts ari par
    Handshake point
  • Jasasaista serveris ar portu (binding the server
    to the port)
  • Tikai viena servera aplikacija var but
    piesaistits specifiskam portam
  • 2. Solis Serveris klausas klienta pieslegumus
  • Serveris nepartraukti (indefinitely) klausas
    potencialos klienta pieslegumus
  • Socket connection server.accept()
  • Izmantojot Socket objektu serveris sadarbojas ar
    klientu
  • The interactions with the client actually occur
    at a different server port from the handshake
    point. This allows the port specified in Step 1
    to be used again in a multithreaded server to
    accept another client connection.
  • 3. Solis Datu sutiana un sanemana
  • OutputStream datu sutianai un InputStream datu
    sanemanai
  • Metode getOutputStream atgrie Socketa
    OutputStream
  • Metode getInputstream atgrie Socketa
    InputStream
  • 4. Solis Procesa faze
  • Serveris un klients sadarbojas izmantojot plusmas

19
Daas piezimes III (Software Engineering
Observation)
  • Port numbers can be between 0 and 65,535. Most
    operating systems reserve port numbers below 1024
    for system services (e.g., e-mail and World Wide
    Web servers). Generally, these ports should not
    be specified as connection ports in user
    programs. In fact, some operating systems require
    special access privileges to bind to port numbers
    below 1024.
  • With sockets, network I/O appears to Java
    programs to be similar to sequential file I/O.
    Sockets hide much of the complexity of network
    programming from the programmer.
  • With Javas multithreading, we can create
    multithreaded servers that can manage many
    simultaneous connections with many clients. This
    multithreaded-server architecture is precisely
    what popular network servers use.
  • A multithreaded server can take the Socket
    returned by each call to accept and create a new
    thread that manages network I/O across that
    Socket. Alternatively, a multithreaded server can
    maintain a pool of threads (a set of already
    existing threads) ready to manage network I/O
    across the new Sockets as they are created.
  • Performance tip.In high-performance systems in
    which memory is abundant, a multithreaded server
    can be implemented to create a pool of threads
    that can be assigned quickly to handle network
    I/O across each new Socket as it is created.
    Thus, when the server receives a connection, it
    need not incur the overhead of thread creation.
    When the connection is closed, the thread is
    returned to the pool for reuse.

20
Vienkara klienta izveidoana izmantojot stream
sockets
  • Cetri soli vienkara klienta izveidoanai valoda
    Java
  • 1. Solis Lai pieslegtos serverim ir jaizveido
    Socket objekts
  • Socket connection new Socket ( serverAddress,
    port )
  • 2. Solis Datu sutiana un sanemana
  • OutputStream datu sutianai un InputStream datu
    sanemanai
  • Metode getOutputStream atgrie Socketa
    OutputStream
  • Metode getInputstream atgrie Socketa
    InputStream
  • 4. Solis Procesa faze
  • Klients un serveris sadarbojas izmantojot plusmas
  • 4. Solis Sledz plusmas un savienojumus

21
Klienta/Servera sadarbiba izmantojot stream
sockets savienojumu
  • Klienta/servera chat lietojumprogramma
  • Izmanto stream sockets
  • Serveris gaida klienta piesleguma meginajumu
  • Klients piesledzas serverim
  • Suta un sanem zinojumus
  • Klients vai serveris izbeidz savienojumu
  • Serveris gaida nakamo klienta pieslegumu

22
Import ServerSocket and Socket from package
java.net
Declare ServerSocket server which waits for
client connection
Declare Socket connection which connects to the
client
23
(No Transcript)
24
Wait for a client connection
Create ServerSocket at port 12345 with queue of
length 100
After the connection is established, obtain
references to the streams for the connection
Send the initial connection message to the client
and process all messages received from the client
25
Use ServerSocket method accept to wait for a
connection from a client
Output the host name of the computer that made
the connection using Socket method getInetAddress
and InetAddress method getHostName
Obtain Sockets OutputStream and use it to
initialize ObjectOutputStream
Method flush empties output buffer and sends
header information
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
Popularakas programmeanas kludas I (Common
Programming Error)
  • Specifying a port that is already in use or
    specifying an invalid port number when creating a
    ServerSocket results in a BindException.

31
Daas piezimes IV (Software Engineering
Observation)
  • When using an ObjectOutputStream and
    ObjectInputStream to send and receive data over a
    network connection, always create the
    ObjectOutputStream first and flush the stream so
    that the clients ObjectInputStream can prepare
    to receive the data. This is required only for
    networking applications that communicate using
    ObjectOutputStream and ObjectInputStream.
  • Performance tip.A computers input and output
    components are typically much slower than its
    memory. Output buffers typically are used to
    increase the efficiency of an application by
    sending larger amounts of data fewer times, thus
    reducing the number of times an application
    accesses the computers input and output
    components.

32
(No Transcript)
33
(No Transcript)
34
(No Transcript)
35
Use InetAddress static method getByName to obtain
an InetAdress object containing the IP address
specified as a command-line argument to the
application
Create a Socket that will connect with port 12345
on the server
Display a message indicating the name of the
server computer to which the client has connected
Obtain Sockets OutputStream and use it to
initialize ObjectOutputStream
Method flush empties output buffer and sends
header information
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
Bezsavienojuma (connectionless) klienta/servera
sadarbiba izmantojot datagrams
  • Bezsavienojuma sadarbiba izmantojot datagrams
  • Netiek uzturets savienojums starp datoriem
  • Sadala zinojumus atsevikas dalas un suta ka
    paketes
  • Zinojumi var pienakt noteikta seciba, bez
    secibas, vai ari nepienakt vispar
  • Sanemejam ir javar sakartot paketes pareiza
    seciba un izlasit

42
(No Transcript)
43
(No Transcript)
44
Use DatagramPacket method getAddress to obtain
the host name of the computer from which the
packet was sent
Use DatagramPacket method getPort to obtain the
port number through which the host computer sent
the packet
Use DatagramPacket method getLength to obtain the
number of bytes of data sent
Use DatagramPacket method getData to obtain an
byte array containing the data
Create a DatagramPacket, which specifies the data
to send, the number of bytes to send, the client
computers Internet address and the port where
the client is waiting to receive packets
45
Use method send of DatagramSocket to send the
packet over the network
46
Server window after packet of data is received
from client
47
(No Transcript)
48
Convert the String to a byte array
Create a DatagramPacket and initialize it with
the byte array, the length of the string that was
entered by the user, the IP address to which the
packet is to be sent and the port number at which
the server is waiting
Use DatagramPacket method send to send the packet
49
(No Transcript)
50
Create a DatagramPacket to store received
information
Use DatagramPacket method getAddress to obtain
the host name of the computer from which the
packet was sent
Use DatagramPacket method getPort to obtain the
port number through which the host computer sent
the packet
Use DatagramPacket method getLength to obtain the
number of bytes of data sent
Use DatagramPacket method getData to obtain an
byte array containing the data
51
(No Transcript)
52
Client window after sending packet to Server
and receiving packet back from Server
53
Popularakas programmeanas kludas II (Common
Programming Error)
  • Specifying a port that is already in use or
    specifying an invalid port number when creating a
    DatagramSocket results in a SocketException.

54
Klienta/Servera Tic-Tac-Toe izmantojot
daudzpavedienu (multithreading) serveri
  • Tuvak neapskatisim.
  • Sikak skat. Java How to Program, Sixth Edition
  • 24.8. Client/Server Tic-Tac-Toe Using a
    Multithreaded Server
  • With Javas multithreading, we can create
    multithreaded servers that can manage many
    simultaneous connections with many clients. This
    multithreaded-server architecture is precisely
    what popular network servers use.
  • A multithreaded server can take the Socket
    returned by each call to accept and create a new
    thread that manages network I/O across that
    Socket. Alternatively, a multithreaded server can
    maintain a pool of threads (a set of already
    existing threads) ready to manage network I/O
    across the new Sockets as they are created.
  • Ja ir interese, tad varat izpetit ari ka ir
    realizets DeitelMessenger Serveris un Klients
  • Sikak skat. Java How to Program, Sixth Edition
  • 24.10. Case Study DeitelMessenger Server and
    Client

55
Klienta/Servera Tic-Tac-Toe izmantojot
daudzpavedienu (multithreading) serveri
56
Jautajumi ?
Write a Comment
User Comments (0)
About PowerShow.com