Distributed Systems
Lecture -2-
Created by :
Eng. Ghadeer Al Hasan
UDP
Intro 1
- DatagramSockets are Java’s mechanismfor network communication via UDP insteadof
TCP.
- Java provides DatagramSocket to communicate over UDP insteadof TCP.
- DatagramSockets can be usedto both send and receive packets over the Internet.
Intro… 2
- It is preferredover TCP is the live coverage of TV channels.
- In this aspect, we want to transmit as many frames to live audience as possible not worrying about
the loss of one or two frames.(TCP being a reliable protocol add its own overhead while transmission)
- Another example where UDP is preferred is online multiplayer gaming.
- In games likecounter-strike or call of duty, it is not necessaryto relayall the information but the
mostimportant ones.
- It should also be noted that most of the applications in real life uses careful blendof both UDP and
TCP; transmitting the critical data over TCP and rest of the data via UDP.
Java Datagram programming model Steps
1st Step 4
1- Creation of DatagramSocket: - First, a datagramSocket object is createdto carry the
packet to the destination and to receive it whenever the server sends any data.
To createa datagramSocket followingconstructors can be used:
protected DatagramSocket DatagramSocket()
protected DatagramSocket DatagramSocket(int port)
protected DatagramSocket DatagramSocket(int port, InetAddress inetaddress)
What is
port?
2nd Step 5
2 - Creation of DatagramPacket: In this step, the packet for sending/receiving data via a
datagramSocket is created.
Constructor to senddata:
DatagramPacket(byte buf[], int length, InetAddress inetaddress, int port)
Parameters:
buf - the packet data.
offset - the packet data offset.
length - the packet data length.
address - the destination socket address.
Constructs a DatagramPacket for sending data at specifiedaddress and specifiedport.
2nd Step… 6
Constructor to receive the data:
DatagramPacket(byte buf[], int length)
Parameters:
buf - thepacket data.
length- the packet data length.
Constructs a DatagramPacket for receiving the data of lengthlengthin the byte array buf.
3rd Step 7
Invoke a send() or receive() call on socket object
void send(DatagramPacket packet)
void receive(DatagramPacket packet)
Server side Implementation
9DatagramSocket socket = null;
byte[] receiverBuffer = null;
DatagramPacket receivePacket = null;
private void initializeVarible(){
try {
socket = new DatagramSocket(Constants.PORT);
receiverBuffer = new byte[Constants.BUFFER_SIZE];
} catch (SocketException ex) {
log("initializeVarible : " + ex.getMessage());
}
}
private String receiveData(){
String line = "";
try {
receivePacket = new
DatagramPacket(receiverBuffer,receiverBuffer.length);
socket.receive(receivePacket);
line = new
String(receivePacket.getData(),0,receivePacket.getLength());
} catch (IOException ex) {
log("receiveData : " + ex.getMessage());
}
return line;
}
10
public static void main(String[] args){
Server server = new Server();
server.initializeVarible();
server.connecting();
}
private void connecting(){
while(true){
String data = receiveData();
log("Client : " + data);
if(data.equals(Constants.STOP)){
log("Client say bye...Exiting");
break;
}
receiverBuffer = new byte[Constants.BUFFER_SIZE];
}
}
Client side Implementation
12Scanner scan = null;
DatagramSocket socket = null;
byte[] buffer = null;
private void initializeVarible(){
try {
scan = new Scanner(System.in);
socket = new DatagramSocket();
} catch (SocketException ex) {
log("initializeVarible : " + ex.getMessage());
}
}
private void send(String message){
try {
InetAddress ip = InetAddress.getLocalHost();
buffer = message.getBytes();
DatagramPacket packetSend = new
DatagramPacket(buffer,buffer.length,ip,Constants.PORT);
socket.send(packetSend);
} catch (IOException ex) {
log("send : " + ex.getMessage());
}
}
13
private void connecting(){
while(true){
String line = readFromKeyboard();
send(line);
if(line.equals(Constants.STOP)){
break;
}
}
}
private String readFromKeyboard(){
String line = scan.nextLine();
return line;
}
public static void main(String[] args){
Client client = new Client();
client.initializeVarible();
client.connecting();
}
Run 14
Multicast Example
Server Side 16
MulticastSocket socket = null;
byte[] buffer = null;
DatagramPacket receivePacket = null;
Scanner scan = null;
private void initializeVarible() {
try {
socket = new MulticastSocket();
buffer = new byte[Constants.BUFFER_SIZE];
scan = new Scanner(System.in);
log("Server Running...");
} catch (SocketException ex) {
log("initializeVarible : " + ex.getMessage());
} catch (IOException ex) {
log("initializeVarible : " + ex.getMessage());
}
}
Server Side… 17
private String readFromKeyboard(){
String line = scan.nextLine();
return line;
}
private void send(String message) {
try {
InetAddress ip = InetAddress.getByName(Constants.IP);
buffer = message.getBytes();
DatagramPacket packetSend = new DatagramPacket(buffer, buffer.length,
ip, Constants.PORT);
socket.send(packetSend);
log("Message Sent");
} catch (IOException ex) {
log("send : " + ex.getMessage());
}
}
Server Side… 18
private void connecting() {
while (true) {
String data = readFromKeyboard();
send(data);
buffer = new byte[Constants.BUFFER_SIZE];
}
}
public static void main(String[] args) {
Server server = new Server();
server.initializeVarible();
server.connecting();
}
Client Side 19
MulticastSocket socket = null;
byte[] buffer = null;
DatagramPacket packet = null;
InetAddress ip = null;
private void initializeVarible() {
try {
socket = new MulticastSocket(Constants.PORT);
ip = InetAddress.getByName(Constants.IP);
buffer = new byte[Constants.BUFFER_SIZE];
} catch (SocketException ex) {
log("initializeVarible : " + ex.getMessage());
} catch (IOException ex) {
log("initializeVarible : " + ex.getMessage());
}
}
Client Side… 20
private String receiveData() {
String line = "";
try {
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
line = new String(packet.getData(), 0, packet.getLength());
} catch (IOException ex) {
log("receiveData : " + ex.getMessage());
}
return line;
}
private void joinGroup(){
try {
socket.joinGroup(ip);
log("Client Running...");
} catch (IOException ex) {
log("joinGroup : " + ex.getMessage());
}
}
Client Side… 21
private void connecting(){
joinGroup();
while(true){
String line = receiveData();
log("Client Received : " + line);
}
}
public static void main(String[] args){
Client client = new Client();
client.initializeVarible();
client.connecting();
}
Run 22
References 24
- YouTube link
https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ
- GitHub
https://github.com/Ghadeerof
End Lecture

#2 (UDP)

  • 1.
    Distributed Systems Lecture -2- Createdby : Eng. Ghadeer Al Hasan UDP
  • 2.
    Intro 1 - DatagramSocketsare Java’s mechanismfor network communication via UDP insteadof TCP. - Java provides DatagramSocket to communicate over UDP insteadof TCP. - DatagramSockets can be usedto both send and receive packets over the Internet.
  • 3.
    Intro… 2 - Itis preferredover TCP is the live coverage of TV channels. - In this aspect, we want to transmit as many frames to live audience as possible not worrying about the loss of one or two frames.(TCP being a reliable protocol add its own overhead while transmission) - Another example where UDP is preferred is online multiplayer gaming. - In games likecounter-strike or call of duty, it is not necessaryto relayall the information but the mostimportant ones. - It should also be noted that most of the applications in real life uses careful blendof both UDP and TCP; transmitting the critical data over TCP and rest of the data via UDP.
  • 4.
  • 5.
    1st Step 4 1-Creation of DatagramSocket: - First, a datagramSocket object is createdto carry the packet to the destination and to receive it whenever the server sends any data. To createa datagramSocket followingconstructors can be used: protected DatagramSocket DatagramSocket() protected DatagramSocket DatagramSocket(int port) protected DatagramSocket DatagramSocket(int port, InetAddress inetaddress) What is port?
  • 6.
    2nd Step 5 2- Creation of DatagramPacket: In this step, the packet for sending/receiving data via a datagramSocket is created. Constructor to senddata: DatagramPacket(byte buf[], int length, InetAddress inetaddress, int port) Parameters: buf - the packet data. offset - the packet data offset. length - the packet data length. address - the destination socket address. Constructs a DatagramPacket for sending data at specifiedaddress and specifiedport.
  • 7.
    2nd Step… 6 Constructorto receive the data: DatagramPacket(byte buf[], int length) Parameters: buf - thepacket data. length- the packet data length. Constructs a DatagramPacket for receiving the data of lengthlengthin the byte array buf.
  • 8.
    3rd Step 7 Invokea send() or receive() call on socket object void send(DatagramPacket packet) void receive(DatagramPacket packet)
  • 9.
  • 10.
    9DatagramSocket socket =null; byte[] receiverBuffer = null; DatagramPacket receivePacket = null; private void initializeVarible(){ try { socket = new DatagramSocket(Constants.PORT); receiverBuffer = new byte[Constants.BUFFER_SIZE]; } catch (SocketException ex) { log("initializeVarible : " + ex.getMessage()); } } private String receiveData(){ String line = ""; try { receivePacket = new DatagramPacket(receiverBuffer,receiverBuffer.length); socket.receive(receivePacket); line = new String(receivePacket.getData(),0,receivePacket.getLength()); } catch (IOException ex) { log("receiveData : " + ex.getMessage()); } return line; }
  • 11.
    10 public static voidmain(String[] args){ Server server = new Server(); server.initializeVarible(); server.connecting(); } private void connecting(){ while(true){ String data = receiveData(); log("Client : " + data); if(data.equals(Constants.STOP)){ log("Client say bye...Exiting"); break; } receiverBuffer = new byte[Constants.BUFFER_SIZE]; } }
  • 12.
  • 13.
    12Scanner scan =null; DatagramSocket socket = null; byte[] buffer = null; private void initializeVarible(){ try { scan = new Scanner(System.in); socket = new DatagramSocket(); } catch (SocketException ex) { log("initializeVarible : " + ex.getMessage()); } } private void send(String message){ try { InetAddress ip = InetAddress.getLocalHost(); buffer = message.getBytes(); DatagramPacket packetSend = new DatagramPacket(buffer,buffer.length,ip,Constants.PORT); socket.send(packetSend); } catch (IOException ex) { log("send : " + ex.getMessage()); } }
  • 14.
    13 private void connecting(){ while(true){ Stringline = readFromKeyboard(); send(line); if(line.equals(Constants.STOP)){ break; } } } private String readFromKeyboard(){ String line = scan.nextLine(); return line; } public static void main(String[] args){ Client client = new Client(); client.initializeVarible(); client.connecting(); }
  • 15.
  • 16.
  • 17.
    Server Side 16 MulticastSocketsocket = null; byte[] buffer = null; DatagramPacket receivePacket = null; Scanner scan = null; private void initializeVarible() { try { socket = new MulticastSocket(); buffer = new byte[Constants.BUFFER_SIZE]; scan = new Scanner(System.in); log("Server Running..."); } catch (SocketException ex) { log("initializeVarible : " + ex.getMessage()); } catch (IOException ex) { log("initializeVarible : " + ex.getMessage()); } }
  • 18.
    Server Side… 17 privateString readFromKeyboard(){ String line = scan.nextLine(); return line; } private void send(String message) { try { InetAddress ip = InetAddress.getByName(Constants.IP); buffer = message.getBytes(); DatagramPacket packetSend = new DatagramPacket(buffer, buffer.length, ip, Constants.PORT); socket.send(packetSend); log("Message Sent"); } catch (IOException ex) { log("send : " + ex.getMessage()); } }
  • 19.
    Server Side… 18 privatevoid connecting() { while (true) { String data = readFromKeyboard(); send(data); buffer = new byte[Constants.BUFFER_SIZE]; } } public static void main(String[] args) { Server server = new Server(); server.initializeVarible(); server.connecting(); }
  • 20.
    Client Side 19 MulticastSocketsocket = null; byte[] buffer = null; DatagramPacket packet = null; InetAddress ip = null; private void initializeVarible() { try { socket = new MulticastSocket(Constants.PORT); ip = InetAddress.getByName(Constants.IP); buffer = new byte[Constants.BUFFER_SIZE]; } catch (SocketException ex) { log("initializeVarible : " + ex.getMessage()); } catch (IOException ex) { log("initializeVarible : " + ex.getMessage()); } }
  • 21.
    Client Side… 20 privateString receiveData() { String line = ""; try { packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); line = new String(packet.getData(), 0, packet.getLength()); } catch (IOException ex) { log("receiveData : " + ex.getMessage()); } return line; } private void joinGroup(){ try { socket.joinGroup(ip); log("Client Running..."); } catch (IOException ex) { log("joinGroup : " + ex.getMessage()); } }
  • 22.
    Client Side… 21 privatevoid connecting(){ joinGroup(); while(true){ String line = receiveData(); log("Client Received : " + line); } } public static void main(String[] args){ Client client = new Client(); client.initializeVarible(); client.connecting(); }
  • 23.
  • 24.
    References 24 - YouTubelink https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ - GitHub https://github.com/Ghadeerof
  • 25.