ServerClient communication without connection When the communication consists

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

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. 1

Datagram management with JAVA Communication is based on assembling UDP packages and sending them

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: 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) 2

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

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 !!! Echo. UDPClient Date. UDPClient 3

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

Classes for Datagrams in Java: Receive Start listening for Datagrams on a certain 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 by using the data variable which points to the byte-array Date. UDPServer Echo. UDPServer 4

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

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

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 PROG 2 PROG 1 PROG 2 7

The Multicast Principle PROG 2 PROG 1 PROG 2 7

Multicast Characteristics To send/receive information in Multicast with Java is similar as doing it

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 Multicast. Socket: extension of Datagram. Socket Multicast. Socket( ) it is

Multicast in Java Multicast. Socket: extension of Datagram. Socket Multicast. Socket( ) it is bound to any available port Multicast. Socket(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 join. Group(Inet. Address group) leave. Group(Inet. Address group) set. Time. To. Live(int ttl) 9

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. Receiver { import java. util. *; public static void main(String[] args) throws IOException { public class Multicast. Sender { 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"); socket. join. Group(address); Buffered. Reader in = null; 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); } catch (Interrupted. Exception e) { try { } Thread. current. Thread(). sleep(200); } } catch (Interrupted. Exception e) {} } } } catch (IOException e) {} } } 10

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

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

A Multicast Based Chat There is no server. Each participant runs the exactly same

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

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

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 Multicast. Register program will show all people participating in the

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 14

Reliable Multicast Each participant process p maintains a sequence number S(p) for the messages

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

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

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 is similar to Multicast but in a local network Every Broadcast based network

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

¿ 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