Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Prepared by: Eng/ Basma Elshoky

Section 7: User Datagram Protocol (UDP)


Socket Part III

-The User Datagram Protocol (UDP) is an alternative transport layer protocol for
sending data over IP that is very quick, but not reliable. When you send UDP data,
you have no way of knowing whether it arrived.
-Clearly, UDP isn’t a good match for applications like FTP that require reliable
transmission of data over potentially unreliable networks. However, there are many
kinds of applications in which raw speed is more important than getting every bit
right. Like audio and video streaming.
-The difference between TCP and UDP is often explained by analogy with the phone
system and the post office. TCP is like the phone system. When you dial a number,
the phone is answered and a connection is established between the two parties. As
you talk, you know that the other party hears your words in the order in which you
say them. If the phone is busy or no one answers, you find out right away. UDP, by
contrast, is like the postal system. You send packets of mail to an address. Most of
the letters arrive, but some may be lost on the way. The letters probably arrive in the
order in which you sent them, but that’s not guaranteed.

-Java’s implementation of UDP is split into two classes:


• DatagramPacket : stuffs bytes of data into UDP packets called datagrams
and lets you unstuff datagrams that you receive.
• Datagram Socket: sends as well as receives UDP datagrams.

-To send data, you put the data in a DatagramPacket and send the packet using a
DatagramSocket. To receive data, you take a DatagramPacket object from a
DatagramSocket and then inspect the contents of the packet.
Prepared by: Eng/ Basma Elshoky

-In UDP, everything about a datagram, including the address to which it is directed,
is included in the packet itself; the socket only needs to know the local port on which
to listen or send.
-A single DatagramSocket can send data to and receive data from many independent
hosts. The socket isn’t dedicated to a single connection, as it is in TCP. UDP doesn’t
have any concept of a connection between two hosts; it only knows about individual
datagrams.

DatagramPacket class
Constructor Description
DatagramPacket(byte[] buf, int length) Constructs a DatagramPacket for
receiving packets of length length.
DatagramPacket(byte[] buf, int length, Constructs a DatagramPacket for
InetAddress address, int port) sending packets of length length to the
specified address and port.

Method Description
byte[] getData() Returns the data buffer of the packet.
void setData(byte[] buf) Sets the data buffer for the packet.
int getLength() Returns the length of the packet's data.
void setLength(int length) Sets the length of the packet's data.
InetAddress getAddress() Returns the IP address of the packet's
destination.
void setAddress(InetAddress Sets the IP address of the packet's destination.
address)
int getPort() Returns the port number of the packet's
destination.
void setPort(int port) Sets the port number of the packet's destination.
Prepared by: Eng/ Basma Elshoky

DatagramSocket class

Constructor Description
DatagramSocket() Constructs a datagram socket and binds it to
any available port on the local host machine.
DatagramSocket(int port) Constructs a datagram socket and binds it to
the specified port on the local host machine.
DatagramSocket(int port, InetAddress Constructs a datagram socket and binds it to
address) the specified port on the specified local
address.

Method Description
void send(DatagramPacket p) Sends a datagram packet from this socket.
void receive(DatagramPacket p) Receives a datagram packet from this socket.
void connect(InetAddress address, int Connects the socket to a remote address and port.
port)
void disconnect() Disconnects the socket.
void close() Closes the socket.
int getPort() Returns the local port to which this socket is
bound.
InetAddress getInetAddress() Returns the address of the endpoint this socket is
connected to, or null if it's not connected.
int getLocalPort() Returns the local port number to which this socket
is bound.
InetAddress getLocalAddress() Returns the local address to which this socket is
bound.
void setSoTimeout(int timeout) Sets the timeout in milliseconds for blocking
receive calls.
int getSoTimeout() Returns the timeout in milliseconds for blocking
receive calls.
void setReceiveBufferSize(int size) Sets the size of the receive buffer used by this
socket.
int getReceiveBufferSize() Returns the size of the receive buffer used by this
socket.
void setSendBufferSize(int size) Sets the size of the send buffer used by this socket.
int getSendBufferSize() Returns the size of the send buffer used by this
socket.
Prepared by: Eng/ Basma Elshoky

void setReuseAddress(boolean on) Sets the SO_REUSEADDR socket option.


boolean getReuseAddress() Tests if the SO_REUSEADDR socket option is
enabled.
void setBroadcast(boolean on) Sets the SO_BROADCAST socket option.
boolean getBroadcast() Tests if the SO_BROADCAST socket option is
enabled.
void setTrafficClass(int tc) Sets the IP_TOS socket option.
int getTrafficClass() Returns the value of the IP_TOS socket option.

Example:
import java.io.*;
import java.net.*;
import java.util.*;
public class App {
public static void main(String[] args) throws Exception {

try {
// Creating a DatagramSocket without specifying a port (let the system assign
one)
DatagramSocket socket = new DatagramSocket();

// Connecting the socket to a remote address and port


InetAddress remoteAddress = InetAddress.getByName("localhost ");
int remotePort = 80;
socket.connect(remoteAddress, remotePort);

// Sending a DatagramPacket
String message = "Hello, world!";
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length);
socket.send(sendPacket);
System.out.println("Sent message: " + message);

// Testing if SO_BROADCAST socket option is enabled


boolean isBroadcastEnabled = socket.getBroadcast();
System.out.println("Is broadcast enabled: " + isBroadcastEnabled);
Prepared by: Eng/ Basma Elshoky

// Disconnecting the socket


socket.disconnect();

// Closing the socket


socket.close();
} catch (Exception e) {
e.printStackTrace();
}
} //end void
}

In this example:
• We create a DatagramSocket without specifying a port, letting the system assign one
automatically using the default constructor DatagramSocket().
• We connect the socket to a remote address (localhost) and port 80 using the connect method.
• We send a message "Hello, world!" to the connected remote address using the send method.
• We check if the SO_BROADCAST socket option is enabled using the getBroadcast
method.
• We disconnect the socket using the disconnect method.
• Finally, we close the socket using the close method.

H.W: Write the code that received the message.

You might also like