Вы находитесь на странице: 1из 57

Socket-based

Distributed Systems

Fabienne Boyer, fabienne.boyer@imag.fr

Adaptation du cours d’Olivier Gruber, LIG


Outline
l  Introduction to sockets

l  Point-to-point communication with TCP sockets

l  Point-to-point communication with UDP sockets

l  Group communication with sockets

l  Client-server programming with sockets

Fabienne Boyer, Basics of Distributed Programming 2


Internet Protocol (IP)
l  IP (Internet Protocol)‫‏‬
l  The Internet protocol is not the Web
l  Corresponds to the network layer of the OSI model
l  Addressing, routing and transport of data packets

l  IP addresses are names


l  4 bytes (IPv4), naming a host machine (e.g. 192.168.2.100)‫‏‬
l  IP addresses are location dependent

Fabienne Boyer, Basics of Distributed Programming 3


DNS
l  Host name to IP Address resolution
l  Using a network naming service
l  DNS (Domain Name System)

l  Discussing DNS


l  A fairly complex world-wide distributed system
l  Allows name aliases (multiple names for an address)
l  And the reverse (multiple adresses for a name)
l  Organized as hierarchical zones across the world
l  A zone is managed by a DNS server
l  Replicated servers for high availability

Fabienne Boyer, Basics of Distributed Programming 4


IP addressing in Java
l  InetAddress class
l  Represents an IP address
l  Either a 32- or 128-bit unsigned number used by IP

l  No public constructor


l  static InetAddress getLocalHost()
§  Returns the local host
l  static InetAddress getByName(String hostname);
§  Lookup a host machine by name

l  Some API elements


l  String getHostAddress()
§  Returns the IP address in textual presentation.
§  E.g., 129.250.35.250
l  String getHostName()
§  Gets the host name for this IP address.
§  E.g. hoff.e.ejf-grenoble.fr

Fabienne Boyer, Basics of Distributed Programming 5


Ports
l  Ports are end points
l  For communication channels over IP
l  An IP address and a port names an end point
l  Communication Channel
l  Between two ports, allocated to two processes
l  Port numbers are managed by the operating system
l  Many important services have a standardized port
l  Example: port 513 for rlogin, 25 for telnet
l  Ports between 1 and 1023 are well-known
l  Ports between 1024 and 49151 are registered
l  Port numbers are allocated on-demand to processes
l  Only one process may be allocated a port

Fabienne Boyer, Basics of Distributed Programming 6


Ports in Java
l  SocketAddress
l  Abstract class for a socket address
l  Independent of any specific addressing protocol

l  InetSocketAddress
l  Represents a socket address over IP
l  Essentially an IP address and a port
l  For example 129.250.35.250 and port 80

l  A hostname can be used instead of an IP address


l  It will be looked up using InetAddress.getByName(String)‫‏‬

SocketAddress

InetSocketAddress

Fabienne Boyer, Basics of Distributed Programming 7


Sockets
l  A distributed programming model based on messages
l  Two protocols over IP
l  TCP
l  Stream oriented
l  Lossless
l  Ordered
l  Connection-oriented

l  UDP
l  Packet based
l  Packets may be lost or reordered
l  Efficient

Fabienne Boyer, Basics of Distributed Programming 8


Sockets
l  Typical applications
l  TCP
l  Do not accept loss or reordering
l  Examples:
•  Transferring files (ftp for instance)‫‏‬
•  Downloading web pages

l  UDP
l  Require high bandwidth, can accept loss or reordering
l  Examples:
•  Transmission of video/sound in real time
•  Out of sequence or incomplete frames are just dropped
•  Other more complex communication protocols
•  Such as totally-ordered multicast using Lamport's logical clocks

Fabienne Boyer, Basics of Distributed Programming 9


Outline
l  Introduction to sockets

l  Point-to-point communication with TCP sockets

l  Point-to-point communication with UDP sockets

l  Group communication

l  Client-server programming based on sockets

Fabienne Boyer, Basics of Distributed Programming 10


TCP Sockets – Steps Involved
l  Server side
l  Creation of the server process
l  Allocate a port
l  The port is granted by the operating system
l  Associate the port with a socket
l  Server waits for incoming connections

"server port"
"server socket "

"server process"

4320

Server Machine

Fabienne Boyer, Basics of Distributed Programming 11


TCP Sockets – Steps Involved
l  Client side
l  Creation of the client process
l  Request a connection to the remote port
§  A port is allocated
§  A socket is associated with the port
§  The connection is requested to the server

"server port"
"client port" "server socket "
"client socket "
"client proces "

"server process"

36243 connection request 4320

Client Machine Server Machine

Fabienne Boyer, Basics of Distributed Programming 12


TCP Sockets – Steps Involved
l  Server side
l  Accept the connection from the client
§  A port is allocated
§  A socket is associated with the port
§  The connection is established between the client and the server

server port
server socket

"client port"
"client socket "
"client proces "
4320

36243 connection request

Client Machine Server Machine

Fabienne Boyer, Basics of Distributed Programming 13


TCP Sockets – Steps Involved

client port
client socket server port
server socket
connection request

36243
4320
Client 1

56789 Server

Client 2

Fabienne Boyer, Basics of Distributed Programming 14


Java classes related to TCP sockets

l  The java.net package provides


l  ServerSocket class
l  Socket class
l  ServerSocket class
l  Represent a socket on a server
l  Accept incoming TCP connections
l  Socket class
l  Represent the end points
l  Both on server and client sides

Fabienne Boyer, Basics of Distributed Programming 15


Example of Java sockets/TCP
import java.net.*;

int port = 1234;

// Create a server socket associated with port 1234

ServerSocket server = new ServerSocket(port);

// End-less loop

while (true) {
// Server waits for a connection
Socket client = server.accept();
// A client connected
System.out.println("Client " + client.getInetAddress() + “connected.");
// Server receives bytes from client
...

Fabienne Boyer, Basics of Distributed Programming 16


Example of Java sockets/TCP
import java.net.*;

int port = 1234; import java.net.*;


// Create a server socket associated with
String port
serverHost = “goedel.imag.fr";
1234
int serverPort = 1234;
ServerSocket server = new ServerSocket(port);
// Client connects to server
// End-less loop

while (true) { Socket server;


// Server waits for a connection server = new Socket(serverHost,serverPort);
Socket client = server.accept();
// Client connected
// A client connected
System.out.println("Client " + System.out.println("Connected to " +server.getInetAddress());
client.getInetAddress() + " connected.");
// Client sends bytes to server
// Server receives bytes from client
... ...
}

Fabienne Boyer, Basics of Distributed Programming 17


Streams
l  Definition
l  Streams are an abstraction for arbitrary data streams
l  Input streams used to receive (i.e. read) bytes
l  Output streams used to send (i.e. write) bytes

l  Examples
l  Streams from/to a socket
l  Streams from/to a file
l  Streams from/to the console

Fabienne Boyer, Basics of Distributed Programming 18


Streams in Java
l  The java.io package contains the following classes related to streams

l  InputStream / OutputStream


abstract classes that represent streams of bytes

l  DataInputStream / DataOutputStream


To manipulate streams of Java primary types

l  ObjectInputStream / ObjectOutputStream


To manipulate streams of Java objects

l  FileInputStream / FileOutputStream


To read data from a file or write data to a file

l  FilterInputStream / FilterOutputStream


To transform data along the way

Fabienne Boyer, Basics of Distributed Programming 19


Java Sockets and Streams
import java.io.*;!
!
..."
// End-less loop!
while (true) {!
! !// Server waits for a connection!
!Socket client = server.accept();!
""
// A client connected!
"System.out.println("Client " + client.getInetAddress() + " connected.");
!
!// Get the server’s output stream!
!OutputStream os = client.getOutputStream();!
!
!// Build data and transform it to bytes!
!Date date = new Date();!
!byte[] b = date.toString().getBytes();!
!
!// Send the date to the client (write in output stream)!
!os.write(b);!
}"

Fabienne Boyer, Basics of Distributed Programming 20


Java Sockets and Streams
import java.io.*;"
!
..." import java.io.*;!
// End-less loop! !
while (true) {" ...!
// Client connects to server!
"System.out.println("Waiting for client…");"
"// Server waits for a connection!Socket server = new Socket(serverHost, serverPort);!
"Socket client = server.accept();"!
"" // Get the client’s input stream!
// Get the server’s output stream! InputStream is = server.getInputStream();!
"OutputStream os = client.getOutputStream();"
!
" // Read data from input stream!
"// Build data and transform it to bytes!
byte[] b = new byte[100];!
"Date date = new Date();" int num = is.read(b);!
"byte[] b = date.toString().getBytes();"
!
"
// Transform data from ^bytes to String!
"// Write in output stream!
"os.write(b);" String date = new String(b);!
}" System.out.println("Server said: " + date);!

Fabienne Boyer, Basics of Distributed Programming 21


Java Sockets and Streams
// Get the server's output stream
OutputStream os =
client.getOutputStream();
Date date = new Date();
byte[] b = date.toString().getBytes();
os.write(b);

Correct?

// Get the client’s input stream


InputStream is =
server.getInputStream();
byte[] b = new byte[100];
int num = is.read(b);
String date = new String(b);

Fabienne Boyer, Basics of Distributed Programming 22


Java Sockets and Streams
l  Maybe, Maybe Not
l  Assumes: // Get the server's output stream
OutputStream os = client.getOutputStream();
l  Nothing is left in the input stream Date date = new Date();
byte[] b = date.toString().getBytes();
before reading os.write(b);
l  Just not always true
l  Some previous read might have // Get the client’s input stream
left something... InputStream is = server.getInputStream();
l  Beware of IP data packets… byte[] b = new byte[100];
int num = is.read(b);
String date = new String(b);

Socket stream

IP packets
Fabienne Boyer, Basics of Distributed Programming 23
Java Sockets and Streams

l  Maybe, Maybe Not // Get the server's output


l  Assumes: stream
OutputStream os =
l  The entire string has been client.getOutputStream();
received when reading Date date = new Date();
byte[] b =
l  Just not always true date.toString().getBytes();
l  When sending variable length os.write(b); Correct?
data structures, send the length
l  How can we do that?
§  Length is an integer
§  We can send bytes
// Get the client’s input stream
InputStream is =
server.getInputStream();
byte[] b = new byte[100];
int num = is.read(b);
String date = new String(b);
Fabienne Boyer, Basics of Distributed Programming 24
Java Sockets and Streams
// Get the server's output stream
OutputStream os;
l  DataOutputStream DataOutputStream dos;
os = client.getOutputStream();
l  Provides type-aware operations dos = new DataOutputStream(os);
l  Endianness proof ! Date date = new Date();
byte[] b =
l  But introduces an overhead date.toString().getBytes();
dos.writeInt(b.length);
dos.write(b);

Correct?

// Get the client’s input stream


InputStream is;
DataInputStream dis;
is = server.getInputStream();
dis = new DataInputStream(is);
int length = dis.readInt();
byte[] b = new byte[length];
int num = dis.read(b,0,length);
String date = new String(b);
Fabienne Boyer, Basics of Distributed Programming 25
Java Sockets and Streams
// Get the server's output
stream
l  DataOutputStream OutputStream os;
DataOutputStream dos;
l  Provides type-aware operations os = client.getOutputStream();
dos = new DataOutputStream(os);
l  Endianness proof !
l  But introduces an overhead Date date = new Date();
byte[] b =
date.toString().getBytes();
dos.writeInt(b.length);
dos.write(b); Correct?

// Get the client’s input stream


InputStream is;
DataInputStream dis;
is = server.getInputStream();
dis = new DataInputStream(is);
int length = dis.readInt();
byte[] b = new byte[length];
Fabienne Boyer, Basics of Distributed Programming int num = dis.read(b,0,length);
26
Java Sockets and Streams
l  String Encoding...
// Get the server's output stream
l  Depends on your string encoding... OutputStream os;
DataOutputStream dos;
l  Better to use UTF-8 os = client.getOutputStream();
dos = new DataOutputStream(os);
l  DataOutputStream.writeUTF(b)‫‏‬
Date date = new Date();
l  How about now? String str = date.toString();
dos.writeUTF(str);
l  Yes, but keep in mind that any
variable length data structure needs
Correct?
to be prefixed with its length...

// Get the client’s input stream


InputStream is;
DataInputStream dis;
is = server.getInputStream();
dis = new DataInputStream(is);
String date = dis.readUTF();

Fabienne Boyer, Basics of Distributed Programming 27


Java Sockets and Streams
We could also use Java Object streams...

import java.io.*;
...
while (true) {

// Server waits for a connection


Socket client = server.accept();
// A client connected
System.out.println("Client " + client.getInetAddress()
+ " connected.");

// Get the server’s output stream


OutputStream os = client.getOutputStream();

// Get the associated object output stream


ObjectOutputStream oos = new ObjectOutputStream(os);

// Write object in output stream


Date date = new Date();
oos.writeObject(date);
// Close output stream
oos.close();
}
Fabienne Boyer, Basics of Distributed Programming 28
Java Object Streams
Looking at both server and client sides...
import java.io.*;
import java.io.*;
...
// End-less loop ...
while (true) { // Client connects to server
System.out.println("Waiting
Socketfor client…");
server = new Socket(serverHost,
// Server waits for a connection
serverPort);
Socket client = server.accept();
// Client connected
// A client connected
System.out.println("Connected
System.out.println("Client to " +
" + client.getInetAddress() + " connected.");
server.getInetAddress());
// Get the server’s output stream
OutputStream os = client.getOutputStream();
// Get the associated object output //stream
// Get the client’s input stream
ObjectOutputStream oos = new
InputStream is = server.getInputStream();
ObjectOutputStream(os);
// Get
// Write object in output the associated object input stream
stream
Date date = new Date();
ObjectInputStream ois = new
oos.writeObject(date);
ObjectInputStream(is);
// Close output stream
oos.close();
} // Read object from input stream
Date date = (Date) ois.readObject();

System.out.println("Server said: " + date);


// Close input stream
ois.close();
Fabienne Boyer, Basics of Distributed Programming 29
Java Object Streams
l  Based on the Java Serialization framework
l  Any class may implement the Serializable interface
l  If it does, instances of that class can be serialized
l  Serialization is a deep copy
l  Recursive serialization along object references
l  Sharing is respected
l  Warning
l  Deep copy does not stop by itself
§  If any object encountered is not serializable, an exception is thrown
§  Most JRE classes are serializable
l  Possible control point
§  References in classes may be declared transient (easy)‫‏‬
§  Redefine how instances of a class are serialized (harder)‫‏‬

Fabienne Boyer, Basics of Distributed Programming 30


Outline
l  Introduction to sockets

l  Addressing

l  Point-to-point communication with TCP sockets

l  Point-to-point communication with UDP sockets

l  Group communication

Fabienne Boyer, Basics of Distributed Programming 31


Java sockets over UDP
l  Networking in the unconnected mode
l  UDP protocol provides a mode of network communication
whereby applications send packets of data, called datagrams, to
one another.

l  A datagram is an independent self-contained message sent over


the network whose arrival, arrival time, and order are not
guaranteed.

l  Some applications that communicate over the network do not


require reliable, point-to-point channel provided by TCP

l  Applications might benefit from a mode of communication that


delivers independent packages of information whose arrival and
order of arrival are not guaranteed

Fabienne Boyer, Basics of Distributed Programming 32


Java classes related to UDP
l  java.net.DatagramPacket
l  Represents a data packet
l  Essentially a byte buffer
l  Maximum buffer length is known by calling DatagramSocket.
getReceiveBufferSize()‫‏‬
l  Includes an InetAddress and port number

l  java.net.DatagramSocket
l  Used for sending and receiving datagram packets
l  Communication happens over UDP
l  Send a DatagramPacket by calling send on a DatagramSocket
l  Receive a DatagramPacket by calling receive on a DatagramSocket

Fabienne Boyer, Basics of Distributed Programming 33


Example of Java sockets/UDP
import java.net.*;

int port = 1234;


// Create a datagram socket associated with the server port
DatagramSocket serverSock = new DatagramSocket(port);

// End-less loop
while (true) {
System.out.println("Waiting for client packet…");
byte[] buf = new byte[256‫;]‏‬
// Create a datagram packet
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// Wait for a packet
serverSock.receive(packet);

// Get client IP address and port number


InetAddress clientAddr = packet.getAddress();
int clientPort = packet.getPort();
// Build a response
initialize buf ...
// Build a datagram packet for response
packet = new DatagramPacket(buf, buf.length, clientAddr, clientPort);
// Send a response
serverSock.send(packet);
}

Fabienne Boyer, Basics of Distributed Programming 34


Example of Java sockets/UDP
import java.net.*;
import java.net.*; int serverPort = 1234;
String serverHost = ...;
int port = 1234;
// Create a datagram socket associated
// Create a datagram socket
// with the server port DatagramSocket clientSock = new DatagramSocket();
DatagramSocket serverSock = newbuf
byte[] DatagramSocket(port);
= new byte[256];
// End-less loop // Get server’s IP address
while (true) { InetAddress serverAddr=InetAddress.getByName(serverHost);
System.out.println("Waiting
// Buildfor client packet…");
a request
byte[] buf = new byte[256];
initialize buf ...
// Create a datagram //
packet
Create a datagram packet destined for the server
DatagramPacket packetDatagramPacket
= new packet = new DatagramPacket(buf,
DatagramPacket(buf, buf.length);
buf.length, serverAddr, serverPort);
// Wait for a packet // Send datagram packet to server
serverSock.receive(packet);
clientSock.send(packet);
// Get client IP address and port
// Build number packet for response
a datagram
InetAddress clientAddr = packet.getAddress();
packet int clientPort
= new DatagramPacket(buf, =
buf.length);
packet.getPort();
// Receive response
// Build a response
clientSock.receive(packet);
initialize buf ...
String received = new String(packet.getData(), 0,
// Build a datagram packet for response
packet.getLength());
packet = new DatagramPacket(buf, buf.length, clientAddr,
System.out.println("Response: clientPort);
" + received);
// Send a response
serverSock.send(packet);
}
Fabienne Boyer, Basics of Distributed Programming 35
Outline
l  Introduction to sockets

l  Addressing

l  Point-to-point communication with TCP sockets

l  Point-to-point communication with UDP sockets

l  Group communication

Fabienne Boyer, Basics of Distributed Programming 36


Java Multicast

l  Based on UDP sockets


l  Datagram packets (same as before)
l  MulticastSocket extends DatagramSocket

l  Relies on IP-level multicast


l  Multicast IP adresses
l  Class D addresses are reserved for multicast
l  In the range 224.0.0.0 to 239.255.255.255
l  A multicast group
l  A multicast address and port

Fabienne Boyer, Basics of Distributed Programming 37


Java Multicast

l  Multicasting to a group


l  Need to create a datagram packet
l  Destination is the group InetAddress and port
l  Normal UDP send

l  Joigning a multicast group


l  Create the multicast socket with the group port
l  Need to join the multicast group, use the multicast address
l  Receive messages multicasted to the group

l  Leaving a multicast group


l  Explicit departure
Fabienne Boyer, Basics of Distributed Programming 38
Java multicast - receiving
import java.net.*;

// Multicast group
int groupPort = 5000;
InetAddress groupAddr = InetAddress.getName(“225.4.5.6”);

// Create the socket


MulticastSocket sock = new MulticastSocket(groupPort);
sock.joinGroup(groupAddr);

// Create a datagram packet and do a receive


byte buf[] = new byte[1234];
DatagramPacket packet = new DatagramPacket(buf, buf.length);

// Wait for a packet


sock.receive(packet);

// When done, leave the multicast group and close the socket
sock.leaveGroup(groupAddr);
sock.close();

Fabienne Boyer, Basics of Distributed Programming 39


Java multicast - sending
import java.net.*;

// Multicast group
int groupPort = 5000;
InetAddress groupAddr = InetAddress.getName(“225.4.5.6”);

// Create the socket


// but we don’t bind it and we don’t join the multicast group
MulticastSocket sock = new MulticastSocket();

byte buf[] = new byte[10];


For (int i=0; i<buf.lenght; i++)
buf[i] = (byte)i;

// Create a datagram packet and do a receive


DatagramPacket packet = new DatagramPacket(buf, buf.length,
groupAddr, groupPort);

// Send the packet


Byte ttl = 1;
sock.send(packet, ttl);

// When doneclose the socket


sock.close();
}
Fabienne Boyer, Basics of Distributed Programming 40
Java Multicast
l  Caveats
l  IP multicast is supported by many routers
l  But most Internet providers forbid IP multicast
l  Over the public Internet, multicast is simply not available
l  Usable on local LAN
l  Normal UDP send

l  Middleware multicast


l  Multicast can be built above IP or UDP or TCP/IP
l  Using point to point messages
l  Middleware provides
l  Group management and memberships
l  From simple groups to publish-subscribe topics

l  Other multicast properties


l  Totally ordered multicast, reliable multicast
Fabienne Boyer, Basics of Distributed Programming 41
Client-Server based on sockets
"server port"
"client port" ”server socket "
"client socket "

36243 4320

server

client request request


requests selection processing server
request queue response

Fabienne Boyer, Distributed Programming 42


Server Design
l  Request processing model

Sequential / Parallel / Replicated


May cache request results

server

client request request server


requests selection processing responses
request queue

Fabienne Boyer, Distributed Programming 43


Sequential processing

while (true) {
receive(client_id,message);
extract(message, service_id, params);
results = do_service(service_id, params);
send(client_id, results);
}

server

client request request


requests selection processing server
request queue response

Fabienne Boyer, Distributed Programming 44


Design choices for a parallel server
l  Advantages
Thread-based l  Light-weight resources
l  Memory sharing
l  Drawbacks
l  Limited isolation

l  Advantages
l  Strong isolation
l  Real parallelism (also with kernel threads)
Process-based
l  Drawbacks
l  Heavy resources

Fabienne Boyer, Distributed Programming 45


Multi-threaded server

server

main thread worker thread


client request request
requests selection processing
request queue

response

Fabienne Boyer, Distributed Programming 46


Multi-threaded server UDP-based

while (true) { Program executed by thread thr:


receive(client_id,message); results = do_service(
extract(message, service_id, service_id, params);
params); send(client_id, results);
thr = create_thread(client_id, exit()
service_id, params);
}
server

main thread worker thread


client request request
requests selection processing
request queue

response
Fabienne Boyer, Distributed Programming 47
Multi-threaded server – TCP-based
Program executed by thread thr:
Socket s;
s.receive(client_id,message);

while (true) { extract(message, service_id,


params);
Socket s;
results = do_service(
s = accept(client_id,message); service_id, params);
thr = create_thread(s); send(client_id, results);
} close()
exit()
server

main thread worker thread


client request request
requests selection processing
request queue

response
Fabienne Boyer, Distributed Programming 48
Multi-threaded server (thread-pool)

server

main thread
client request
requests selection
request queue worker thread
worker thread
request
worker thread
request
processing
work_to_do processing
request
processing

response
49

Fabienne Boyer, Distributed Programming


Multi-threaded server (pool) – UDP-
based

while (true) { Pool of threads:


receive(client_id,message); while (true) {
extract(message, service_id, work_to_do.get(
params); client_id, service_id,
work_to_do.put(client_id, params);
service_id,params); results = do_service(
} service_id, params);
send(client_id, results);
}

Fabienne Boyer, Distributed Programming 50


Multi-threaded server (pool) – TCP-
based

Pool of threads:
Socket s;
while (true) {
while (true) {
s = work_to_do.get();
Socket s;
s.receive(client_id,message);
s = accept(client_id,message);
extract(message, service_id,
work_to_do.put(s);
params);
}
results = do_service(
service_id, params);
send(client_id, results);
close();
}

Fabienne Boyer, Distributed Programming 51


Multi-threaded clients

l  Some clients are single-threaded programs but rarely


l  Let’s discuss a simple GUI-based client
l  Graphical User Interface
l  Most widget toolkits are single-threaded
l  Imposes that clients are multi-threaded
l  Otherwise, blocking on a socket freezes the GUI
l  Examples
l  Eclipse Rich Platform, Google Android
l  Use background threads to carry out potentially blocking invocations
such as socket read / write
l  Post events on the GUI thread

Fabienne Boyer, Basics of Distributed Programming 52


Multi-threaded servers with Java

l  Integrated into the language


l  Creation / Execution
l  Synchronization mechanisms

l  One process, the JVM

l  User vs Kernel threads

Fabienne Boyer, Basics of Distributed Programming 53


Thread (Reminder)
l  Two ways
l  Extends Thread
l  Implements Runnable

public class HelloThread extends Thread {

public void run() {


System.out.println("Hello from a thread!");
}

public static void main(String args[]) {


(new HelloThread()).start();
}

This example is taken from Java Thread tutorial

Fabienne Boyer, Basics of Distributed Programming 54


Thread (Reminder)
l  Another way is to use the Runnable interface

public class HelloRunnable implements Runnable {

public void run() {


System.out.println("Hello from a thread!");
}

public static void main(String args[]) {


(new Thread(new HelloRunnable())).start();
}

l  Be careful, directly calling the run method does not create a
thread

Fabienne Boyer, Basics of Distributed Programming 55


Multi-process servers

l  Parallelize the processing of requests on a pool of processes


l  Static vs dynamic pool
l  Request dispatching through a load-balancer

l  Load-balancer needs to communicate with the slaves servers


l  Use socket or higher level communication layer
l  Need to detect and manage failures

Fabienne Boyer, Basics of Distributed Programming 56


References

This lecture is built from:

l  Sun Microsystems. Java Tutorial on Networking.


http://java.sun.com/docs/books/tutorial/networking/

l  Sun Microsystems. Java Tutorial on Concurrency.


http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

l  M. Boger. Java in Distributed Systems: Concurrency, Distribution and


Persistence. Wiley, 2001.

Fabienne Boyer, Basics of Distributed Programming 57

Вам также может понравиться