Transcript cap5-2006

Server-Client communication without
connection
When the communication consists of sending
and/or receiving datagram packets instead of a
data stream it is called a connectionless
communication
This means there is no “virtual link” created
between both end of a communication.
This is very near to how the packages are
actually delivered over the over the internet.
This is why the arriving, order or uniqueness of
packages cannot be guaranteed.
1
Datagram management with JAVA
Communication is based on assembling UDP
packages and sending them to the interent. An
UDP package consists of:
 Data: a bytes array
 Destination Port : int
 Destination Address: InetAddress
A server start by listening at a certain port for
packages.
The client assembles a package and send it to the
net.
The server receives the package (routed by the
net to its final destination) and extracts the data.
If the server needs to answer, it extracts the
sender address and port (the client must be
listening for packages)
2
Classes for Datagrams in Java: Send
 Create a socket for sending a Datagram to the internet
 DatagramSocket ds = new DatagramSocket();
 Create and assemble the Datagram
 byte[] data = new byte[256];
 InetAddress address =
InetAddress.getByName(“www.ctc.cl”);
 DatagramPacket pack = new DatagramPacket(data,
data.length,address,4444);
 Send
 ds.send(pack);
 Wait for an answer
 socket.receive(pack); //make sure it is clean before, perhaps
by using a new one !!!
EchoUDPClient DateUDPClient
3
Classes for Datagrams in Java: Receive
Start listening for Datagrams on a certain socket
 socket = new DatagramSocket(4444);
Preparing a Datagram for receiving data
 byte[] data = new byte[256];
 DatagramPacket pack = new DatagramPacket(data,data.length);
Start listening for a package
 socket .receive(pack);
Obtaining the data and address and port of sender




int port = pack.getPort();
InetAddress address = pack getAddress();
String content = new String(pack.getData());
Or just by using the data variable which points to the byte-array
DateUDPServer EchoUDPServer
4
An UDP Ping Client
We will use the echo server by default
In a Unix machine there is normally an echo server
listening at port 7 for UDP and for TCP requests
It is not the same server, but it is possible to open 2
sever sockets for the same port but for different
protocols
The Ping client will send a package to the server
with time of issue, which will be returned by the
server
By comparing time in the Datagram and current
time we can have the round-trip delay
The program will also calculate max/min and avg
Pinging.java
5
Multicasting
What happens when a server has to distribute the
same information to many clients and the
information is too heavy ?
 The server will spend too much time attending
each client
 In the case of a videoconference in real time this
is impossible
There is a way to have the server transmitting the
information only once and received by many
For this, the network must be “multicastingable”
6
The Multicast Principle
PROG2
PROG1
PROG2
PROG2
7
Multicast Characteristics
To send/receive information in Multicast with Java
is similar as doing it with UDP
Variations:
 Sender should address packages to an IP number
in the range between 224.0.0.1 and 239.255.254
 Receivers should have previously “expressed”
the wish to receive them by joining a Multicast
group (identified by a multicast address ).
The routers of the network will take care of
delivering a copy to every host in the Internet which
is in the group (not really true
8
Multicast in Java
MulticastSocket: extension of DatagramSocket
 MulticastSocket( ) it is bound to any available port
 MulticastSocket(int port) bound to a specific port
several multicast socekts can be bound
simultanously to the same port ! (contrary to
TCP o UDP)
Inherited methods (send, receive) + 3 new
 joinGroup(InetAddress group)
 leaveGroup(InetAddress group)
 setTimeToLive(int ttl)
9
Example of Multicast in Java
import java.io.*;
import java.io.*;
import java.net.*;
import java.net.*;
public class MulticastReceiver {
import java.util.*;
public static void main(String[] args) throws IOException {
public class MulticastSender {
MulticastSocket socket = new MulticastSocket(4446);
static public void main(String args[]) {
InetAddress address =
DatagramSocket socket = null;
InetAddress.getByName("224.2.2.3");
BufferedReader in = null;
socket.joinGroup(address);
boolean moreQuotes = true;
byte[] buf = new byte[256];
try {
DatagramPacket packet;
socket = new DatagramSocket();
while (true) {
while(true) {
InetAddress grupo = InetAddress.getByName("224.2.2.3");
packet = new DatagramPacket(buf, buf.length);
for (int i=1; i< 1000; i++) {
socket.receive(packet);
String dString = i+"--"+(InetAddress.getLocalHost());
byte[] buf = dString.getBytes();
String received = new String(packet.getData());
DatagramPacket packet =
System.out.println("Received: " + received);
new DatagramPacket(buf, buf.length, grupo, 4446); try {
socket.send(packet);
Thread.currentThread().sleep(0); }
try {
catch (InterruptedException e) {
}
Thread.currentThread().sleep(200); }
}
catch (InterruptedException e) {}
}
}
}
} catch (IOException e) {}
}
}
10
Sending video by Multicast
MulticastMovieServer
MulticastMovieClient
11
A Multicast Based Chat
There is no server.
Each participant runs the exactly same
program, joining a common Multicast
group
• The messages are “multicasted” over the
net, thus everyone joining the group will
receive them
• There is no guarantee about the arriving,
arriving time, or duplication of messages
MulticastChat
12
Spontaneous Networking
Multicasting is the right way to program systems when
the participants in the session may come and go very
frequently
This is the case of spontaneous networking with mobile
devices in a room
Someone “announce” her presence to the other members
by sending message to all at regular intervals
The fact that someone has left is recorded by the others
when there have been no messages from her since a
certain period of time
13
An Awareness Example
The MulticastRegister program will show all
people participating in the multicast group
It implements a thread that will send every
second a packet with the client's identification
It starts 3 other threads:
 ReceiveThread: will listen to packets sent by other members of
the group. When it receives one, it will register it in a vector
containing a pair (participant-id, time) with the time the last
packet received from a participant was received
 ChckThread: check the vector every 2 seconds the vector and
deletes the entries from the participants whose last package
was received more than 10 seconds ago
 RefreshThread: it simply refreshes the list showing the active
participants according to the vector’s content
14
Reliable Multicast
Each participant process p maintains a sequence number S(p)
for the messages it sends to a certain multicast group
It also maintains a register R(q) for each other process q
participating with the number of the last message sent by that
process
When p wants to send a message it includes the number S(p)
and the pairs <q,R(q)>, afetr that it increments S(p).
A process k receiving this message will process it only if
S(p) = R(p) +1
If S <= R(p) the message was already received before
If S > R(p) + 1 messages are missing, the process k sends a
request to p
This means, processes should store messages they send in
case they receive a request to send it again
15
Ordering Multicast messages
In order to process all messages in the same order, processes
maintain a queue with multicast messages. The principle is that all
processes assign one and the same processing sequence number to a
certain message sent to the group. Each process q maintains a
number A(q), corresponding to the highest of the agreed sequence
observed and a number P(q,g) corresponding to the highest of its
own sequence of sent messages. When p wants to send a message:
1- p Sends <m,i> in a reliable way (m is the message itself and i
an identification)
2- Each process q answers with a proposition number for that
message which will be P(q) = Max(A(q), P(q))+1.
3- p collecta all the numbers and selects the highest for this
message and sends it to the rest
5- all processes receive this num ber and use it for ordering the
messages in the queue, which will eventually processed
16
MBone
Multicast is currently not widely deployed on the
Internet so it is not possible to implement it across
different networks. This is mainly because of the
routers not supporting the IGMP
There is a subnet called MBone which communicate
multicast-enabled islands, allowing the transport of
multicast packets through tunnels.
A tunnel communicates the routers of two networks
which are not physically adjacent.
Packages will be forwarded from router to the other
as if there were actually neighboring networks
17
Broadcast
Broadcast is similar to Multicast but in a local
network
Every Broadcast based network (like ethernet) has a
broadcast IP address. Any message sent to this
address will be received by all computers on the
local network
Usually this is the last IP address of the subnet:
 Class C: 192.1.2.0 -> 192.1.2.255
 For a sub-network of 16 hosts 197.84.66.192 ->
197.84.66.207
A port number should be agreed
18
¿ Broadcast or Multicast ?
If you can chose it is better to use Multicast
because it does not disturb other machines
Sometimes is necessary to have privileges to
write on a broadcast address.
Multicast allows many multicast groups in
the same network
The generated traffic is the same: one
package which is received by all members
Broadcast works in Java like common UDP.
Only the IP address is special
19