SlideShare a Scribd company logo
3
Most read
8
Most read
9
Most read
Datagram Socket class
&
Datagram Packet class
PREPARED BY
Ruchi Maurya
Contents
• Introduction
• Datagram Socket class
• Datagram Packet class
• Example
Ruchi Maurya
Introduction
• Datagram is a bundle of information passed between machines
• Java implements datagrams on top of the UDP protocol by using two classes:
i. Datagram Packet class (Data container)
ii. Datagram Socket class (Mechanism used)
Ruchi Maurya
Datagram Socket class
• The Datagram Socket class represents a connection-less socket for sending
or receiving datagram packets
• The Datagram Socket object is the data container used to send or receive the
Datagram packets
• No guarantee of its content , arrival or arrival time
Ruchi Maurya
Datagram Socket class
• It defines four public constructors:
• DatagramSocket() throws SocketException: Creates a DatagramSocket
bound to any unused port
• DatagramSocket(int port) throws SocketException: Creates a
DatagramSocket bound to Specified port
• DatagramSocket(int port, InetAddress ipAddress) throws SocketException:
Constructs a DatagramSocket bound to Specified port & InetAddress
• DatagramSocket(SocketAddress address) throws SocketException: Creates
a DatagramSocket bound to Specified SocketAddress
Ruchi Maurya
Datagram Packet class
• DatagramPacket is a message that can be sent or received
• When multiple packets are sent, it may arrive in any order
• Packet Delivery is not guaranteed
Ruchi Maurya
Datagram Packet class
• Commonly used constructors:
• DatagramPacket(byte data[ ], int size): This constructor is used to receive the
packets
• DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port): This
constructor is used to send the packets
Ruchi Maurya
Example: Sending datagramPacket by DatagramSocket
import java.net.*;
public class Dsender
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
Ruchi Maurya
Example: Receiving datagramPacket by DatagramSocket
import java.net.*;
public class Dreceiver
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}
Ruchi Maurya
Thanks A Lot….!!
Ruchi Maurya

More Related Content

PPT
Software Engineering (Testing techniques)
PPT
Java Networking
PPT
JDBC Architecture and Drivers
PPT
Unit 3 Network Layer PPT
PPTX
Tcp/ip server sockets
PPSX
Flow Control
PPT
Socket programming
PPTX
Application Layer
Software Engineering (Testing techniques)
Java Networking
JDBC Architecture and Drivers
Unit 3 Network Layer PPT
Tcp/ip server sockets
Flow Control
Socket programming
Application Layer

What's hot (20)

PDF
Network Layer Numericals
PPTX
Strings in Java
PPTX
Exception Handling in C#
PPT
Serialization/deserialization
PPTX
Inheritance ppt
PPT
Java Socket Programming
PPT
IP security Part 1
 
PDF
Servlet and servlet life cycle
PDF
IT6601 MOBILE COMPUTING
PPT
Fundamentals of cryptography
PPTX
Android Intent.pptx
PPTX
ip security
PPTX
Multi-threaded Programming in JAVA
PPT
block ciphers
PDF
Python network programming
PPT
Operator Overloading
PPT
Conventional Encryption NS2
PDF
Report face recognition : ArganRecogn
PPT
Network programming in Java
Network Layer Numericals
Strings in Java
Exception Handling in C#
Serialization/deserialization
Inheritance ppt
Java Socket Programming
IP security Part 1
 
Servlet and servlet life cycle
IT6601 MOBILE COMPUTING
Fundamentals of cryptography
Android Intent.pptx
ip security
Multi-threaded Programming in JAVA
block ciphers
Python network programming
Operator Overloading
Conventional Encryption NS2
Report face recognition : ArganRecogn
Network programming in Java
Ad

Similar to Java- Datagram Socket class & Datagram Packet class (20)

PDF
Chapter 3 : User Datagram Protocol (UDP)
PDF
28 networking
PPTX
Datagrams
PPTX
Java socket presentation
PPTX
5_6278455688045789623.pptx
PPTX
Advance Java-Network Programming
PPT
Easy Steps to implement UDP Server and Client Sockets
PPT
UDP-Dept Businesss Computing university .ppt
PDF
Core Java Programming Language (JSE) : Chapter XII - Threads
PDF
PPT
Udp Programming
PPT
Udp Programming
PPT
Networking.ppt(client/server, socket) uses in program
PPTX
PPTX
Java networking
PPT
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
PPTX
Networking in Java
PDF
Lecture6
PPTX
Client server chat application
Chapter 3 : User Datagram Protocol (UDP)
28 networking
Datagrams
Java socket presentation
5_6278455688045789623.pptx
Advance Java-Network Programming
Easy Steps to implement UDP Server and Client Sockets
UDP-Dept Businesss Computing university .ppt
Core Java Programming Language (JSE) : Chapter XII - Threads
Udp Programming
Udp Programming
Networking.ppt(client/server, socket) uses in program
Java networking
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
Networking in Java
Lecture6
Client server chat application
Ad

More from Ruchi Maurya (7)

PPTX
Computer graphics - bresenham line drawing algorithm
PPTX
SHA- Secure hashing algorithm
PPTX
Nams- Roots of equations by numerical methods
PPTX
Types of Addressing modes- COA
PPTX
Social networking
PPTX
Random access memory
PPTX
open system interconnection
Computer graphics - bresenham line drawing algorithm
SHA- Secure hashing algorithm
Nams- Roots of equations by numerical methods
Types of Addressing modes- COA
Social networking
Random access memory
open system interconnection

Recently uploaded (20)

PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
master seminar digital applications in india
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Pharma ospi slides which help in ospi learning
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Week 4 Term 3 Study Techniques revisited.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
master seminar digital applications in india
O5-L3 Freight Transport Ops (International) V1.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
TR - Agricultural Crops Production NC III.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pharma ospi slides which help in ospi learning
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O7-L3 Supply Chain Operations - ICLT Program
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Types and Its function , kingdom of life
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Java- Datagram Socket class & Datagram Packet class

  • 1. Datagram Socket class & Datagram Packet class PREPARED BY Ruchi Maurya
  • 2. Contents • Introduction • Datagram Socket class • Datagram Packet class • Example Ruchi Maurya
  • 3. Introduction • Datagram is a bundle of information passed between machines • Java implements datagrams on top of the UDP protocol by using two classes: i. Datagram Packet class (Data container) ii. Datagram Socket class (Mechanism used) Ruchi Maurya
  • 4. Datagram Socket class • The Datagram Socket class represents a connection-less socket for sending or receiving datagram packets • The Datagram Socket object is the data container used to send or receive the Datagram packets • No guarantee of its content , arrival or arrival time Ruchi Maurya
  • 5. Datagram Socket class • It defines four public constructors: • DatagramSocket() throws SocketException: Creates a DatagramSocket bound to any unused port • DatagramSocket(int port) throws SocketException: Creates a DatagramSocket bound to Specified port • DatagramSocket(int port, InetAddress ipAddress) throws SocketException: Constructs a DatagramSocket bound to Specified port & InetAddress • DatagramSocket(SocketAddress address) throws SocketException: Creates a DatagramSocket bound to Specified SocketAddress Ruchi Maurya
  • 6. Datagram Packet class • DatagramPacket is a message that can be sent or received • When multiple packets are sent, it may arrive in any order • Packet Delivery is not guaranteed Ruchi Maurya
  • 7. Datagram Packet class • Commonly used constructors: • DatagramPacket(byte data[ ], int size): This constructor is used to receive the packets • DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port): This constructor is used to send the packets Ruchi Maurya
  • 8. Example: Sending datagramPacket by DatagramSocket import java.net.*; public class Dsender { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(); String str = "Welcome java"; InetAddress ip = InetAddress.getByName("127.0.0.1"); DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000); ds.send(dp); ds.close(); } } Ruchi Maurya
  • 9. Example: Receiving datagramPacket by DatagramSocket import java.net.*; public class Dreceiver { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(3000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp); String str = new String(dp.getData(), 0, dp.getLength()); System.out.println(str); ds.close(); } } Ruchi Maurya