SlideShare a Scribd company logo
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

More Related Content

PDF
Netty: asynchronous data transfer
PDF
PDF
Fun with Network Interfaces
PPTX
Network simulator 2
PPTX
Why learn Internals?
PPTX
Working with NS2
PDF
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
DOC
Packet filtering using jpcap
Netty: asynchronous data transfer
Fun with Network Interfaces
Network simulator 2
Why learn Internals?
Working with NS2
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Packet filtering using jpcap

What's hot (20)

PDF
NS-2 Tutorial
PPT
Introduction to NS2 - Cont..
PPT
Ns 2 Network Simulator An Introduction
PPT
Session 1 introduction to ns2
PPT
Ns network simulator
PPT
Stackless Python In Eve
PDF
Yevhen Tatarynov "From POC to High-Performance .NET applications"
PPTX
Network simulator 2
PDF
Building Network Functions with eBPF & BCC
PPT
Network Simulator Tutorial
PPTX
Network programming using python
PDF
tokyotalk
PDF
Ngrep commands
PPTX
Disruptor
PDF
Introduction to ns2
PDF
PPT
Venkat ns2
PDF
Abusing text/template for data transformation
ODP
Nach os network
ODP
Nach os network
NS-2 Tutorial
Introduction to NS2 - Cont..
Ns 2 Network Simulator An Introduction
Session 1 introduction to ns2
Ns network simulator
Stackless Python In Eve
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Network simulator 2
Building Network Functions with eBPF & BCC
Network Simulator Tutorial
Network programming using python
tokyotalk
Ngrep commands
Disruptor
Introduction to ns2
Venkat ns2
Abusing text/template for data transformation
Nach os network
Nach os network
Ad

Similar to #2 (UDP) (20)

PPT
Udp Programming
PPT
Udp Programming
PPT
TCP IP
PPTX
Advance Java-Network Programming
PDF
PPT
Pemrograman Jaringan
PPT
Easy Steps to implement UDP Server and Client Sockets
PDF
Lecture6
PPTX
Networking in Java
PDF
ikh331-06-distributed-programming
PPTX
#1 (TCPvs. UDP)
PPTX
Networking in Python2025 (programs allll)
PPT
Networking.ppt(client/server, socket) uses in program
PPT
Java Socket Programming
PDF
Chapter 3 : User Datagram Protocol (UDP)
DOCX
Lab manual cn-2012-13
PPT
Unit 2 DSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDS.ppt
PPT
Chapter 4 slides
PDF
nw-lab_dns-server.pdf
DOCX
Distributed systems
Udp Programming
Udp Programming
TCP IP
Advance Java-Network Programming
Pemrograman Jaringan
Easy Steps to implement UDP Server and Client Sockets
Lecture6
Networking in Java
ikh331-06-distributed-programming
#1 (TCPvs. UDP)
Networking in Python2025 (programs allll)
Networking.ppt(client/server, socket) uses in program
Java Socket Programming
Chapter 3 : User Datagram Protocol (UDP)
Lab manual cn-2012-13
Unit 2 DSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDSDS.ppt
Chapter 4 slides
nw-lab_dns-server.pdf
Distributed systems
Ad

More from Ghadeer AlHasan (20)

PPTX
[C++ Tutorial ] #9 Classes
PPTX
[C++ Tutorial] #8 Files
PPTX
[C++ Tutorial] #7- Linked List
PPTX
[Java] #8 String and Inner Class
PPTX
[C++ Tutorial] #6- Pointers
PPTX
[Java] #7 - Input & Output Stream
PPTX
[C++] #5 - Structures
PPTX
#6- Arrays and Collections Framework
PPTX
5- Overriding and Abstraction In Java
PPTX
4- Inheritance, Aggregation, Encapsulation and Overloading
PPTX
3- Operators in Java
PPTX
2- Introduction to java II
PPTX
1- Introduction to java
PPTX
0- Overview
PPTX
4- Arrays
PPTX
3- Functions
PPTX
2- Control Structures
PPTX
1- Languages Basics
PPTX
#8 (Java Message Service)
PPTX
#7 (Java Message Service)
[C++ Tutorial ] #9 Classes
[C++ Tutorial] #8 Files
[C++ Tutorial] #7- Linked List
[Java] #8 String and Inner Class
[C++ Tutorial] #6- Pointers
[Java] #7 - Input & Output Stream
[C++] #5 - Structures
#6- Arrays and Collections Framework
5- Overriding and Abstraction In Java
4- Inheritance, Aggregation, Encapsulation and Overloading
3- Operators in Java
2- Introduction to java II
1- Introduction to java
0- Overview
4- Arrays
3- Functions
2- Control Structures
1- Languages Basics
#8 (Java Message Service)
#7 (Java Message Service)

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
Sustainable Sites - Green Building Construction
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Well-logging-methods_new................
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
additive manufacturing of ss316l using mig welding
PPT
Project quality management in manufacturing
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
composite construction of structures.pdf
Sustainable Sites - Green Building Construction
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
bas. eng. economics group 4 presentation 1.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Structs to JSON How Go Powers REST APIs.pdf
Well-logging-methods_new................
Operating System & Kernel Study Guide-1 - converted.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
additive manufacturing of ss316l using mig welding
Project quality management in manufacturing
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original
Internet of Things (IOT) - A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Construction Project Organization Group 2.pptx
Foundation to blockchain - A guide to Blockchain Tech

#2 (UDP)

  • 1. Distributed Systems Lecture -2- Created by : Eng. Ghadeer Al Hasan UDP
  • 2. 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.
  • 3. 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.
  • 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 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.
  • 8. 3rd Step 7 Invoke a send() or receive() call on socket object void send(DatagramPacket packet) void receive(DatagramPacket packet)
  • 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 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]; } }
  • 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){ 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(); }
  • 17. 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()); } }
  • 18. 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()); } }
  • 19. 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(); }
  • 20. 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()); } }
  • 21. 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()); } }
  • 22. 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(); }
  • 24. References 24 - YouTube link https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ - GitHub https://github.com/Ghadeerof