Вы находитесь на странице: 1из 7

FN513 NETWORK PROGRAMMING | 2015

CHAPTER 3
User Datagram Protocol

3.1 Understand Class UDP Socket


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, much less whether different pieces
of data arrived in the order in which you sent them. However, the pieces that do
arrive generally arrive quickly.
A datagram is an independent, self-contained message sent over the network
whose arrival, arrival time, and content are not guaranteed.
UDP:

Connectionless and unreliable service.


There isnt an initial handshaking phase.
Doesnt have a pipe.
transmitted data may be received out of order, or lost

Socket Programming with UDP:


No need for a welcoming socket.
No streams are attached to the sockets.
The sending hosts create packets by attaching the IP destination
address and port number to each batch of bytes.
The receiving process must unravel to receive packet to obtain the
packets information bytes.

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

FN513 NETWORK PROGRAMMING | 2015

Figure 3.1: Client/server socket interaction


Refer to Figure 3.1; UDP doesnt have any notion of a unique connection between
two hosts. One socket sends and receives all data directed to or from a port without
any concern for who the remote host is. A single DatagramSocket can send data to
and receive data from many independent hosts. The socket isnt dedicated to a
single connection. UDP doesnt have any concept of a connection between two
hosts; it only knows about individual datagrams. All the data you stuff into a single
datagram is sent as a single packet and is either received or lost as a group. One
packet is not necessarily related to the next. Given two packets, there is no way to
determine which packet was sent first and which was sent second. Datagrams try to
crowd into the recipient as quickly as possible, like a crowd of people pushing their
way onto a bus. And occasionally, if the bus is crowded enough, a few packets, like
people, may not squeeze on and will be left waiting at the bus stop. 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.
3.1.1 Usage of class
The java.net package contains three classes to help you write Java programs that
use datagram to send and receive packets over the network:
DatagramSocket, DatagramPacket, and MulticastSocket .
DatagramSocket
In Java, a datagram socket is created and accessed through the DatagramSocket
class. DatagramSocket is a socket for sending and receiving datagram packets. An
application can send and receive DatagramPackets through a DatagramSocket. All
datagram sockets are bound to a local port, on which they listen for incoming data
and which they place in the header of outgoing datagrams.

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

FN513 NETWORK PROGRAMMING | 2015

Writing a client:
You don't care what the local port is, so you call a constructor that lets
the system assign an unused port (an anonymous port).
This port number is placed in any outgoing datagrams and will be used
by the server to address any response datagrams.
opening a datagram socket
DatagramSocket socket = new DatagramSocket(0);
Note: You only specify a local port to connect to. The socket does not know the
remote host or address. By specifying port 0 you ask Java to pick a random available
port for you, much as with server sockets.

Writing a server:
Clients need to know on which port the server is listening for incoming
datagrams.
When a server constructs a DatagramSocket, it must specify the local
port on which it will listen.
A UDP server follows almost the same pattern as a UDP client, except
that you usually receive before sending and dont choose an
anonymous port to bind to.
opening a datagram socket on a well-known port 13:
DatagramSocket socket = new DatagramSocket(13);

Table 3.1: DatagramSocket class

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

FN513 NETWORK PROGRAMMING | 2015

DatagramPacket
To send data, put the data in a DatagramPacket and send the packet using a
DatagramSocket. To receive data, take a DatagramPacket object from a
DatagramSocket and then inspect the contents of the packet. UDP datagram is
represented by an instance of the DatagramPacket class:
public final class DatagramPacket extends Object

This class provides methods to:


get and set the source or destination address from the IP header,
get and set the source or destination port,
get and set the data,
get and set the length of the data.

Table 3.2: DatagramPacket class

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

FN513 NETWORK PROGRAMMING | 2015

3.2 Understand MulticastSocket


Multicasting is broader than unicast, point-to-point communication but narrower
and more targeted than broadcast communication. Multicasting sends data from one
host to many different hosts, but not to everyone; the data only goes to clients that
have expressed an interest by joining a particular multicast group.
Unicast is point-to-point communication. Broadcast is communication to all
connected members.
Multicast is communication to a selected set of connected members:
broader than unicast
narrower and more targeted than broadcast
Basic multicast is connectionless and unreliable; in an unreliable multicast
system, messages are not guaranteed to be safely delivered to each participant.
Videoconferencing, by contrast, sends an audio-video feed to a select group of
people. Usenet news is posted at one site and distributed around the world to
hundreds of thousands of people. DNS router updates travel from the site,
announcing a change to many other routers. However, the sender relies on the
intermediate sites to copy and relay the message to downstream sites. The sender
does not address its message to every host that will eventually receive it. These are
examples of multicasting, although theyre implemented with additional application
layer protocols on top of TCP or UDP.
In applications or network services that make use of multicasting, we have a set of
processes that form a group called multicast group. Each process in the group can
send and receive messages. A message sent by any process in the group will be
received by each participating process in the group.
IP address range: 224.0.0.0 to 239.255.255.255 (i.e. first four bits is 1110)
These are the class D addresses. A multicast group is a set of host that shares a
multicast address. To create a multicast group, select a random address from
225.0.0.0 to 238.255.255.255. After which, create an InetAddress object for that
address and start sending its data.

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

FN513 NETWORK PROGRAMMING | 2015

Figure 3.2 : Common Permanent Multicast Addresses


Multicast Socket
A MulticastSocket is a DatagramSocket, with additional capabilities for joining and
leaving a multicast group. An object of the multicast datagram socket class can be
used for sending and receiving IP multicast packets.
Java MulticastSocket Class
InetAddress
In the datagram socket API, this class represents the IP address of the
sender or receiver. In multicasting, this class can be used to identify
a multicast group.
DatagramPacket
As with datagram sockets, an object of this class represents an actual
datagram; in multicast, a DatagramPacket object represents a packet
of data sent to all participants or received by each participant in a
multicast group.
DatagramSocket
In the datagram socket API, this class represents a socket through
which a process may send or receive data.
MulticastSocket
A MulticastSocket is a DatagramSocket, with additional capabilities for
joining and leaving a multicast group. An object of the multicast
datagram socket class can be used for sending and receiving IP
multicast packets.
The MulticastSocket class is an extension of the DatagramSocket class and provides
capabilities for joining and leaving a multicast group.The constructor of the
MulticastSocket class takes an integer argument that corresponds to the port
number to which the object of this class would be bound to. The receive( ) method
of the MulticastSocket class is a blocking-method, which when invoked on a
MulticastSocket object will block the execution of the receiver until a message
arrives to the port to which the object is bound to.

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

FN513 NETWORK PROGRAMMING | 2015

Communicating with a Multicast Group


Once a MulticastSocket has been created, it can perform four key operations:
1.
2.
3.
4.

Join a multicast group.


Send data to the members of the group.
Receive data from the group.
Leave the multicast group.

Joining a multicast group


To join a multicast group at IP address m and UDP port p, a MulticastSocket object
must be instantiated with p, then the objects joinGroup method can be invoked
specifying the address m:
// join a Multicast group at IP address 239.1.2.3 and port 3456 InetAddress inet
InetAddress.getByName("239.1.2.3");
MulticastSocket s = new MulticastSocket(3456);
s.joinGroup(inet);

Sending to a multicast group


A multicast message can be sent using syntax similar with the datagram socket API.
String msg = "This is a multicast message.";
InetAddress inet = InetAddress.getByName("239.1.2.3");
MulticastSocket s = new MulticastSocket(3456);
s.joinGroup(inet); // optional
DatagramPacket hi = new DatagramPacket(msg.getBytes( ), msg.length(),inet, 3456);
s.send(hi);

Receiving messages sent to a multicast group


A process that has joined a multicast group may receive messages sent to the group
using syntax similar to receiving data using a datagram socket API.
byte[] buf = new byte[1000];
InetAddress inet = InetAddress.getByName("239.1.2.3");
MulticastSocket s = new MulticastSocket(3456);
s.joinGroup(inet);
DatagramPacket recv = new DatagramPacket(buf, buf.length);
s.receive(recv);

Leaving a multicast group


A process may leave a multicast group by invoking the leaveGroup method of a
MulticastSocket object, specifying the multicast address of the group.
s.leaveGroup(inet);

Lab activity:
1. Create the classes using Java program
2. Create a java program to listen for UDP packets
3. Create a java program to send and receive file.
4. Create the application of multicast socket

DEPARTMENT OF INFORMATION AND COMMUNICATION TECHNOLOGY

Вам также может понравиться