ServerClient communication without connection When the communication consists

  • Slides: 35
Download presentation
Server-Client communication without connection • When the communication consists of sending and/or receiving datagram

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 internet. • This is why the arriving, order or uniqueness of packages cannot be guaranteed.

Datagram manipulation with JAVA • Communication is based on assembling UDP packages and sending

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: Inet. Address • 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)

UDP: Communication with Datagrams DATAGRAMA: an independent self-containing message sent through the internet whose

UDP: 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. waseda 2. jp www. waseda 1. jp A SERVER A CLIENT ? 4444 www. waseda 1. jp 4444 message

Sending Datagrams The client must open a UDP socket to send the datagram which

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 www. waseda 1. jp A SERVER ? 4444 www. waseda 2. jp A CLIENT 3333

Remtent address Before the datagram leaves the computer, the TCP/IP layer puts the address

Remtent address Before the datagram leaves the computer, the TCP/IP layer puts the address and port of origin www. waseda 1. jp A SERVER ! 4444 www. waseda 2. jp A CLIENT 3333

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

Receiving Datagrams The client can now start waiting for an answer of the server www. waseda 1. jp www. waseda 2. jp ? A SERVER 4444 A CLIENT 3333

Sending datagramas The server can extract the information about the address and port from

Sending datagramas The server can extract the information about the address and port from the datagram and prepare the answer www. waseda 1. jp www. waseda 2. jp ? A SERVER 4444 answer 3333 A CLIENT

Sending datagramas Finally the answer can be sent. www. waseda 1. jp www. waseda

Sending datagramas Finally the answer can be sent. www. waseda 1. jp www. waseda 2. jp ? A SERVER 4444 3333 A CLIENT

Classes for Datagrams in Java: Send • Create a socket for sending a Datagram

Classes for Datagrams in Java: Send • Create a socket for sending a Datagram to the internet – Datagram. Socket ds = new Datagram. Socket(); • Create and assemble the Datagram – byte[] data = new byte[256]; – Inet. Address address = Inet. Address. get. By. Name(“www. ctc. cl”); – Datagram. Packet pack = new Datagram. Packet(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 !!!

Classes for Datagrams in Java: Receive • Start listening for Datagrams on a certain

Classes for Datagrams in Java: Receive • Start listening for Datagrams on a certain socket – socket = new Datagram. Socket(4444); • Preparing a Datagram for receiving data – byte[] data = new byte[256]; – Datagram. Packet pack = new Datagram. Packet(data, data. length); • Start listening for a package – socket. receive(pack); • Obtaining the data and address and port of sender – int port = pack. get. Port(); – Inet. Address address = pack get. Address(); – String content = new String(pack. get. Data()); • Or just use the data variable which points to the byte-array

An UDP Ping Client • We will use the echo server by default •

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

¿ What about lost packages ? • UDP Datagrams can get lost like internet

¿ 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/Recibo. Paquetes. No. End

Bemerkungen über UDP • Do not try to develop your own fully reliable UDP

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

Multicasting & Broadcasting • If a server must distribute information to many clients 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

The Multicast paradigm PROG 2 PROG 1 PROG 2

The Multicast paradigm PROG 2 PROG 1 PROG 2

Multicast & Broadcast • Multicast & Broadcast are protocols which allow an application to

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

Multicast • Multicast in Java is similar to UDP except that sending-receiving should be

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

What happens when a package is “thrown” • The packet will be picked up

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

Multicast support in Java • Multicast. Socket: Extension of the Datagram. Socket – Multicast.

Multicast support in Java • Multicast. Socket: Extension of the Datagram. Socket – Multicast. Socket( ) binds to any available port – Multicast. Socket(int port) uses a specific port • Many multicast sockets may be bound the same port at the same time! (contrary to TCP or UDP) • Inherited methods (send, receive) + 3 new – join. Group(Inet. Address group) – leave. Group(Inet. Address group) – set. Time. To. Live(int ttl)

Example of Multicast in Java import java. io. *; import java. net. *; public

Example of Multicast in Java import java. io. *; import java. net. *; public class Multicast. Client { import java. util. *; public static void main(String[] args) throws IOException { public class Multicast. Server { Multicast. Socket socket = new Multicast. Socket(4446); static public void main(String args[]) { Inet. Address address = Datagram. Socket socket = null; Inet. Address. get. By. Name("224. 2. 2. 3"); Buffered. Reader in = null; socket. join. Group(address); boolean more. Quotes = true; byte[] buf = new byte[256]; try { Datagram. Packet packet; socket = new Datagram. Socket(); while (true) { while(true) { Inet. Address grupo = Inet. Address. get. By. Name("224. 2. 2. 3"); packet = new Datagram. Packet(buf, buf. length); for (int i=1; i< 1000; i++) { socket. receive(packet); String d. String = i+"--"+(Inet. Address. get. Local. Host()); byte[] buf = d. String. get. Bytes(); String received = new String(packet. get. Data()); Datagram. Packet packet = System. out. println("Received: " + received); new Datagram. Packet(buf, buf. length, grupo, 4446); try { socket. send(packet); Thread. current. Thread(). sleep(0); } try { catch (Interrupted. Exception e) { } Thread. current. Thread(). sleep(200); } } catch (Interrupted. Exception e) {} } } catch (IOException e) {} } }

Sending video by Multicast. Movie. Server Multicast. Movie. Client

Sending video by Multicast. Movie. Server Multicast. Movie. Client

Group communication with Multicast Provides: • Fault tolerance based of replicated services: there are

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)

Possible faults with Multicast Because Multicast is based on UDP : • Fault tolerant

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

A peer-to-peer chat based on Multicast • There is no server. • Every participant

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 Multicast. Chat

Spontaneous Networking • Multicasting is the right way to program systems when the participants

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

An Awareness Example • The Multicast. Register program will show all people participating in

An Awareness Example • The Multicast. Register 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: – Receive. Thread: 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 – Chck. Thread: 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 – Refresh. Thread: it simply refreshes the list showing the active participants according to the vector’s content

Multicast Model for groups Multicast has certain characteristics that makes it efficient to trnasmit

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

Reliable Multicast In order to have reliable multicast, 3 conditions must be fullfilled: Integrity:

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

Reliable Multicast with IP ! • Every process maintains a sequence number S(p, g)

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.

Ordering the Multicast messages • A queue is used to store the messages before

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

Time to Live • Multicast packets include a TTL field that limits the propagation

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 !

MBone • Multicast is currently not widely deployed on the Internet so it is

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

Broadcast • Broadcast is similar a Multicast but in a local network • Every

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

¿ Broadcast or Multicast ? • If you can chose it is better to

¿ 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

Homework: Merge multicastig chat with multicasting awareness • Rewrite Multicast. Chat in order to

Homework: Merge multicastig chat with multicasting awareness • Rewrite Multicast. Chat 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 Multicast. Register schema