Socket-based Distributed Systems - LIG...
Transcript of Socket-based Distributed Systems - LIG...
![Page 1: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/1.jpg)
Socket-based Distributed Systems
Fabienne Boyer, [email protected]
Adaptation du cours d’Olivier Gruber, LIG
![Page 2: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/2.jpg)
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
![Page 3: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/3.jpg)
Fabienne Boyer, Basics of Distributed Programming 3
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
![Page 4: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/4.jpg)
Fabienne Boyer, Basics of Distributed Programming 4
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
![Page 5: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/5.jpg)
Fabienne Boyer, Basics of Distributed Programming 5
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
![Page 6: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/6.jpg)
Fabienne Boyer, Basics of Distributed Programming 6
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
![Page 7: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/7.jpg)
Fabienne Boyer, Basics of Distributed Programming 7
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
![Page 8: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/8.jpg)
Fabienne Boyer, Basics of Distributed Programming 8
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
![Page 9: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/9.jpg)
Fabienne Boyer, Basics of Distributed Programming 9
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
![Page 10: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/10.jpg)
Fabienne Boyer, Basics of Distributed Programming 10
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
![Page 11: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/11.jpg)
Fabienne Boyer, Basics of Distributed Programming 11
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 Machine
"server socket "
4320
"server port"
"server process"
![Page 12: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/12.jpg)
Fabienne Boyer, Basics of Distributed Programming 12
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
Client Machine
"client socket "
36243
"client proces "
Server Machine
"server socket "
4320
"server port"
"server process"
"client port"
connection request
![Page 13: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/13.jpg)
Fabienne Boyer, Basics of Distributed Programming 13
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
Client Machine
"client socket "
36243
"client proces " "client port"
connection request
Server Machine
server socket
4320
server port
![Page 14: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/14.jpg)
Fabienne Boyer, Basics of Distributed Programming
14
TCP Sockets – Steps Involved
Client 1
client socket
36243
Server
server socket
4320
server port client port
connection request
Client 2
56789
![Page 15: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/15.jpg)
Fabienne Boyer, Basics of Distributed Programming 15
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
![Page 16: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/16.jpg)
Fabienne Boyer, Basics of Distributed Programming 16
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 ...
}
![Page 17: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/17.jpg)
Fabienne Boyer, Basics of Distributed Programming 17
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 ...
}
Example of Java sockets/TCP
import java.net.*;
String serverHost = “goedel.imag.fr";
int serverPort = 1234;
// Client connects to server
Socket server;
server = new Socket(serverHost,serverPort);
// Client connected
System.out.println("Connected to " +server.getInetAddress());
// Client sends bytes to server
...
![Page 18: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/18.jpg)
Fabienne Boyer, Basics of Distributed Programming 18
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
![Page 19: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/19.jpg)
Fabienne Boyer, Basics of Distributed Programming 19
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
![Page 20: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/20.jpg)
Fabienne Boyer, Basics of Distributed Programming 20
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);!
}"
![Page 21: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/21.jpg)
Fabienne Boyer, Basics of Distributed Programming 21
import java.io.*;"!..."// End-less loop!while (true) {"
"System.out.println("Waiting for client…");""// Server waits for a connection!"Socket client = server.accept();"""
// 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();"
""// Write in output stream!"os.write(b);"
}"
Java Sockets and Streams
import java.io.*;!!...!// Client connects to server!Socket server = new Socket(serverHost, serverPort);!!// Get the client’s input stream!InputStream is = server.getInputStream();!!// Read data from input stream!byte[] b = new byte[100];!int num = is.read(b);!!// Transform data from ^bytes to String!String date = new String(b);!System.out.println("Server said: " + date);!
![Page 22: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/22.jpg)
Fabienne Boyer, Basics of Distributed Programming 22
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); // 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);
Correct?
![Page 23: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/23.jpg)
Fabienne Boyer, Basics of Distributed Programming 23
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);
// 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);
l Maybe, Maybe Not l Assumes:
l Nothing is left in the input stream before reading
l Just not always true l Some previous read might have
left something... l Beware of IP data packets…
Socket stream
IP packets
![Page 24: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/24.jpg)
Fabienne Boyer, Basics of Distributed Programming 24
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); // 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);
Correct?
l Maybe, Maybe Not l Assumes:
l The entire string has been received when reading
l Just not always true l When sending variable length
data structures, send the length l How can we do that?
§ Length is an integer § We can send bytes
![Page 25: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/25.jpg)
Fabienne Boyer, Basics of Distributed Programming 25
Java Sockets and Streams
// Get the server's output stream OutputStream os; DataOutputStream dos; os = client.getOutputStream(); dos = new DataOutputStream(os); Date date = new Date(); byte[] b = date.toString().getBytes(); dos.writeInt(b.length); dos.write(b); // 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);
l DataOutputStream l Provides type-aware operations l Endianness proof ! l But introduces an overhead
Correct?
![Page 26: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/26.jpg)
Fabienne Boyer, Basics of Distributed Programming 26
Java Sockets and Streams
// Get the server's output stream OutputStream os; DataOutputStream dos; os = client.getOutputStream(); dos = new DataOutputStream(os); Date date = new Date(); byte[] b = date.toString().getBytes(); dos.writeInt(b.length); dos.write(b); // 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);
l DataOutputStream l Provides type-aware operations l Endianness proof ! l But introduces an overhead
Correct?
![Page 27: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/27.jpg)
Fabienne Boyer, Basics of Distributed Programming 27
Java Sockets and Streams
// Get the server's output stream OutputStream os; DataOutputStream dos; os = client.getOutputStream(); dos = new DataOutputStream(os); Date date = new Date(); String str = date.toString(); dos.writeUTF(str);
// Get the client’s input stream InputStream is; DataInputStream dis; is = server.getInputStream(); dis = new DataInputStream(is); String date = dis.readUTF();
l String Encoding... l Depends on your string encoding...
l Better to use UTF-8 l DataOutputStream.writeUTF(b)
l How about now? l Yes, but keep in mind that any
variable length data structure needs to be prefixed with its length... Correct?
![Page 28: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/28.jpg)
Fabienne Boyer, Basics of Distributed Programming 28
Java Sockets and 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();
}
We could also use Java Object streams...
![Page 29: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/29.jpg)
Fabienne Boyer, Basics of Distributed Programming 29
import java.io.*; ... // End-less loop while (true) {
System.out.println("Waiting for client…"); // 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();
}
Java Object Streams
import java.io.*; ... // Client connects to server Socket server = new Socket(serverHost, serverPort); // Client connected System.out.println("Connected to " + server.getInetAddress()); // Get the client’s input stream InputStream is = server.getInputStream(); // Get the associated object input stream ObjectInputStream ois = new ObjectInputStream(is); // Read object from input stream Date date = (Date) ois.readObject(); System.out.println("Server said: " + date); // Close input stream ois.close();
Looking at both server and client sides...
![Page 30: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/30.jpg)
Fabienne Boyer, Basics of Distributed Programming 30
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)
![Page 31: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/31.jpg)
Fabienne Boyer, Basics of Distributed Programming 31
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
![Page 32: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/32.jpg)
Fabienne Boyer, Basics of Distributed Programming 32
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
![Page 33: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/33.jpg)
Fabienne Boyer, Basics of Distributed Programming 33
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
![Page 34: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/34.jpg)
Fabienne Boyer, Basics of Distributed Programming 34
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);
}
![Page 35: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/35.jpg)
Fabienne Boyer, Basics of Distributed Programming 35
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);
}
Example of Java sockets/UDP import java.net.*; int serverPort = 1234; String serverHost = ...; // Create a datagram socket DatagramSocket clientSock = new DatagramSocket(); byte[] buf = new byte[256]; // Get server’s IP address InetAddress serverAddr=InetAddress.getByName(serverHost); // Build a request initialize buf ... // Create a datagram packet destined for the server DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, serverPort); // Send datagram packet to server clientSock.send(packet); // Build a datagram packet for response packet = new DatagramPacket(buf, buf.length); // Receive response clientSock.receive(packet); String received = new String(packet.getData(), 0, packet.getLength()); System.out.println("Response: " + received);
![Page 36: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/36.jpg)
Fabienne Boyer, Basics of Distributed Programming 36
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
![Page 37: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/37.jpg)
Fabienne Boyer, Basics of Distributed Programming 37
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
![Page 38: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/38.jpg)
Fabienne Boyer, Basics of Distributed Programming 38
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
![Page 39: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/39.jpg)
Fabienne Boyer, Basics of Distributed Programming 39
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(); }
![Page 40: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/40.jpg)
Fabienne Boyer, Basics of Distributed Programming 40
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(); }
![Page 41: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/41.jpg)
Fabienne Boyer, Basics of Distributed Programming 41
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
l Leaving a multicast group l Explicit departure
![Page 42: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/42.jpg)
Fabienne Boyer, Distributed Programming 42
Client-Server based on sockets
request processing
request selection
request queue
client requests server
response
server
"client socket "
36243
"client port"
4320
"server port" ”server socket "
![Page 43: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/43.jpg)
Fabienne Boyer, Distributed Programming 43
Server Design l Request processing model
Sequential / Parallel / Replicated May cache request results
request processing
request selection
request queue
client requests
server responses
server
![Page 44: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/44.jpg)
Fabienne Boyer, Distributed Programming 44
Sequential processing
while (true) {
receive(client_id,message);
extract(message, service_id, params);
results = do_service(service_id, params);
send(client_id, results);
}
request processing
request selection
request queue
client requests server
response
server
![Page 45: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/45.jpg)
Fabienne Boyer, Distributed Programming 45
Design choices for a parallel server l Advantages
l Light-weight resources l Memory sharing
l Drawbacks l Limited isolation
l Advantages l Strong isolation l Real parallelism (also with kernel threads)
l Drawbacks l Heavy resources
Thread-based
Process-based
![Page 46: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/46.jpg)
Fabienne Boyer, Distributed Programming 46
Multi-threaded server
request selection
request queue
main thread client
requests
response
server
request processing
worker thread
![Page 47: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/47.jpg)
Fabienne Boyer, Distributed Programming 47
Multi-threaded server UDP-based
while (true) {
receive(client_id,message);
extract(message, service_id, params);
thr = create_thread(client_id, service_id, params);
}
request selection
request queue
main thread
Program executed by thread thr:
results = do_service( service_id, params);
send(client_id, results);
exit()
client requests
response
worker thread request
processing
server
![Page 48: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/48.jpg)
Fabienne Boyer, Distributed Programming 48
Multi-threaded server – TCP-based
while (true) {
Socket s;
s = accept(client_id,message);
thr = create_thread(s);
}
request selection
request queue
main thread
Program executed by thread thr:
Socket s;
s.receive(client_id,message);
extract(message, service_id, params);
results = do_service( service_id, params);
send(client_id, results);
close()
exit()
client requests
response
worker thread request
processing
server
![Page 49: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/49.jpg)
Fabienne Boyer, Distributed Programming
49
Multi-threaded server (thread-pool)
request selection
request queue
main thread
worker thread request
processing
worker thread request
processing
client requests
response
worker thread request
processing
server
work_to_do
![Page 50: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/50.jpg)
Fabienne Boyer, Distributed Programming 50
Multi-threaded server (pool) – UDP-based
while (true) {
receive(client_id,message);
extract(message, service_id, params);
work_to_do.put(client_id, service_id,params);
}
Pool of threads:
while (true) {
work_to_do.get( client_id, service_id, params);
results = do_service( service_id, params);
send(client_id, results);
}
![Page 51: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/51.jpg)
Fabienne Boyer, Distributed Programming 51
Multi-threaded server (pool) – TCP-based
while (true) {
Socket s;
s = accept(client_id,message);
work_to_do.put(s);
}
Pool of threads:
Socket s;
while (true) {
s = work_to_do.get();
s.receive(client_id,message);
extract(message, service_id, params);
results = do_service( service_id, params);
send(client_id, results);
close();
}
![Page 52: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/52.jpg)
Fabienne Boyer, Basics of Distributed Programming 52
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
l User vs Kernel threads
![Page 53: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/53.jpg)
Fabienne Boyer, Basics of Distributed Programming 53
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
![Page 54: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/54.jpg)
Fabienne Boyer, Basics of Distributed Programming 54
Thread (Reminder)
l Two ways l Extends Thread l Implements Runnable
This example is taken from Java Thread tutorial
public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }
![Page 55: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/55.jpg)
Fabienne Boyer, Basics of Distributed Programming 55
Thread (Reminder)
l Another way is to use the Runnable interface
l Be careful, directly calling the run method does not create a thread
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(); } }
![Page 56: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/56.jpg)
Fabienne Boyer, Basics of Distributed Programming 56
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
![Page 57: Socket-based Distributed Systems - LIG Membreslig-membres.imag.fr/boyer/html/Documents/cours/AR/02-Sockets-ETD.pdf · Fabienne Boyer, Basics of Distributed Programming 11 TCP Sockets](https://reader030.fdocuments.us/reader030/viewer/2022040906/5e7ab532b7ce851cb427d0ac/html5/thumbnails/57.jpg)
Fabienne Boyer, Basics of Distributed Programming 57
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.