SlideShare a Scribd company logo
Jan. 2004 1
Java Networking UDP
Yangjun Chen
Dept. Business Computing
University of Winnipeg
Jan. 2004 2
User Datagram Protocol
• UDP (User Datagrarn Protocol)
- a protocol that sends independent packets of data, called
datagrams,
from one computer to another with no guarantees about arrival
- not connection based like TCP
• If a UDP packet is lost, IT’S LOST.
• The packets appear in the order they are received not
necessarily in the order they were sent.
Jan. 2004 3
So Why UDP?
• Speed!
• UDP can be up to three times faster than TCP.
• There are applications where speed is more important than
reliability, such as audio and video data.
Jan. 2004 4
UDP
• Java classes using UDP
- java. net
DatagramPacket()
DatagramSocket()
Jan. 2004 5
UDP Classes
• Java has two classes for UDP support
• java.net.DatagramSocket
• java.net.DatagramPacket
• A DatagramSocket is used to send and receive
DatagramPacket.
• Since UDP is connectionless, streams are not used.
• The maximum size of a Datagrarm packet is
limited to slightly less than 64Kbytes.
Jan. 2004 6
UDP Classes
• DatagramSockets are connected to a port that allow for
sending and receiving of data.
• Unlike TCP sockets, there is no distinction between client and
server sockets in UDP.
• Also, a DatagramSocket can be used to send data to multiple
different hosts.
- This is because the address is stored in the packet, not in
the socket.
• There are 65,536 UDP ports as well as TCP ports that are
separate from each other.
Jan. 2004 7
DatagramPacket Class
• You construct a DatagrarmPacket by using one of the two
constructors.
- public DatagramPacket(byte[] data, int length)
- public DatagramPacket(byte[] data, int length,
InetAddress addr, int port)
• The byte array is passed by reference and not by value. A
change in its contents will change the packet.
Jan. 2004 8
DatagramPacket Class
• Example of using the constructors:
String data = "My UDP Packet”;
byte[] h = data.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length)
• You can also pass in the host and port to which the packet is to
be sent.
Jan. 2004 9
DatagramPacket Class
try {
InetAddress tr = new
InetAddress(" www.win.trlabs.ca”);
int port = 9100;
String data = "Another UDP Packet";
byte[] b = data.getBytes( );
DatagramPacket dp = new DatagramPacket(b,
b.length, tr,port);
}//try
catch (UnknownHostException e) {
System.err.printin(e);
}//catch
Jan. 2004 10
DatagramPacket Class
• After creation of a DatagramPacket, it is possible to change the
date, the length of the data, the port, or the address by using
the following methods:
setAddress(InetAddress addr)
setPort(int port)
setData(byte buf[])
setLength(int length)
• To retrieve the current status of a DatagramPacket, use the
corresponding get methods.
Jan. 2004 11
DatagramSocket Class
• This class is a connection to a port that does the transmitting
and receiving.
• Unlike TCP sockets, the same Datagramsocket can be used to
send and receive.
• The Datagramsocket class has three constructors.
Jan. 2004 12
DatagramSocket Class
• public DatagramSocket()
• public DatagramSocket(int port)
• public Datagramsocket(int port, InetAddress addr)
• all three constructors throw an IOException.
• The first constructor is mainly used to act as clients.
• The other two that specify a port and optionally an IP address
are intended for servers that must run on a well known port.
Jan. 2004 13
Sending UDP Datagrams
• 1) Convert the data into a byte array.
• 2) Pass this byte array, length of data, port and address to the
DatagramPacket constructor.
• 3) Create a DatagramSocket object.
• 4) Pass the Datagram packet to the send method.
Jan. 2004 14
Sending UDP Datagrams
try {
TnetAddress tr=new
InetAddress("win.tr1abs.ca”);
int pt=9100;
string data = "Sending a Datagram";
byte[] b = data.getBytes();
DatagramPacket dP=new
DatagramPacket(b,b.length,tr,pt);
}//try catch (UnknownHostException e) {
system.err.println(e);
}//catch
Jan. 2004 15
Sending UDP Datagrams
try {
DatagramSocket sender = new Datagramsocket();
sender.send(dP);
}//try
catch (IOException e) {
system.out.printIn(e);
}//catch
Jan. 2004 16
Receiving UDP Datagrams
• 1) Construct a DatagramSocket object on the port you wish to
listen.
• 2) Pass it an empty Datagrampacket object to the
Datagramsocket’s receive() method.
• 3) Use methods
getport ( ),
getAddress( ),
getData ( ),
getLength( )
to retrieve information about the data.
Jan. 2004 17
Receiving UDP Datagrams
try {
byte buf=new byte[655361;
DatagramPacket dp = new
DatagramPacket(buf, buf.length);
DatagramSocket ds=newDatagramSocket(9100);
ds.receive(dp);
byte[] data=dp.getData();
String s=new String(data, 0, data.getLength());
System.out.println(s);
}//try
catch (IOException e) {
System.err.println(e);
}//catch
Jan. 2004 18
UDP Echo Example
• As with the TCP echo port, the UDP echo port is port 7.
• When this port receives a datagram, it copies the data and
sends it back to the user.
• As with the TCP echo example, the UDP echo example will
read in data from System.in, send it to the echo server on port
7 and then display the results.
• Remember that since UDP is connectionless, a packet might be
lost somewhere between the client and the server.
Jan. 2004 19
UDP Echo Example
import java.io.*;
import java.net.*;
public class UDPEcho extends Thread {
static int port=7;
static volatile boolean running=false;
DatagramSocket ds;
public static void main(String args[]) {
InputstreamReader isr=new
InputStreamReader(System.in);
BufferedReader in = new
BufferedReader(isr);
String line;
Jan. 2004 20
UDP Echo Example
running=true;
try{
while ((line==in.readLine()) != null) {
byte[] data = line.getBytes();
DatagramPacket dP = new DatagramPacket
(data, data.length, server, port);
ds.send(dp);
Thread.yield();
}//while
}//try
Jan. 2004 21
UDP Echo Example
catch(IoException e) {
system.err.println(e); }
//catch
running = false; }
//main
public UDPEcho(DatagramSocket ds) {
this.ds=ds; }
//UDPEcho constructor
Jan. 2004 22
UDP Echo Example
public void run() {
byte[] buf = new byte[1024];
String line;
DatagramPacket incoming = new DatagramPacket (buf, buf.length);
while(running) {
try{
ds.receive(incoming);
byte[] data = incoming.getData();
line = new
String(data,0,incoming.getLength());
Jan. 2004 23
UDP Echo Example
System.out.println("Echo: ", line);
} //try
catch(IOException e){
system.err.println(e);
}//catch
}//while
}//run
}//UDPEcho class

More Related Content

PDF
Chapter 3 : User Datagram Protocol (UDP)
PPT
Udp Programming
PPT
Udp Programming
PPTX
Advance Java-Network Programming
PDF
Lecture6
PDF
Java- Datagram Socket class & Datagram Packet class
PPTX
Javanetworkingbasicssocketsoverview
PPTX
Java networking basics & sockets overview
Chapter 3 : User Datagram Protocol (UDP)
Udp Programming
Udp Programming
Advance Java-Network Programming
Lecture6
Java- Datagram Socket class & Datagram Packet class
Javanetworkingbasicssocketsoverview
Java networking basics & sockets overview

Similar to UDP-Dept Businesss Computing university .ppt (20)

PPTX
PPTX
Datagrams
PPTX
Java socket presentation
PPTX
Networking in Java
PPT
Easy Steps to implement UDP Server and Client Sockets
PPT
Network programming in Java
PPTX
#2 (UDP)
PPTX
Socket & Server Socket
PDF
28 networking
PPT
Networking.ppt(client/server, socket) uses in program
PPT
Java Socket Programming
PPT
Md13 networking
PPTX
5_6278455688045789623.pptx
PPT
Networking
PPT
Network programming in Java
PPT
Network Programming in Java
PPTX
User Datagram Protocol
PDF
PPT
2-CN_UDP_TCP_f7a922763a77c5ea2bc334f8e36c71f8.ppt
PPTX
Network programming in java - PPT
Datagrams
Java socket presentation
Networking in Java
Easy Steps to implement UDP Server and Client Sockets
Network programming in Java
#2 (UDP)
Socket & Server Socket
28 networking
Networking.ppt(client/server, socket) uses in program
Java Socket Programming
Md13 networking
5_6278455688045789623.pptx
Networking
Network programming in Java
Network Programming in Java
User Datagram Protocol
2-CN_UDP_TCP_f7a922763a77c5ea2bc334f8e36c71f8.ppt
Network programming in java - PPT
Ad

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Welding lecture in detail for understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Well-logging-methods_new................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
composite construction of structures.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
additive manufacturing of ss316l using mig welding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Digital Logic Computer Design lecture notes
PDF
PPT on Performance Review to get promotions
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
OOP with Java - Java Introduction (Basics)
Welding lecture in detail for understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
R24 SURVEYING LAB MANUAL for civil enggi
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Well-logging-methods_new................
bas. eng. economics group 4 presentation 1.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
573137875-Attendance-Management-System-original
composite construction of structures.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
additive manufacturing of ss316l using mig welding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Lecture Notes Electrical Wiring System Components
Digital Logic Computer Design lecture notes
PPT on Performance Review to get promotions
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Ad

UDP-Dept Businesss Computing university .ppt

  • 1. Jan. 2004 1 Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg
  • 2. Jan. 2004 2 User Datagram Protocol • UDP (User Datagrarn Protocol) - a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival - not connection based like TCP • If a UDP packet is lost, IT’S LOST. • The packets appear in the order they are received not necessarily in the order they were sent.
  • 3. Jan. 2004 3 So Why UDP? • Speed! • UDP can be up to three times faster than TCP. • There are applications where speed is more important than reliability, such as audio and video data.
  • 4. Jan. 2004 4 UDP • Java classes using UDP - java. net DatagramPacket() DatagramSocket()
  • 5. Jan. 2004 5 UDP Classes • Java has two classes for UDP support • java.net.DatagramSocket • java.net.DatagramPacket • A DatagramSocket is used to send and receive DatagramPacket. • Since UDP is connectionless, streams are not used. • The maximum size of a Datagrarm packet is limited to slightly less than 64Kbytes.
  • 6. Jan. 2004 6 UDP Classes • DatagramSockets are connected to a port that allow for sending and receiving of data. • Unlike TCP sockets, there is no distinction between client and server sockets in UDP. • Also, a DatagramSocket can be used to send data to multiple different hosts. - This is because the address is stored in the packet, not in the socket. • There are 65,536 UDP ports as well as TCP ports that are separate from each other.
  • 7. Jan. 2004 7 DatagramPacket Class • You construct a DatagrarmPacket by using one of the two constructors. - public DatagramPacket(byte[] data, int length) - public DatagramPacket(byte[] data, int length, InetAddress addr, int port) • The byte array is passed by reference and not by value. A change in its contents will change the packet.
  • 8. Jan. 2004 8 DatagramPacket Class • Example of using the constructors: String data = "My UDP Packet”; byte[] h = data.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length) • You can also pass in the host and port to which the packet is to be sent.
  • 9. Jan. 2004 9 DatagramPacket Class try { InetAddress tr = new InetAddress(" www.win.trlabs.ca”); int port = 9100; String data = "Another UDP Packet"; byte[] b = data.getBytes( ); DatagramPacket dp = new DatagramPacket(b, b.length, tr,port); }//try catch (UnknownHostException e) { System.err.printin(e); }//catch
  • 10. Jan. 2004 10 DatagramPacket Class • After creation of a DatagramPacket, it is possible to change the date, the length of the data, the port, or the address by using the following methods: setAddress(InetAddress addr) setPort(int port) setData(byte buf[]) setLength(int length) • To retrieve the current status of a DatagramPacket, use the corresponding get methods.
  • 11. Jan. 2004 11 DatagramSocket Class • This class is a connection to a port that does the transmitting and receiving. • Unlike TCP sockets, the same Datagramsocket can be used to send and receive. • The Datagramsocket class has three constructors.
  • 12. Jan. 2004 12 DatagramSocket Class • public DatagramSocket() • public DatagramSocket(int port) • public Datagramsocket(int port, InetAddress addr) • all three constructors throw an IOException. • The first constructor is mainly used to act as clients. • The other two that specify a port and optionally an IP address are intended for servers that must run on a well known port.
  • 13. Jan. 2004 13 Sending UDP Datagrams • 1) Convert the data into a byte array. • 2) Pass this byte array, length of data, port and address to the DatagramPacket constructor. • 3) Create a DatagramSocket object. • 4) Pass the Datagram packet to the send method.
  • 14. Jan. 2004 14 Sending UDP Datagrams try { TnetAddress tr=new InetAddress("win.tr1abs.ca”); int pt=9100; string data = "Sending a Datagram"; byte[] b = data.getBytes(); DatagramPacket dP=new DatagramPacket(b,b.length,tr,pt); }//try catch (UnknownHostException e) { system.err.println(e); }//catch
  • 15. Jan. 2004 15 Sending UDP Datagrams try { DatagramSocket sender = new Datagramsocket(); sender.send(dP); }//try catch (IOException e) { system.out.printIn(e); }//catch
  • 16. Jan. 2004 16 Receiving UDP Datagrams • 1) Construct a DatagramSocket object on the port you wish to listen. • 2) Pass it an empty Datagrampacket object to the Datagramsocket’s receive() method. • 3) Use methods getport ( ), getAddress( ), getData ( ), getLength( ) to retrieve information about the data.
  • 17. Jan. 2004 17 Receiving UDP Datagrams try { byte buf=new byte[655361; DatagramPacket dp = new DatagramPacket(buf, buf.length); DatagramSocket ds=newDatagramSocket(9100); ds.receive(dp); byte[] data=dp.getData(); String s=new String(data, 0, data.getLength()); System.out.println(s); }//try catch (IOException e) { System.err.println(e); }//catch
  • 18. Jan. 2004 18 UDP Echo Example • As with the TCP echo port, the UDP echo port is port 7. • When this port receives a datagram, it copies the data and sends it back to the user. • As with the TCP echo example, the UDP echo example will read in data from System.in, send it to the echo server on port 7 and then display the results. • Remember that since UDP is connectionless, a packet might be lost somewhere between the client and the server.
  • 19. Jan. 2004 19 UDP Echo Example import java.io.*; import java.net.*; public class UDPEcho extends Thread { static int port=7; static volatile boolean running=false; DatagramSocket ds; public static void main(String args[]) { InputstreamReader isr=new InputStreamReader(System.in); BufferedReader in = new BufferedReader(isr); String line;
  • 20. Jan. 2004 20 UDP Echo Example running=true; try{ while ((line==in.readLine()) != null) { byte[] data = line.getBytes(); DatagramPacket dP = new DatagramPacket (data, data.length, server, port); ds.send(dp); Thread.yield(); }//while }//try
  • 21. Jan. 2004 21 UDP Echo Example catch(IoException e) { system.err.println(e); } //catch running = false; } //main public UDPEcho(DatagramSocket ds) { this.ds=ds; } //UDPEcho constructor
  • 22. Jan. 2004 22 UDP Echo Example public void run() { byte[] buf = new byte[1024]; String line; DatagramPacket incoming = new DatagramPacket (buf, buf.length); while(running) { try{ ds.receive(incoming); byte[] data = incoming.getData(); line = new String(data,0,incoming.getLength());
  • 23. Jan. 2004 23 UDP Echo Example System.out.println("Echo: ", line); } //try catch(IOException e){ system.err.println(e); }//catch }//while }//run }//UDPEcho class