0% found this document useful (0 votes)
5 views18 pages

Unit-10 UDP

The document provides an overview of UDP (User Datagram Protocol) in Java network programming, highlighting its connectionless nature, efficiency, low latency, and suitability for real-time applications. It details the steps to create UDP clients and servers using the DatagramSocket and DatagramPacket classes, along with socket options and the advantages of using DatagramChannel for non-blocking I/O operations. Additionally, it covers the DatagramPacket class's methods and constructors, emphasizing its role in encapsulating UDP packet data and metadata.

Uploaded by

binaygupta191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Unit-10 UDP

The document provides an overview of UDP (User Datagram Protocol) in Java network programming, highlighting its connectionless nature, efficiency, low latency, and suitability for real-time applications. It details the steps to create UDP clients and servers using the DatagramSocket and DatagramPacket classes, along with socket options and the advantages of using DatagramChannel for non-blocking I/O operations. Additionally, it covers the DatagramPacket class's methods and constructors, emphasizing its role in encapsulating UDP packet data and metadata.

Uploaded by

binaygupta191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT-10: UDP

➢ In network programming with Java, UDP (User Datagram Protocol) is one of the transport protocols used for sending and receiving datagrams over
a network.
➢ UDP is a connectionless protocol, which means that it doesn't establish a persistent connection between the sender and receiver. Instead, it sends
individual packets called datagrams from one host to another.
➢ UDP (User Datagram Protocol) is an important transport protocol in computer networking due to the following reasons:
1. Simplicity and Efficiency: UDP is lightweight and doesn't have the overhead of establishing and maintaining connections, making it faster and
more efficient for certain applications.
2. Low Latency: UDP offers low latency by avoiding acknowledgments and retransmissions, making it suitable for real-time applications where
immediate data delivery is crucial.
3. Broadcast and Multicast: UDP supports broadcasting and multicasting, allowing a single packet to be sent to multiple recipients simultaneously,
which is useful for streaming media and online gaming.
4. Stateless Communication: UDP is stateless, enabling scalability for server applications that handle a large number of clients.
5. VoIP and Streaming: UDP is commonly used in Voice over IP (VoIP) and multimedia streaming applications, where real-time transmission takes
priority over reliability.
6. DNS: UDP is utilized by DNS for name resolution queries, which are typically small and time-sensitive.
7. Internet of Things (IoT): UDP is frequently used in IoT devices and applications, where quick and efficient transmission of small data packets is
essential. ALPINE SKI HOUSE 1
Krishna Pd. Acharya
STRUCTURE OF A UDP DATAGRAM

ALPINE SKI HOUSE


Krishna Pd. Acharya 2
UNIT-10: UDP
➢ To use UDP in network programming with Java, you can make use of the java.net package, which provides classes for networking
operations. The following steps outline the process of creating a UDP client and server using Java:

Client UDP:

1. Create a DatagramSocket object to send and receive UDP packets.

2. Create a DatagramPacket object to hold the data to be sent.

3. Convert your data into bytes and store it in the DatagramPacket object.

4. Specify the destination address and port number of the server by creating an instance of InetAddress and specifying the server's IP
address and port number.

5. Use the send() method of the DatagramSocket object to send the DatagramPacket to the server.

6. Close the DatagramSocket object when done.

ALPINE SKI HOUSE


Krishna Pd. Acharya 3
UNIT-10: UDP
UDP Server:

1. Create a DatagramSocket object and bind it to a specific port number on the server machine.

2. Create a byte array to hold the received data.

3. Create a DatagramPacket object to receive the incoming UDP packet and specify the size of the buffer.

4. Use the receive() method of the DatagramSocket object to receive the DatagramPacket.

5. Extract the data from the DatagramPacket object and process it as needed.

6. Optionally, send a response back to the client by creating a new DatagramPacket and using the send() method of the DatagramSocket.

7. Close the DatagramSocket object when done.

ALPINE SKI HOUSE


Krishna Pd. Acharya 4
UDP CLIENT PROGRAM

ALPINE SKI HOUSE


Krishna Pd. Acharya 5
UDP SERVER PROGRAM

ALPINE SKI HOUSE


Krishna Pd. Acharya 6
UDP ECHO CLIENT PROGRAM

ALPINE SKI HOUSE


Krishna Pd. Acharya 7
UDP ECHO SERVER PROGRAM

ALPINE SKI HOUSE


Krishna Pd. Acharya 8
THE DATAGRAMPACKET CLASS
• The DatagramPacket class in Java is part of the java.net package and represents a UDP packet or datagram. It encapsulates the data being sent
or received, along with the information about the source and destination addresses and ports. The DatagramPacket class provides methods to
access and manipulate the data and header information of the packet.
➢ The DatagramPacket class has two constructors:
➢ DatagramPacket(byte[] data, int length): Constructs a DatagramPacket with the specified byte array data and its length. This constructor is
commonly used when receiving data.
➢ DatagramPacket(byte[] data, int length, InetAddress address, int port): Constructs a DatagramPacket with the specified byte array data, its
length, destination address, and destination port. This constructor is commonly used when sending data.
➢ The DatagramPacket class provides several methods for accessing and modifying the packet's properties:
o getData(): Returns the byte array that holds the data of the packet.
o getLength(): Returns the length of the data in the packet.
o setData(byte[] data): Sets the data of the packet using the provided byte array.
o setLength(int length): Sets the length of the data in the packet.
o getAddress(): Returns the source or destination IP address of the packet.
o getPort(): Returns the source or destination port number of the packet.
o setAddress(InetAddress address): Sets the source or destination IP address of the packet.
o setPort(int port): Sets the source or destination port number of the packet.

ALPINE SKI HOUSE


Krishna Pd. Acharya 9
THE DATAGRAMPACKET CLASS

ALPINE SKI HOUSE 10


Krishna Pd. Acharya
THE DATAGRAMPACKET CLASS
o The DatagramSocket class in Java is a fundamental class for network communication using UDP (User Datagram
Protocol). It provides the functionality to send and receive datagrams (UDP packets) over the network.
1.Creating a DatagramSocket:
DatagramSocket(): Constructs a new DatagramSocket object that binds to any available local port.
DatagramSocket(int port): Constructs a new DatagramSocket object and binds it to the specified local port.
2.Sending Data:
send(DatagramPacket packet): Sends a datagram packet to the destination specified in the packet parameter.
3.Receiving Data:
receive(DatagramPacket packet): Receives a datagram packet and stores it in the provided packet object.
setSoTimeout(int timeout): Sets the maximum time to wait for a packet to arrive when receiving data. If no packet is
received within the specified timeout, a SocketTimeoutException is thrown.
4.Closing the DatagramSocket:
close(): Closes the DatagramSocket and releases any resources associated with it.
ALPINE SKI HOUSE 11
Krishna Pd. Acharya
THE DATAGRAMPACKET CLASS
5.Binding and Unbinding:
bind(SocketAddress localAddr): Binds the DatagramSocket to a specific local address and port.
bind(SocketAddress localAddr, boolean reuseAddr): Binds the DatagramSocket to a specific local address and port,
with an option to enable/disable the reuse of the address.
6.Getting Socket Options:
getLocalAddress(): Returns the local address to which the DatagramSocket is bound.
getLocalPort(): Returns the local port to which the DatagramSocket is bound.

ALPINE SKI HOUSE 12


Krishna Pd. Acharya
UDP PORT SCANNER
5

ALPINE SKI HOUSE 13


Krishna Pd. Acharya
DATAGRAMCHANNEL
DatagramChannel is a class in Java that represents a channel for sending and receiving UDP datagrams. It is part of the Java NIO (New I/O)
package, introduced in Java 1.4, which provides a non-blocking I/O API for efficient I/O operations.
The DatagramChannel class offers several advantages over the traditional DatagramSocket class when working with UDP:
Non-blocking I/O: DatagramChannel supports non-blocking I/O operations, allowing you to perform I/O operations without blocking the thread.
This can be useful in scenarios where you need to handle multiple connections simultaneously or perform other tasks while waiting for I/O
operations to complete.
Asynchronous I/O: DatagramChannel provides methods for asynchronous I/O operations using Java NIO's CompletionHandler interface. This
allows you to initiate I/O operations and specify a callback handler that will be notified when the operation completes.
Channel-based architecture: DatagramChannel follows the channel-based I/O architecture introduced in Java NIO. This architecture provides a
unified API for various types of channels (e.g., TCP channels, UDP channels, file channels) and promotes code reusability and modularity.
Improved performance: The Java NIO package, including DatagramChannel, is designed to provide better performance compared to the
traditional I/O operations in Java. It achieves this through the use of operating system features like kernel-level event notification and memory-
mapped I/O.
To use DatagramChannel, you can perform operations such as opening a channel, binding it to a specific local address and port, sending and
receiving datagrams, and closing the channel. It offers methods like open(), bind(), send(), receive(), and close() for these operations.

ALPINE SKI HOUSE 14


Krishna Pd. Acharya
DATAGRAMCHANNEL ECHO SERVER

ALPINE SKI HOUSE 15


Krishna Pd. Acharya
DATAGRAMCHANNEL ECHO CLIENT

ALPINE SKI HOUSE 16


Krishna Pd. Acharya
DATAGRAMCHANNEL ECHO CLIENT
• In Java, you can set socket options for UDP sockets using the SocketOption class and the setOption() method provided by the DatagramSocket class. Here's an overview of the six socket
options commonly used for UDP:
SO_TIMEOUT:
• This option sets the maximum time the receive() method of a DatagramSocket will block waiting for incoming data.
• The value is specified in milliseconds.
• Example usage: socket.setSoTimeout(5000);
SO_RCVBUF:
• This option sets the receive buffer size for the underlying socket.
• The value is specified in bytes.
• Example usage: socket.setReceiveBufferSize(8192);
SO_SNDBUF:
• This option sets the send buffer size for the underlying socket.
• The value is specified in bytes.
• Example usage: socket.setSendBufferSize(8192);
SO_REUSEADDR:
• This option allows multiple DatagramSocket instances to bind to the same local address and port, even if there are active connections on the socket.
• It is useful when you need to quickly reuse a socket that was recently closed.
• Example usage: socket.setReuseAddress(true);
SO_BROADCAST:
• This option enables or disables the ability to send broadcast messages.
• By default, UDP sockets are not allowed to send broadcast messages.
• Example usage: socket.setBroadcast(true);
IP_TOS:
• This option sets the Type of Service (ToS) field in the IP header.
• It allows you to specify the quality of service or priority for the outgoing packets.
• Example usage: socket.setTrafficClass(0x10);

ALPINE SKI HOUSE 17


Krishna Pd. Acharya
EXAMPLE OF UDP SOCKET EXAMPLE

ALPINE SKI HOUSE 18


Krishna Pd. Acharya

You might also like