03 - ΣΔΥ50 - ΟΣΣ3 -...

download 03 - ΣΔΥ50 - ΟΣΣ3 - Ασκήσεις

of 50

  • date post

    28-Feb-2018
  • Category

    Documents

  • view

    225
  • download

    0

Embed Size (px)

Transcript of 03 - ΣΔΥ50 - ΟΣΣ3 -...

  • 7/25/2019 03 - 50 - 3 -

    1/50

    3

  • 7/25/2019 03 - 50 - 3 -

    2/50

    1 - Java Sockets) Echo service

    Java 4.5 4.6 (www.cdk5.net/ipc) echo TCP streams. server client. : java TCPServer

    java TCPClient Hello SDY50 Tomos Blocalhost

    server IP port number /

    2

  • 7/25/2019 03 - 50 - 3 -

    3/50

    Fig. 4.5 TCPClient

    import java.net.*;

    import java.io.*;

    public class TCPClient {

    public static void main (String args[]) {

    // arguments supply message and hostname of destination

    Socket s = null;

    try{

    int serverPort = 7896;

    s = new Socket(args[1], serverPort);

    DataInputStream in = new DataInputStream( s.getInputStream());

    DataOutputStream out =

    new DataOutputStream( s.getOutputStream());

    out.writeUTF(args[0]); // UTF is a string encoding see Sn 4.3

    String data = in.readUTF();

    System.out.println("Received: "+ data) ;}catch (UnknownHostException e){

    System.out.println("Sock:"+e.getMessage());

    }catch (EOFException e){System.out.println("EOF:"+e.getMessage());

    }catch (IOException e){System.out.println("IO:"+e.getMessage());}

    }finally {if(s!=null) try {s.close();}catch (IOException

    e){System.out.println("close:"+e.getMessage());}}

    }

    }

    TCP client makes connection to server, sends request and receives reply

  • 7/25/2019 03 - 50 - 3 -

    4/50

    import java.net.*;

    import java.io.*;

    public class TCPServer {

    public static void main (String args[]) {

    try{

    int serverPort = 7896;

    ServerSocket listenSocket = new ServerSocket(serverPort);

    while(true) {

    Socket clientSocket = listenSocket.accept();

    Connection c = new Connection(clientSocket);

    }} catch(IOException e) {System.out.println("Listen :"+e.getMessage());}

    }

    }

    // this figure continues on the next slide

    Fig. 4.6 TCPServer TCP server makes a connection for each client and then echoes the

    clients request

  • 7/25/2019 03 - 50 - 3 -

    5/50

    class Connection extends Thread {

    DataInputStream in;DataOutputStream out;

    Socket clientSocket;

    public Connection (Socket aClientSocket) {

    try {

    clientSocket = aClientSocket;

    in = new DataInputStream( clientSocket.getInputStream());

    out =new DataOutputStream( clientSocket.getOutputStream());

    this.start();

    } catch(IOException e) {System.out.println("Connection:"+e.getMessage());}

    }

    public void run(){

    try { // an echo server

    String data = in.readUTF();out.writeUTF(data);

    } catch(EOFException e) {System.out.println("EOF:"+e.getMessage());

    } catch(IOException e) {System.out.println("IO:"+e.getMessage());}

    } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}

    }

    }

    Fig. 4.6 continued

  • 7/25/2019 03 - 50 - 3 -

    6/50

    TCPServer

    public class TCPServer {public static void main (String args[]) {

    try{

    int serverPort = 7896; // the server port

    ServerSocket listenSocket = new ServerSocket(serverPort);

    while(true) {

    Socket clientSocket = listenSocket.accept();

    Connection c = new Connection(clientSocket);}

    } catch(IOException e) {System.out.println("Listensocket:"+e.getMessage());}

    }

    } 6

  • 7/25/2019 03 - 50 - 3 -

    7/50

    TCPServer

    public class TCPServer {public static void main (String args[]) {

    try{

    int serverPort = 7896; // the server port

    ServerSocket listenSocket = new ServerSocket(serverPort);

    while(true) {

    Socket clientSocket = listenSocket.accept();

    System.out.println("Connection request form client: " +clientSocket.getInetAddress() + " : " + clientSocket.getPort());

    Connection c = new Connection(clientSocket);}

    } catch(IOException e) {System.out.println("Listensocket:"+e.getMessage());}

    }

    } 7

  • 7/25/2019 03 - 50 - 3 -

    8/50

    1 - Java Sockets) Knock KncokWhos there?

    Knock KncokWhos there - Java sockets

    To ( )

    (). :

    : Knock, knock!

    : Who's there?

    : (.. Atch)

    : ; (.. Atch who?)

    : (.. Bless you!)

    8

  • 7/25/2019 03 - 50 - 3 -

    9/50

    1..I - KKClient

    () . , , Bye. .

    , Client. Client

    ( {localhost, 5050})

    9

  • 7/25/2019 03 - 50 - 3 -

    10/50

    1..I - KKClient

    10

  • 7/25/2019 03 - 50 - 3 -

    11/50

    1..I - KKClient

    11

  • 7/25/2019 03 - 50 - 3 -

    12/50

    1..I - KKClient

    12

  • 7/25/2019 03 - 50 - 3 -

    13/50

    1..I - KKClient

    13

  • 7/25/2019 03 - 50 - 3 -

    14/50

    1..I - KKServer

    14

  • 7/25/2019 03 - 50 - 3 -

    15/50

    1..I - KKServer

    15

    listening

    socket

    socket /

    Protocol

    request/reply prtotocol

  • 7/25/2019 03 - 50 - 3 -

    16/50

    1..I - KKProtocol

    16

  • 7/25/2019 03 - 50 - 3 -

    17/50

    1..I - KKProtocol

    17

  • 7/25/2019 03 - 50 - 3 -

    18/50

    1..I - KKProtocol

    18

  • 7/25/2019 03 - 50 - 3 -

    19/50

    1..IConcurrent Server

    19

  • 7/25/2019 03 - 50 - 3 -

    20/50

    1..IConcurrent Server

    20

    thread

  • 7/25/2019 03 - 50 - 3 -

    21/50

    1..IConcurrent Server

    21

    2 Di ib d Obj d

  • 7/25/2019 03 - 50 - 3 -

    22/50

    2 - Distributed Objects andRemote Invocation

    Election :

    vote: ,

    (string) (integer).

    result:

    22

  • 7/25/2019 03 - 50 - 3 -

    23/50

    2.

    ) input output;

    5.3.1 Interfaces indistributed systems

    23

  • 7/25/2019 03 - 50 - 3 -

    24/50

    2.

    Vote: : Input

    : Input

    result:

    : Output

    : Output

    24

  • 7/25/2019 03 - 50 - 3 -

    25/50

    2.

    ) Election Java RMI

    Figure 5.16 .

    25

  • 7/25/2019 03 - 50 - 3 -

    26/50

    2.

    :

    class Result implements Serializable {

    String candidateName;

    int votes;

    }

    :

    import java.rmi.*;

    public interface Election extends Remote {

    void vote(String candidateName, int voterID)throws RemoteException;

    Result result() throws RemoteException;

    };

    26

  • 7/25/2019 03 - 50 - 3 -

    27/50

    2.

    ) Election

    . Election (Maybe invocation,At-Least-Once Invocation,At-Most-Once Invocation);

    5.3.1 Design Issues forRPC, call semantics

    27

  • 7/25/2019 03 - 50 - 3 -

    28/50

    2.

    H Maybe invocation At-Least-Once Invocation,

    , ,

    At-Most-Once Invocation,

    Java RMIAt-Most-Once Invocation

    28

  • 7/25/2019 03 - 50 - 3 -

    29/50

  • 7/25/2019 03 - 50 - 3 -

    30/50

    2.

    (voterID, candidateName)

    () vote,

    Hash Table

    Java Hashtable

    , ,

    semaphores monitors

    30

  • 7/25/2019 03 - 50 - 3 -

    31/50

    2.

    ) Election .

    )

    31

  • 7/25/2019 03 - 50 - 3 -

    32/50

    2.

    server ( ) (),

    .

    32

  • 7/25/2019 03 - 50 - 3 -

    33/50

    3 - (threads)

    ) threads, , ;

    7.4.3 Threads , Threads versus multiple processes.

    33

  • 7/25/2019 03 - 50 - 3 -

    34/50

    3

    thread switching thread

    thread

    , (.. thread-per-request )

    threads

    34

  • 7/25/2019 03 - 50 - 3 -

    35/50

    3 - (threads)

    ) file server caching (hit ratio) 80%. server 5ms CPU time, caching, 15 ms / .

    server ( /sec), :

    i) server (single-threaded)

    ii) o server -

    iii) server

    . 292-293 7.4.3 Threads .

    35

  • 7/25/2019 03 - 50 - 3 -

    36/50

    Average Request Processing Time (ARPT)

    Average Throughput(AT)i) server (single-threaded)

    ARPT = 0,8 * 5 + 0,2 * 20 = 8 ms

    AT= 1000/8 = 125reqs/sec

    ii) o server -

    (cache hit):(cache miss) 4:1

    / :

    25 ms 4 + 1

    ARPT = 25/5 = 5 ms

    AT = 1000/5 = 200 reqs/sec

    Req1 Req1(I/O) Req1(I/O) Req1(I/O)

    Req2 Req3 Req4 Req5

    3

    36

  • 7/25/2019 03 - 50 - 3 -

    37/50

    3

    iii) o server .

    tiM thread request / tiH thread request cache

    (1 cell = 5ms)

    20ms 5 reqs. :

    ARPT = 20/5 = 4 ms

    AT = 1000/4 = 250 reqs/sec

    37

    CPU1 t1M I/O I/O I/O

    CPU2 t2H t2H t2H t2H

  • 7/25/2019 03 - 50 - 3 -

    38/50

    4 -

    web-based . . :

    (entry ticket) ( )

    ( )

    38

  • 7/25/2019 03 - 50 - 3 -

    39/50

    4 -

    ( , ,

    , , ..), .

    , (web service provider)

    39

  • 7/25/2019 03 - 50 - 3 -

    40/50

    4: