Title: Server-Client communication without connection
1Server-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.
2Datagram manipulation 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 packages 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)
3UDP Communication with Datagrams
DATAGRAMA an independent self-containing message
sent through the internet whose arrival, delay
and content are not guaranteed
Once a server program is hearing for incoming
datagrams, the client can send one.
www.waseda2.jp
www.waseda1.jp
A SERVER
A CLIENT
?
4444
www.waseda1.jp
4444
message
4Sending Datagrams
The client must open a UDP socket to send the
datagram which should contain the address and
port of destination. The routing algorithm will
forward it to the destination
www.waseda2.jp
www.waseda1.jp
A SERVER
A CLIENT
?
3333
4444
5Remtent address
Before the datagram leaves the computer, the
TCP/IP layer puts the address and port of origin
www.waseda2.jp
www.waseda1.jp
A SERVER
A CLIENT
!
3333
4444
6Receiving Datagrams
The client can now start waiting for an answer of
the server
www.waseda2.jp
www.waseda1.jp
A SERVER
A CLIENT
?
3333
4444
7Sending datagramas
The server can extract the information about the
address and port from the datagram and prepare
the answer
www.waseda1.jp
www.waseda2.jp
A SERVER
A CLIENT
?
3333
4444
answer
8Sending datagramas
Finally the answer can be sent sent.
www.waseda2.jp
www.waseda1.jp
A SERVER
A CLIENT
?
3333
4444
9Classes 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 byte256
- InetAddress address InetAddress.getByName(www.c
tc.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 !!!
10Classes 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 byte256
- 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 use the data variable which points to the
byte-array
11An 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
12 What about lost packages ?
- UDP Datagrams can get lost like internet frames
- We can check this by numbering the packages and
controlling the sequence - Make the following experiments
- package loss between Chile and Germany
- package loss between Japan and Chile
- package loss between Chile and Japan
- Make also the same inside a LAN
- Varying the number of clients
Tiro/ReciboPaquetesNoEnd
13Bemerkungen über UDP
- Do not try to develop your own fully reliable UDP
based communication mechanism - It is only helpful if you want to add some
reliability - A server listening for datagrams will receive all
of them ! - This means, if you want to establish a dialogue
with a client in a separate thread, do it through
another available port, like in
tiro/recibopaquetes
14Multicasting Broadcasting
- If a server must distribute information to many
clients it may overload its resources - This is especially true in situations like
videoconferencing (multipoint), where several
frames per seconds should be transmitted to may
be several clients this is not possible in the
practice! - In multicasting and broadcasting the server
transmits this information only once. - This requires the hardware (network) to support
this
15The Multicast paradigm
PROG2
PROG1
PROG2
PROG2
16Multicast Broadcast
- Multicast Broadcast are protocols which allow
an application to put a single package on the net
which will be received by many other
applications, - Broadcast works only inside a local network. A
broadcasted package will be received by all. - It requires support from the local network.
- Multicast will arrive only to interested
clients which have registered before - Multicast requires host and routers to support
the IGMP routing algorithm
17Multicast
- Multicast in Java is similar to UDP except that
sending-receiving should be implemented on an IP
number in the range between (224.0.0.0 -
239.255.255.255) - In order to receive the Multicast packs the
client must express interest in joining a certain
multicast group at a certain multicast address
and port. The network, (the routers) will deliver
the packs to the interested hosts - Any application can transmit packs to the group !
18What happens when a package is thrown
- The packet will be picked up my any machines on
the local network that are interested in that
group. - In addition it will be picked up by routers that
will forward it as appropriate to adjacent
networks that are interested - The significant complexity of multicast is how
routers will know what adjacent networks are
interested - This requires the storage of additional
information in the routing table
19Multicast support in Java
- MulticastSocket Extension of the
DatagramSocket - MulticastSocket( ) binds to any available port
- MulticastSocket(int port) uses a specific port
- Many multicast sockets may be bound the the same
port at the same time! (contrary to TCP or UDP) - Inherited methods (send, receive) 3 new
- joinGroup(InetAddress group)
- leaveGroup(InetAddress group)
- setTimeToLive(int ttl)
20Example of Multicast in Java
import java.io. import java.net. public class
MulticastClient public static void
main(String args) throws IOException
MulticastSocket socket new MulticastSocket(4446)
InetAddress address
InetAddress.getByName("224.2.2
.3") socket.joinGroup(address) byte buf
new byte256 DatagramPacket packet while(tru
e) packet new DatagramPacket(buf,
buf.length) socket.receive(packet)
String received new String(packet.getData())
System.out.println("Received " received)
try Thread.currentThread().sleep(0)
catch (InterruptedException e)
import java.io. import java.net. import
java.util. public class MulticastServer
static public void main(String args)
DatagramSocket socket null BufferedReader
in null boolean moreQuotes true
try socket new DatagramSocket()
while (true) InetAddress grupo
InetAddress.getByName("224.2.2.3")
for (int i1 ilt 1000 i)
String dString i"--"(InetAddress.getLocalHost(
)) byte buf
dString.getBytes() DatagramPacket packet
new DatagramPacket(buf,
buf.length, grupo, 4446)
socket.send(packet) try
Thread.currentThread().sleep(200) catch
(InterruptedException e)
catch (IOException e)
21Sending video by Multicast
MulticastMovieServer
MulticastMovieClient
22Group communication with Multicast
- Provides
- Fault tolerance based of replicated services
there are many servers providing the same
service. The client sends a request to all by a
multicasting message - Discovery of services client and services use
multicast messages to register their services and
interfaces. Clients use it to localize them and
do the lookup of the interfaces. - Better performance for replicated data when
clients keep a copy of a certain data locally
(cache) the server sends the updated data by
multicast - Propagation of notification events when clients
wait for a certain event to react (jini)
23Possible faults with Multicast
- Because Multicast is based on UDP
- Fault tolerant replicated services If all
servers start from the same initial state and
they coordinate themselves by events, if one
client does not receive the update or receives it
in a bad order its sate may be inconsistent - Finding discovery services The servers should
send in regularly messages telling about their
presence - Better performance in replicated data If the
message of changing of a certain data does not
arrive to a client it may have an inconsistency
with the rest - Propagation of notification events Send
notifications regularly
24A peer-to-peer chat based on Multicast
- There is no server.
- Every participant runs the same program joining
the multicast group defined by addressport - Messages are distributed to the net as datagrams
to the multicast group, so every interested
application will receive - There is no guarantee that the messages will
arrive or not, nor if they will arrive in the
correct order
MulticastChat
25Spontaneous 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
26An 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
vectors content
27Multicast Model for groups
Multicast has certain characteristics that makes
it efficient to trnasmit messages to all members
of a group Model message(g,m) operation of
sending a message to all members of the
group g deliver(m) operation of
processing the message m sender(m)
identification of the sender of a message
group(m) target group of message m
open/closed group tells if the group is
open or closed for other members to send
messages to the group
28Reliable Multicast
In order to have reliable multicast, 3
conditions must be fullfilled Integrity
The message that has been sent is the same that
the message that is prodecced. Moreover, no
message is processed two times. A process p
performs the operation deliver(m) only once and
p ? group(m) Validity if a process p
sends a multicast message to the group and p ?
group(m), p will eventually make deliver(m)
Agreement if a process p makes deliver(m) all
the rest will eventually do this also
29Reliable Multicast with IP !
- Every process maintains a sequence number S(p,g)
for every multicast group it belongs. - It also maintains a register R(q,g) which is the
sequence number of the last processed message
sent by q to the group g - When q sends a message to g it includes the
number S(p,g) and pairs ltq,R(q,g)gt, and then it
increments S(p,g). - A process of the group processes a message sent
by p only if S R(p,g) 1 - if S lt R(p,g) then it is a message already
processed and it is discarded - Si S gt R(p,g) 1 means the process has not
received one of the messages of p and sends a
negative ack message in order to have p send it
again - Integrity is achieved by detection of duplicated
messages and checksums made by the IP layer over
the datagrams. Validity is achieved by multicast
properties. - In order to reach agreement, every process has to
keep a copy of the messages sent in order to send
them again if necessary. -
30Ordering the Multicast messages
- A queue is used to store the messages before
processing them. For each message, a sequence
number will be assigned which must be accorded by
all participants. Every process q in a group g
maintains a number A(q,g), which is the biggest
for a sequence previously accorded which has been
seen for a group g and P(q,g) is the greatest of
the process own sequence. When p wants to send a
message - sends in a secure way ltm,igt being m the message
and i an unique identifier for m - Every process q answers to p with a proposal of
the sequetial number for this message P(q,g)
Max(A(q,g), P(q,g))1. Every process stores in
its queue the message with the number it
provisionally proposed ordering the sequence from
the smallest to the biggest. - p collects all numbers and selects the biggest,
wich will be used to transmit the message in a
secure way lti,agt - every process then orders the message queue
before processing them according to the number
sequence -
31Time to Live
- Multicast packets include a TTL field that limits
the propagation across the internet - In general, every time a packet is relayed by a
router or a tunnel the value of TTL will be
decreased - When it reaches cero the package is discarded
- This concept is also present on every internet
packet !
32MBone
- 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
33Broadcast
- Broadcast is similar a 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 - Clase C 192.1.2.0 -gt 192.1.2.255
- Para una subred de 16 hosts 197.84.66.192 -gt
197.84.66.207 - A port number should be agreed
34 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
35Homework Merge multicastig chat with
multicasting awareness
- Rewrite MulticastChat in order to include
- A window interface like the one seen on the
TCP/IP chat with central server - The list of participating people should be
implemnted according to the MulticastRegister
schema -