Server-Client communication without connection When the communication consists of sending and/or...

35
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.
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    0

Transcript of Server-Client communication without connection When the communication consists of sending and/or...

Page 1: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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.

Page 2: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Datagram 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)

Page 3: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

UDP: Communication with Datagrams

DATAGRAMA: an independent self-containing message sent through the internet whose arrival, delay and content are not guaranteedOnce a server program is hearing for incoming datagrams, the client can send one.

A SERVER A CLIENT

4444

www.waseda1.jp

www.waseda1.jp

message

4444

www.waseda2.jp

?

Page 4: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Sending 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

A SERVER A CLIENT

4444

www.waseda1.jp

3333

www.waseda2.jp

?

Page 5: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Before the datagram leaves the computer, the TCP/IP layer puts the address and port of origin

A SERVER A CLIENT

4444

www.waseda1.jp

3333

www.waseda2.jp

!

Remtent address

Page 6: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

The client can now start waiting for an answer of the server

A SERVER A CLIENT

4444

www.waseda1.jp

3333

www.waseda2.jp

?

Receiving Datagrams

Page 7: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

The server can extract the information about the address and port from the datagram and prepare the answer

A SERVER A CLIENT

4444

www.waseda1.jp

3333

www.waseda2.jp

answer

?

Sending datagramas

Page 8: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Finally the answer can be sent sent.

A SERVER A CLIENT

4444

www.waseda1.jp

3333

www.waseda2.jp

?

Sending datagramas

Page 9: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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 !!!

Page 10: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

• 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 use the data variable which points to the byte-array

Classes for Datagrams in Java: Receive

Page 11: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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

Page 12: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

¿ 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

Page 13: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Bemerkungen ü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

Page 14: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Multicasting & 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

Page 15: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

The Multicast paradigm

PROG1

PROG2

PROG2

PROG2

Page 16: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Multicast & 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

Page 17: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Multicast• 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 !

Page 18: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

What 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

Page 19: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Multicast 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)

Page 20: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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 byte[256];DatagramPacket packet;

while(true) { 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) {

} }}

Example of Multicast in Javaimport 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 i=1; i< 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) {} }}

Page 21: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Sending video by Multicast

MulticastMovieServer

MulticastMovieClient

Page 22: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Group 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)

Page 23: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Possible 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

Page 24: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

A peer-to-peer chat based on Multicast

• There is no server.• Every participant runs the same program joining

the multicast group defined by address+port • 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

Page 25: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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

Page 26: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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

Page 27: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Multicast 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

Page 28: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Reliable 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

Page 29: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Reliable 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 <q,R(q,g)>, 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 <= R(p,g) then it is a message already processed and it is discarded• Si S > 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.

Page 30: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Ordering 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 <m,i> 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 <i,a>• every process then orders the message queue before processing them according to the number sequence

Page 31: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Time 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 !

Page 32: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

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

Page 33: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Broadcast• 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 -> 192.1.2.255– Para una subred de 16 hosts 197.84.66.192 ->

197.84.66.207 • A port number should be agreed

Page 34: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

¿ 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

Page 35: Server-Client communication without connection When the communication consists of sending and/or receiving datagram packets instead of a data stream it.

Homework: 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