You are on page 1of 50

3

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 B localhost

server

IP port number /

Fig. 4.5 TCPClient


TCP client makes connection to server, sends request and receives reply
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());}}
}
}

Fig. 4.6 TCPServer


TCP server makes a connection for each client and then echoes the

clients request
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 continued


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*/}}
}
}

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("Listen
socket:"+e.getMessage());}
}
}

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("Listen
socket:"+e.getMessage());}
}
}

1 - Java Sockets
) Knock Kncok Whos there?
Knock Kncok

Whos there
- Java sockets
To ( )
().
:
: Knock, knock!
: Who's there?
: (.. Atch)
:

; (.. Atch who?)


: (.. Bless you!)
8

1..I - KKClient
()


.
,
, Bye.
.

,
Client. Client
(
{localhost, 5050})

1..I - KKClient

10

1..I - KKClient

11

1..I - KKClient

12

1..I - KKClient

13

1..I - KKServer

14

1..I - KKServer
listening
socket


socket /

Protocol


request/reply prtotocol


15

1..I - KKProtocol

16

1..I - KKProtocol

17

1..I - KKProtocol

18

1..I Concurrent Server

19

1..I Concurrent Server



thread

20

1..I Concurrent Server

21

2 - Distributed Objects and


Remote Invocation

Election
:
vote: ,
(string)
(integer).

result:

22

2.
)
input output;
5.3.1
Interfaces in

distributed systems

23

2.
Vote:
: Input
: Input
result:
: Output
: Output

24

2.
) Election Java RMI
Figure 5.16
.

25

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

2.
) Election



.

Election (Maybe invocation, At-LeastOnce Invocation, At-Most-Once Invocation);
5.3.1 Design Issues for
RPC, call semantics
27

2.
H Maybe invocation
At-Least-Once Invocation,
,
,

At-Most-Once Invocation,

Java RMI

At-Most-Once Invocation

28

2.
)
Election

29

2.

(voterID, candidateName)
()
vote,

Hash Table

Java Hashtable

,

,
semaphores monitors

30

2.
) Election


.

)

31

2.
server (

)
(),

.




32

3 - (threads)
) threads,
,

;

7.4.3 Threads ,
Threads versus multiple processes.

33

thread switching
thread
thread
,
(.. thread-perrequest )
threads

34

3 - (threads)
) file server caching
(hit ratio) 80%.
server 5 ms CPU time, caching
, 15 ms
/ .
server ( /sec),
:
i) server (single-threaded)
ii) o server -

iii) server

. 292-293 7.4.3 Threads .


35

3
Average Request Processing Time (ARPT)
Average Throughput (AT)
i) server (single-threaded)
ARPT = 0,8 * 5 + 0,2 * 20 = 8 ms
AT= 1000/8 = 125 reqs/sec

ii) o server
(cache hit):(cache miss) 4:1
/ :
Req1

Req1(I/O)

Req1(I/O)

Req1(I/O)

Req2

Req3

Req4

25 ms 4 + 1
ARPT = 25/5 = 5 ms
AT = 1000/5 = 200 reqs/sec

Req5

36

3
iii) o server
.
tiM thread request /
tiH thread request cache
(1 cell = 5ms)
CPU1

t1M

I/O

I/O

I/O

CPU2

t2H

t2H

t2H

t2H

20ms
5 reqs. :
ARPT = 20/5 = 4 ms
AT = 1000/4 = 250 reqs/sec
37

4 -
web-based

.
.
:
(entry ticket)

( )


(
)

38

4 -

(
,
,
,
, ..),

.


,
(web service provider)

39

4:

- /
-
-/
-

-
-
-
( ),

-

40

4:

-
-

-
-
-

41

5: Pastry

?
?
?

(P2P)

e232fc

65a1fc

,
(overlay routing)
Pastry, b=4 ( GUIDs) L=8
.
GUID: 65a1fc
D: e232fc, :
GUIDs 3

; ( ?)

;

(
Figure 10.6 Distributed Systems)

4: Pastry
?
?

e232fc

65a1fc

0
n

1
n

2
n

3
n

4
n

5
n

7
n

8
n

9
n

a
n

b
n

c
n

d
n

e
n

f
n

60
n

61
n

62
n

63
n

64
n

65

66
n

67
n

68
n

69
n

6a
n

6b
n

6c
n

6d
n

6e
n

6f
n

650
n

651
n

652 653 654 655 656 657 658 659 65a 65b 65c 65d 65e
n
n
n
n
n
n
n
n
n
n
n
n

65f
n

65a0
n

65a1

65a2
n

65af
n

65a3
n

65a4
n

65a5
n

65a6
n

65a7
n

65a8
n

65a9
n

65aa
n

65ab
n

65ac
n

65ad
n

65ae
n

d462bc
d462ba

d4213f

e232fc

65a1fc

e13da3

0
n

1
n

2
n

3
n

4
n

5
n

6
n

7
n

8
n

9
n

a
n

b
n

c
n

d
n

f
n

e0
n

e1

e2
n

e3
n

e4
n

e5
n

e6
n

e7
n

e8
n

e9
n

ea
n

eb
n

ec
n

ed
n

ee
n

ef
n

e10
n

e11
n

e12
n

e13

e14
n

e15
n

e16
n

e17
n

e18
n

e19
n

e1a
n

e1b
n

e1c
n

e1d
n

e1e
n

e1f
n

e130
n

e131
n

e132
n

e133
n

e134
n

e135
n

e136 e137
n
n

e13a e13b e13c e13d e13e


n
n
n
n

e13f
n

e138 e139
n
n

d462bc
d462ba

e20ff4

e232fc

e13da3

65a1fc
45

0
n

1
n

2
n

3
n

4
n

5
n

6
n

7
n

8
n

9
n

a
n

b
n

c
n

d
n

f
n

e0
n

e1
n

e2

e3
n

e4
n

e5
n

e6
n

e7
n

e8
n

e9
n

ea
n

eb
n

ec
n

ed
n

ee
n

ef
n

e20

e21
n

e22
n

e23 e24 e25 e26 e27 e28 e29 e2a e2b e2c e2d e2e
n
n
n
n
n
n
n
n
n
n
n
n

e2f
n

e200

e201
n

e20
2
n

e203
n

e204

e20
5
n

e206

e207

e208

e209
n

e20a

e20b

e20c e20d
n
n

e20
e
n

e20f

d462bc
e23b3a

e20ff4

e232fc

e13da3

65a1fc
46

O GUID e232fc
e23b3a -
(leaf nodes)

e232fc
e23b3a

e20ff4

e232fc

e13da3

65a1fc
47

5: Pastry
,

4

log2b N (= log16 2128 =
32) ( 128-bit GUIDs)

(circular routing),
,
L/2 = 4
(e232fc-65a1fc)/4
.

48

,
([1], [2], )



/ ,
( )

(
)
MS Word
/ (. Caption Cross-Reference)
49


:
:

[1].
GUIDE [2].
:
[1] . Abowd, C. Atkeson, J. Hong, Cyberguide: A Mobile Context-Aware Tour
Guide, Wireless Networks, 3(5):421433, 1996.
[2] K. Cheverst, N. Davies, K. Mitchell, A. Friday, Experiences of Developing
and Deploying a Context-Aware Tourist Guide: The GUIDE Project,
Proceedings of the 6th ACM/IEEE International Conference on Mobile
Computing and Networking (Mobicom2000): 2031, 2000.

50

You might also like