SlideShare a Scribd company logo
1 of 30
Download to read offline
Distributed Systems
Lecture -1-
Created by :
Eng. Ghadeer Al Hasan
TCP vs. UDP
Connection-oriented vs. Connectionless 1
TCP UDP
TCP is a connection-orientedprotocol.
Connection-orientationmeans that the
communicating devices should establisha
connectionbefore transmitting data and
should close the connectionafter
transmitting the data.
UDP is the Datagramorientedprotocol.
This is because there is no overhead for
opening a connection, maintaining a
connection, And terminating a connection.
UDP is efficientFor broadcast and multicast
type of networktransmission.
Reliability 2
TCP UDP
TCP provides the delivery guarantee,
whichmeans a message sent using TCP
protocol is guaranteedto be delivered to
the client. If a message is lost in transits
thenits recovered using resending, which
is handled by TCP protocol itself
UDP is unreliable, it doesn't provide any
deliveryguarantee. A datagrampackage
may be lost in transits. That's why UDP is
not suitable for programs which require
guaranteed delivery.
Error Checking 3
TCP UDP
TCP provides extensive error checking
mechanisms. It is becauseit provides flow
control and acknowledgment of data.
UDP has only the basic error checking
mechanismusing checksums.
Ordering 4
TCP UDP
TCP guarantees the order of message. The
message will be delivered to the client in
the same order, the server has sent, though
it's possible they may reach out of order to
the other end of the network. TCP protocol
will do all sequencing and ordering for
you
UDP doesn't provide any ordering or
sequencing guarantee.
Speed 5
TCP UDP
TCP is slow.
TCP does has to createa connection,
ensure guaranteedand ordereddelivery, it
does a lot more than UDP.
UPDis fast.
UDP is more suitable where speed is a
concern, for example, online video
streaming, telecast or online multiplayer
games
Retransmission 6
TCP UDP
Retransmissionof lost packets is possible
in TCP.
There is no retransmission of lost packets in
User DatagramProtocol (UDP).
Heavyweight vs. Lightweight 7
TCP UDP
TCP is heavyweight.
Because of the overheadmentionedabove
UPDis lightweight.
UDP is deliver a message without bearing
any overhead of creating connection and
guaranteeing deliveryor order guarantee
keeps it light weight.
Header Size 8
TCP UDP
TCP has bigger header thanUDP.
TCP header size is 20 bytes.
UPDhas smallest header than TCP.
UDP Header size is 8 bytes.
Congestion or Flow Control 9
TCP UDP
TCP does Flow Control. It requires three
packets to set up a socket connection
before any userdata can be sent.
TCP handles reliabilityand congestion
control.
UDP does not havean option for flow
control.
Using 10
TCP UDP
TCP is usedby HTTP, HTTPs, FTP, SMTP
and Telnet
UDP is used by DNS, DHCP, TFTP, SNMP,
RIP, and VoIP.
Socket Programming in Java
Server Side Programming… 12
Establisha Socket Connection
- To write a server application two sockets are needed.
- A ServerSocket which waits for the client requests (when a client makes a new Socket())
- Socket socket to use for communication withthe client.
ServerSocket serverSocket = new ServerSocket(PORT);
1st argument: TCPport
Server Side Programming 13
Communication
- getOutputStream() method is usedto send the output through the socket.
- getInputStream() method is used to receive the input through the socket.
Close the Connection
- After finishing, it is important to close theconnectionby closing thesocket as well as input/output
streams.
Client Side Programming 14
Establisha Socket Connection :
- To connect to other machine we needa socket connection.
- A socket connectionmeans the two machines have informationabout eachother’s networklocation
- (IP Address) and TCP port
- The java.net.Socket classrepresents a Socket
Socket socket = new Socket(“127.0.0.1”, 5000)
- 1st argument : IP address of Server (127.0.0.1 is the IP address of local host)
- 2nd argument : TCP Port (Just a number representing which application to run on a
server, e.g. : HTTP runs on port 80, Port number can be from0 to 65535)
Client Side Programming… 15
- Communication :
To communicateover a socket connection, streams are used to bothinputand output the data.
- Closing the connection:
The socket connection is closed explicitly oncethe message to server is sent
Java Implementation
16
private Socket socket = null;
private ServerSocket serverSocket = null;
private DataInputStream input = null;
Server Side
private void openSocket(){
try{
serverSocket = new ServerSocket(Constants.PORT);
log("Server Started...");
}catch(IOException ex){
log("start server : " + ex.getMessage());
}
}
private void waitingConnection(){
log("Waiting for client...");
try {
socket = serverSocket.accept();
log("Client accepted");
} catch (IOException ex) {
log("waiting Connection : " + ex.getMessage());
}
}
set it zero, what
is happens?
17
private void openInputStream(){
try{
input = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
}catch(IOException ex){
log("open InputStream : " + ex.getMessage());
}
}
Server Side…
private String read(){
String line = "";
try{
line = input.readUTF();
}catch(IOException ex){
log("read : " + ex.getMessage());
}
return line;
}
private void closeInputStream(){
try{
input.close();
}catch(IOException ex){
log("close Input Stream : " + ex.getMessage());
}
}
18
private void closeSocket(){
try{
serverSocket.close();
socket.close();
}catch(IOException ex){
log("close socket : " + ex.getMessage());
}
}
Server Side…
private void log(String message){
System.out.println(message);
}
19
private void startConnection(){
openSocket();
waitingConnection();
openInputStream();
}
Server Side…
private void Connecting(){
String line = "";
while(! line.equals(Constants.STOP)){
line = read();
System.out.println("Client : " + line);
}
}
private void stopConnection(){
closeSocket();
closeInputStream();
}
20Server Side…
public static void main(String[] args){
Server server = new Server();
server.startConnection();
server.Connecting();
server.stopConnection();
}
21
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
Client Side
private void openSocket(){
try{
socket= new Socket(Constants.IP, Constants.PORT);
}catch(UnknownHostException ex){
log("openSocket : " + ex.getMessage());
}catch(IOException ex){
log("openSocket : " + ex.getMessage());
}
} private void openStreams(){
try{
input = new DataInputStream((System.in));
output = new DataOutputStream(socket.getOutputStream());
}catch(IOException ex){
log("openStreams : " + ex.getMessage());
}
}
22
private void closeStreams(){
try{
input.close();
output.close();
}catch(IOException ex){
log("closeStreams : " + ex.getMessage());
}
}
Client Side…
private void closeSocket(){
try{
socket.close();
}catch(IOException ex){
log("closeSocket : " + ex.getMessage());
}
} private String read(){
String line = "";
try{
line = input.readLine();
}catch(IOException ex){
log("read : " + ex.getMessage());
}
return line;
}
23
private void write(String message){
try{
output.writeUTF(message);
}catch(IOException ex){
log("write : " + ex.getMessage());
}
}
Client Side…
private String read(){
String line = "";
try{
line = input.readLine();
}catch(IOException ex){
log("read : " + ex.getMessage());
}
return line;
}
24
private void startConnection(){
openSocket();
if(socket != null){
openStreams();
}
log("Start Client");
}
Client Side…
private void connecting(){
String line = "";
while(! line.equals(Constants.STOP)){
line = read();
write(line);
}
}
private void stopConnection(){
closeStreams();
closeSocket();
log("Close Client");
}
25Client Side…
public static void main(String[] args) {
Client client = new Client();
client.startConnection();
client.connecting();
client.stopConnection();
}
26Run
References 28
- YouTube link
https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ
- GitHub
https://github.com/Ghadeerof
End Lecture

More Related Content

What's hot

What's hot (20)

IPC SOCKET
IPC SOCKETIPC SOCKET
IPC SOCKET
 
TCP vs UDP / Sumiet23
TCP vs UDP / Sumiet23TCP vs UDP / Sumiet23
TCP vs UDP / Sumiet23
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
 
Np unit2
Np unit2Np unit2
Np unit2
 
Lect16
Lect16Lect16
Lect16
 
Chap 19 ftp & tftp
Chap 19 ftp & tftpChap 19 ftp & tftp
Chap 19 ftp & tftp
 
Np unit iv ii
Np unit iv iiNp unit iv ii
Np unit iv ii
 
Socket Programming using Java
Socket Programming using JavaSocket Programming using Java
Socket Programming using Java
 
Transport layer interface
Transport layer interface Transport layer interface
Transport layer interface
 
Ports & sockets
Ports  & sockets Ports  & sockets
Ports & sockets
 
Puertos tcp & udp
Puertos tcp & udpPuertos tcp & udp
Puertos tcp & udp
 
Udp
UdpUdp
Udp
 
Tcp vs udp difference and comparison diffen
Tcp vs udp   difference and comparison   diffenTcp vs udp   difference and comparison   diffen
Tcp vs udp difference and comparison diffen
 
Ch21
Ch21Ch21
Ch21
 
Udp vs-tcp
Udp vs-tcpUdp vs-tcp
Udp vs-tcp
 
Tcpip
TcpipTcpip
Tcpip
 
Tcpdump
TcpdumpTcpdump
Tcpdump
 
Tcp vs udp
Tcp vs udpTcp vs udp
Tcp vs udp
 
What is the difference between udp and tcp internet protocols
What is the difference between udp and tcp internet protocols What is the difference between udp and tcp internet protocols
What is the difference between udp and tcp internet protocols
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programming
 

Similar to #1 (TCPvs. UDP) (20)

Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Sockets
SocketsSockets
Sockets
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 
Networking
NetworkingNetworking
Networking
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
Python networking
Python networkingPython networking
Python networking
 
28 networking
28  networking28  networking
28 networking
 
Ipc
IpcIpc
Ipc
 
07 coms 525 tcpip - udp
07    coms 525 tcpip - udp07    coms 525 tcpip - udp
07 coms 525 tcpip - udp
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Sockets
Sockets Sockets
Sockets
 

More from Ghadeer AlHasan

[C++ Tutorial ] #9 Classes
[C++ Tutorial ] #9 Classes[C++ Tutorial ] #9 Classes
[C++ Tutorial ] #9 ClassesGhadeer AlHasan
 
[C++ Tutorial] #7- Linked List
[C++ Tutorial] #7- Linked List[C++ Tutorial] #7- Linked List
[C++ Tutorial] #7- Linked ListGhadeer AlHasan
 
[Java] #8 String and Inner Class
[Java] #8 String and Inner Class[Java] #8 String and Inner Class
[Java] #8 String and Inner ClassGhadeer AlHasan
 
[C++ Tutorial] #6- Pointers
[C++ Tutorial] #6- Pointers [C++ Tutorial] #6- Pointers
[C++ Tutorial] #6- Pointers Ghadeer AlHasan
 
[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output StreamGhadeer AlHasan
 
#6- Arrays and Collections Framework
#6- Arrays and Collections Framework#6- Arrays and Collections Framework
#6- Arrays and Collections FrameworkGhadeer AlHasan
 
5- Overriding and Abstraction In Java
5- Overriding and Abstraction In Java5- Overriding and Abstraction In Java
5- Overriding and Abstraction In JavaGhadeer AlHasan
 
4- Inheritance, Aggregation, Encapsulation and Overloading
4- Inheritance, Aggregation, Encapsulation and Overloading4- Inheritance, Aggregation, Encapsulation and Overloading
4- Inheritance, Aggregation, Encapsulation and OverloadingGhadeer AlHasan
 
2- Introduction to java II
2-  Introduction to java II2-  Introduction to java II
2- Introduction to java IIGhadeer AlHasan
 
#8 (Java Message Service)
#8 (Java Message Service)#8 (Java Message Service)
#8 (Java Message Service)Ghadeer AlHasan
 
#7 (Java Message Service)
#7 (Java Message Service)#7 (Java Message Service)
#7 (Java Message Service)Ghadeer AlHasan
 

More from Ghadeer AlHasan (20)

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

Recently uploaded

Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Bahzad5
 
Guardians and Glitches: Navigating the Duality of Gen AI in AppSec
Guardians and Glitches: Navigating the Duality of Gen AI in AppSecGuardians and Glitches: Navigating the Duality of Gen AI in AppSec
Guardians and Glitches: Navigating the Duality of Gen AI in AppSecTrupti Shiralkar, CISSP
 
Mohs Scale of Hardness, Hardness Scale.pptx
Mohs Scale of Hardness, Hardness Scale.pptxMohs Scale of Hardness, Hardness Scale.pptx
Mohs Scale of Hardness, Hardness Scale.pptxKISHAN KUMAR
 
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratoryدليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide LaboratoryBahzad5
 
Technical Management of cement industry.pdf
Technical Management of cement industry.pdfTechnical Management of cement industry.pdf
Technical Management of cement industry.pdfMadan Karki
 
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...Amil baba
 
Basic Principle of Electrochemical Sensor
Basic Principle of  Electrochemical SensorBasic Principle of  Electrochemical Sensor
Basic Principle of Electrochemical SensorTanvir Moin
 
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfSummer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfNaveenVerma126
 
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptxVertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptxLMW Machine Tool Division
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
solar wireless electric vechicle charging system
solar wireless electric vechicle charging systemsolar wireless electric vechicle charging system
solar wireless electric vechicle charging systemgokuldongala
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabusViolet Violet
 
News web APP using NEWS API for web platform to enhancing user experience
News web APP using NEWS API for web platform to enhancing user experienceNews web APP using NEWS API for web platform to enhancing user experience
News web APP using NEWS API for web platform to enhancing user experienceAkashJha84
 
Carbohydrates principles of biochemistry
Carbohydrates principles of biochemistryCarbohydrates principles of biochemistry
Carbohydrates principles of biochemistryKomakeTature
 
Graphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesGraphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesDIPIKA83
 
Phase noise transfer functions.pptx
Phase noise transfer      functions.pptxPhase noise transfer      functions.pptx
Phase noise transfer functions.pptxSaiGouthamSunkara
 
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Apollo Techno Industries Pvt Ltd
 
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.pptOracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.pptDheerajKashnyal
 

Recently uploaded (20)

Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)
 
Guardians and Glitches: Navigating the Duality of Gen AI in AppSec
Guardians and Glitches: Navigating the Duality of Gen AI in AppSecGuardians and Glitches: Navigating the Duality of Gen AI in AppSec
Guardians and Glitches: Navigating the Duality of Gen AI in AppSec
 
Mohs Scale of Hardness, Hardness Scale.pptx
Mohs Scale of Hardness, Hardness Scale.pptxMohs Scale of Hardness, Hardness Scale.pptx
Mohs Scale of Hardness, Hardness Scale.pptx
 
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratoryدليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
 
Technical Management of cement industry.pdf
Technical Management of cement industry.pdfTechnical Management of cement industry.pdf
Technical Management of cement industry.pdf
 
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
Popular-NO1 Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialis...
 
Basic Principle of Electrochemical Sensor
Basic Principle of  Electrochemical SensorBasic Principle of  Electrochemical Sensor
Basic Principle of Electrochemical Sensor
 
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfSummer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
 
Présentation IIRB 2024 Marine Cordonnier.pdf
Présentation IIRB 2024 Marine Cordonnier.pdfPrésentation IIRB 2024 Marine Cordonnier.pdf
Présentation IIRB 2024 Marine Cordonnier.pdf
 
Lecture 4 .pdf
Lecture 4                              .pdfLecture 4                              .pdf
Lecture 4 .pdf
 
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptxVertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
Vertical- Machining - Center - VMC -LMW-Machine-Tool-Division.pptx
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
solar wireless electric vechicle charging system
solar wireless electric vechicle charging systemsolar wireless electric vechicle charging system
solar wireless electric vechicle charging system
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabus
 
News web APP using NEWS API for web platform to enhancing user experience
News web APP using NEWS API for web platform to enhancing user experienceNews web APP using NEWS API for web platform to enhancing user experience
News web APP using NEWS API for web platform to enhancing user experience
 
Carbohydrates principles of biochemistry
Carbohydrates principles of biochemistryCarbohydrates principles of biochemistry
Carbohydrates principles of biochemistry
 
Graphics Primitives and CG Display Devices
Graphics Primitives and CG Display DevicesGraphics Primitives and CG Display Devices
Graphics Primitives and CG Display Devices
 
Phase noise transfer functions.pptx
Phase noise transfer      functions.pptxPhase noise transfer      functions.pptx
Phase noise transfer functions.pptx
 
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
 
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.pptOracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
Oracle_PLSQL_basic_tutorial_with_workon_Exercises.ppt
 

#1 (TCPvs. UDP)

  • 1. Distributed Systems Lecture -1- Created by : Eng. Ghadeer Al Hasan TCP vs. UDP
  • 2. Connection-oriented vs. Connectionless 1 TCP UDP TCP is a connection-orientedprotocol. Connection-orientationmeans that the communicating devices should establisha connectionbefore transmitting data and should close the connectionafter transmitting the data. UDP is the Datagramorientedprotocol. This is because there is no overhead for opening a connection, maintaining a connection, And terminating a connection. UDP is efficientFor broadcast and multicast type of networktransmission.
  • 3. Reliability 2 TCP UDP TCP provides the delivery guarantee, whichmeans a message sent using TCP protocol is guaranteedto be delivered to the client. If a message is lost in transits thenits recovered using resending, which is handled by TCP protocol itself UDP is unreliable, it doesn't provide any deliveryguarantee. A datagrampackage may be lost in transits. That's why UDP is not suitable for programs which require guaranteed delivery.
  • 4. Error Checking 3 TCP UDP TCP provides extensive error checking mechanisms. It is becauseit provides flow control and acknowledgment of data. UDP has only the basic error checking mechanismusing checksums.
  • 5. Ordering 4 TCP UDP TCP guarantees the order of message. The message will be delivered to the client in the same order, the server has sent, though it's possible they may reach out of order to the other end of the network. TCP protocol will do all sequencing and ordering for you UDP doesn't provide any ordering or sequencing guarantee.
  • 6. Speed 5 TCP UDP TCP is slow. TCP does has to createa connection, ensure guaranteedand ordereddelivery, it does a lot more than UDP. UPDis fast. UDP is more suitable where speed is a concern, for example, online video streaming, telecast or online multiplayer games
  • 7. Retransmission 6 TCP UDP Retransmissionof lost packets is possible in TCP. There is no retransmission of lost packets in User DatagramProtocol (UDP).
  • 8. Heavyweight vs. Lightweight 7 TCP UDP TCP is heavyweight. Because of the overheadmentionedabove UPDis lightweight. UDP is deliver a message without bearing any overhead of creating connection and guaranteeing deliveryor order guarantee keeps it light weight.
  • 9. Header Size 8 TCP UDP TCP has bigger header thanUDP. TCP header size is 20 bytes. UPDhas smallest header than TCP. UDP Header size is 8 bytes.
  • 10. Congestion or Flow Control 9 TCP UDP TCP does Flow Control. It requires three packets to set up a socket connection before any userdata can be sent. TCP handles reliabilityand congestion control. UDP does not havean option for flow control.
  • 11. Using 10 TCP UDP TCP is usedby HTTP, HTTPs, FTP, SMTP and Telnet UDP is used by DNS, DHCP, TFTP, SNMP, RIP, and VoIP.
  • 13. Server Side Programming… 12 Establisha Socket Connection - To write a server application two sockets are needed. - A ServerSocket which waits for the client requests (when a client makes a new Socket()) - Socket socket to use for communication withthe client. ServerSocket serverSocket = new ServerSocket(PORT); 1st argument: TCPport
  • 14. Server Side Programming 13 Communication - getOutputStream() method is usedto send the output through the socket. - getInputStream() method is used to receive the input through the socket. Close the Connection - After finishing, it is important to close theconnectionby closing thesocket as well as input/output streams.
  • 15. Client Side Programming 14 Establisha Socket Connection : - To connect to other machine we needa socket connection. - A socket connectionmeans the two machines have informationabout eachother’s networklocation - (IP Address) and TCP port - The java.net.Socket classrepresents a Socket Socket socket = new Socket(“127.0.0.1”, 5000) - 1st argument : IP address of Server (127.0.0.1 is the IP address of local host) - 2nd argument : TCP Port (Just a number representing which application to run on a server, e.g. : HTTP runs on port 80, Port number can be from0 to 65535)
  • 16. Client Side Programming… 15 - Communication : To communicateover a socket connection, streams are used to bothinputand output the data. - Closing the connection: The socket connection is closed explicitly oncethe message to server is sent
  • 18. 16 private Socket socket = null; private ServerSocket serverSocket = null; private DataInputStream input = null; Server Side private void openSocket(){ try{ serverSocket = new ServerSocket(Constants.PORT); log("Server Started..."); }catch(IOException ex){ log("start server : " + ex.getMessage()); } } private void waitingConnection(){ log("Waiting for client..."); try { socket = serverSocket.accept(); log("Client accepted"); } catch (IOException ex) { log("waiting Connection : " + ex.getMessage()); } } set it zero, what is happens?
  • 19. 17 private void openInputStream(){ try{ input = new DataInputStream( new BufferedInputStream(socket.getInputStream())); }catch(IOException ex){ log("open InputStream : " + ex.getMessage()); } } Server Side… private String read(){ String line = ""; try{ line = input.readUTF(); }catch(IOException ex){ log("read : " + ex.getMessage()); } return line; } private void closeInputStream(){ try{ input.close(); }catch(IOException ex){ log("close Input Stream : " + ex.getMessage()); } }
  • 20. 18 private void closeSocket(){ try{ serverSocket.close(); socket.close(); }catch(IOException ex){ log("close socket : " + ex.getMessage()); } } Server Side… private void log(String message){ System.out.println(message); }
  • 21. 19 private void startConnection(){ openSocket(); waitingConnection(); openInputStream(); } Server Side… private void Connecting(){ String line = ""; while(! line.equals(Constants.STOP)){ line = read(); System.out.println("Client : " + line); } } private void stopConnection(){ closeSocket(); closeInputStream(); }
  • 22. 20Server Side… public static void main(String[] args){ Server server = new Server(); server.startConnection(); server.Connecting(); server.stopConnection(); }
  • 23. 21 private Socket socket = null; private DataInputStream input = null; private DataOutputStream output = null; Client Side private void openSocket(){ try{ socket= new Socket(Constants.IP, Constants.PORT); }catch(UnknownHostException ex){ log("openSocket : " + ex.getMessage()); }catch(IOException ex){ log("openSocket : " + ex.getMessage()); } } private void openStreams(){ try{ input = new DataInputStream((System.in)); output = new DataOutputStream(socket.getOutputStream()); }catch(IOException ex){ log("openStreams : " + ex.getMessage()); } }
  • 24. 22 private void closeStreams(){ try{ input.close(); output.close(); }catch(IOException ex){ log("closeStreams : " + ex.getMessage()); } } Client Side… private void closeSocket(){ try{ socket.close(); }catch(IOException ex){ log("closeSocket : " + ex.getMessage()); } } private String read(){ String line = ""; try{ line = input.readLine(); }catch(IOException ex){ log("read : " + ex.getMessage()); } return line; }
  • 25. 23 private void write(String message){ try{ output.writeUTF(message); }catch(IOException ex){ log("write : " + ex.getMessage()); } } Client Side… private String read(){ String line = ""; try{ line = input.readLine(); }catch(IOException ex){ log("read : " + ex.getMessage()); } return line; }
  • 26. 24 private void startConnection(){ openSocket(); if(socket != null){ openStreams(); } log("Start Client"); } Client Side… private void connecting(){ String line = ""; while(! line.equals(Constants.STOP)){ line = read(); write(line); } } private void stopConnection(){ closeStreams(); closeSocket(); log("Close Client"); }
  • 27. 25Client Side… public static void main(String[] args) { Client client = new Client(); client.startConnection(); client.connecting(); client.stopConnection(); }
  • 28. 26Run
  • 29. References 28 - YouTube link https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ - GitHub https://github.com/Ghadeerof