7 Programming client-server communication UDP
Download
Report
Transcript 7 Programming client-server communication UDP
TASHKENT UNIVERSITY OF INFORMATION
TECHNOLOGIES
THE DEPARTMENT OF
DATA COMMUNICATION NETWORKS AND SYSTEMS
Lecture №7
Design of client-server communication
software.
UDP-based network programming
Lector: Aliyev H.U.
Introduction
•
•
•
•
•
•
•
•
In lecture 6, we looked at socket programming in .NET, and saw how we can use the
Socket class to connect to remote hosts using different protocols. In the last chapter,
we looked at the TcpClient and TcpListener classes, which provide a higher-level
implementation for connecting over TCP. The Microsoft .NET Framework also
provides a special class called UdpClient specifically for implementing the User
Datagram Protocol (UDP). In this chapter we'll look at the basics of the UDP protocol,
and then see how to use the UdpClient class.
In the previous lecture, we saw the 'three-phase handshake' that TCP uses to ensure
that data is transmitted correctly. While this does make TCP far more reliable, it also
adds a lot of overhead. UDP has none of this overhead, so it's much faster. This makes
it well suited for multi-media transmissions such as video streams, where the precise
order that packets arrive in may not be critical.
In fact, UDP is an exceptionally simple protocol-the specification (RFC 768) is only
three pages long! (This compares to 85 pages for the TCP specification, RFC 793.)
In this lesson we'll look at:
A basic introduction to UDP
The advantages and disadvantages of the UDP protocol
Implementation of the UDP protocol in .NET using the UdpClient class
Higher-level UDP-based protocols
Connectionless-oriented client-server
architecture
Overview of the UDP Protocol
• The User Datagram Protocol (UDP) is a simple, connection-less, datagramoriented protocol and provides a fast but not necessarily reliable transport
service. It supports and is often used for one-to-many communications,
using broadcast or multicast IP datagrams.
• Internet Protocol (IP) is the basic protocol of the Internet. Transmission
Control Protocol (TCP) and UDP are both transport-level protocols built on
top of the underlying IP protocol. The following figure shows how the OSI
model maps to the TCP/IP architecture and the TCP/IP protocol suite:
The differences between TCP and
UDP
Characteristics
UDP
TCP
Connection-oriented
N
Y
Use of session
N
Y
Reliability
N
Y
Acknowledgement
N
Y
Sequencing
N
Y
Flow control
N
Y
Secure
Less
More
Data checksum
Optionally
Y
Overhead
Less
More
Speed
Fast
Slower
Topology
One-to-one
One-to-many
One-to-one
Header
8 bytes
20 bytes
UDP Terminology
•
•
•
•
•
•
•
•
•
•
•
Before we examine how UDP works, there is some basic terminology that we need to be familiar with.
In the following section, we'll briefly define some of the major terms related to UDP.
Packets
In data communication, a packet is a sequence of binary digits, representing data and control signals,
which is transmitted and switched across the host. Within the packet, this information is arranged in a
specific format.
Datagrams
A datagram is a self-contained, independent packet of data, carrying sufficient data to be routed from
source to destination without further information, so no exchanges between the source and
destination computers and the transporting network are required.
MTU
MTU stands for Maximum Transmission Unit. The MTU is a characteristic of the link layer that
describes the maximum number of bytes of data that can be transferred in a single packet. In other
words, the MTU is the largest packet that a given network medium can carry. Ethernet, for example,
has a fixed MTU of 1,500 bytes. In UDP, if the size of a datagram is larger than the MTU, IP performs
fragmentation, breaking the datagram up into smaller pieces (fragments), so that each fragment is
smaller than the MTU.
Ports
UDP uses ports to map incoming data to a particular process running on a computer. UDP routes the
packet at the appropriate location using the port number specified in the UDP header of the datagram.
Ports are represented by 16-bit numbers, and therefore range from 0 to 65,535. Ports are also referred
to as the endpoints of logical connections, and are divided into three categories:
Well-Known Ports-From 0 to 1,023
Registered Ports-From 1,024 to 49,151 and Dynamic/Private Ports-49,152 to 65,535.
How UDP Works
• When a UDP-based application sends data to another
networked host, UDP adds an eight-byte header containing
the destination and source port number, along with the
total length of the data and a checksum. IP adds its own
header on top of the UDP datagram to form an IP datagram
How UDP Works
•
•
•
•
•
The checksum is used to check whether the data has properly arrived at the
destination without being corrupted. The checksum covers both the UDP header
and data. A pad byte is used if the checksum of the datagram is odd. If the
checksum transmitted is zero, the receiver detects a checksum error and the
datagram is discarded. The checksum is optional, but it is recommended always to
keep it enabled.
In the next step, the IP layer adds 20 bytes of header that include the TTL, the
source and destination IP addresses, along with other information. This is known
as IP encapsulation.
As we mentioned above, the maximum size of a packet is 65,507 bytes. If the size
of the packet exceeds the default size or the maximum size of the MTU, the IP
layer breaks it into segments. These segments are called fragments, and the
process of breaking the data into segments is known as fragmentation. The IP
header contains all the information about the fragments.
When the sender application ‘throws' a datagram onto the network, the datagram
is routed to the destination IP address specified in the IP header. While passing
through the router, the TTL (time-to-live) value in the IP header is decreased by
one.
When the datagram arrives at the correct destination and port, the IP layer checks
whether the datagram is fragmented or not from the IP header. If it is fragmented,
the datagram is reassembled using the information available in the header. Finally,
the application layer retrieves the filtered data by removing the header.
Advantages of UDP
• No connection establishment. UDP is a connection-less protocol, so the
overhead of making connections can be avoided. As UDP does not use any
handshaking signals, the delay in making connections can be avoided. This
is why DNS prefers UDP over TCP-DNS would be much slower if it ran over
TCP.
• Speed. UDP is fast compared to TCP. Because of this, many applications
prefer UDP over TCP. The features that make TCP more robust than UDP
(such as handshaking signals) also make it slower.
• Topology support. UDP supports both one-to-one and one-to-many
connections, whereas TCP supports only one-to-one communication.
• Overheads. TCP has higher overhead requirements; UDP has comparatively
low overhead requirements. TCP uses substantially more OS resources than
UDP does, and as a result UDP is widely used in environments where
servers handle many simultaneous clients.
• Header size. UDP has only eight-byte headers for every segment, whereas
TCP has 20-byte headers, making UDP less consuming of network
bandwidth.
Disadvantages of UDP
•
•
•
•
•
•
•
Compared to TCP, UDP has the following disadvantages:
Lack of handshaking signals. Before sending a segment, UDP does not use handshaking
signals between sending and receiving the transport layer. The sender therefore has no
way of knowing whether the datagram reaches the end system. As a result, UDP cannot
guarantee that the data will actually be delivered at the destination (for example, in
cases where the end system is off or the network is down).
In contrast, TCP is a connection-oriented service and provides communication between
a networked host using packets. TCP uses handshaking signals to check whether the
transportation of data was successful.
Use of sessions. To make TCP connection-oriented, sessions are maintained between
hosts. TCP uses session IDs to keep track of connections between two hosts. UDP
doesn't have any support for sessions due to its connection-less nature.
Reliability. UDP does not guarantee that only one copy of the data will be delivered to
the destination. To send large amounts of data to the end system, UDP breaks it into
small segments. UDP does not guarantee that these segments will be delivered to the
destination in the same order as they were created at the source. In contrast, TCP uses
sequence numbers along with port numbers and frequent acknowledgement packets,
which guarantee sequenced delivery of data.
Security. TCP is more secure than UDP. In many organizations, firewalls and routers do
not allow UDP packets. This is because hackers can use UDP ports, as explicit
connections aren't required.
Flow control. UDP doesn't have flow control; as a result, a poorly designed UDP
application can tie up a big chunk of network bandwidth.
When we Use UDP
• Many applications on the Internet use UDP. UDP is known as a 'best
effort service' protocol. Looking at the advantages and disadvantages
of UDP we can conclude that UDP is beneficial:
• For broadcasting or multicasting purposes where the application
wants to communicate with multiple hosts
• Where the datagram size is small and the sequence of fragments is
not important
• Where connection setup is not required
• When the application doesn't want to send important bulk data (as
UDP has no flow control)
• If retransmission of packets is not required
• Where low overhead on the operating system is required
• Where network bandwidth is crucial
UDP in .NET
•
•
•
•
•
In .NET, the UDP protocol can be implemented using:
The UdpClient class
The Socket class
The Winsock control
The Winsock unmanaged API
The UdpClient Class
• The Microsoft .NET Framework provides the UdpClient class for
implementing the UDP protocol on a network. As with the TcpClient
and TcpListener classes, this class is built upon the Socket class but
hides unnecessary members that aren't required for implementing
a UDP-based application.
• Using UdpClient is quite simple. First, create an instance of
UdpClient. Next, connect to the remote host by calling its Connect()
method. These two steps can be achieved in one line of code by
specifying the remote IP address and remote port number in the
UdpClient's constructor. We said earlier that UDP is a connectionless protocol; so, you might be wondering, why do we need to
connect? In fact, the Connect() method does not actually establish
a connection to the remote host prior to sending and receiving
data. When you send a datagram, the destination needs to be
known; the specified IP address and port number serve this
purpose.
UDP programming in .NET
UdpClient Methods and Properties
UDPClient based programming
• The basic procedure for sending data with the UDPClient class is shown in
the following diagram:
UDPClient based programming
•
•
•
•
•
The four steps for sending data using UDP are:
-Create a UdpClient instance
-Connect to the remote host (optional)
-Send the data
-Close the connection
UDPClient based programming
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Before we look at the Send() method in detail, let's take a quick look at an example that shows this
general process:
// Example: Sending Data.
private static void Send(string datagram)
{ // Remote IP address
IPAddress remoteAddress = IPAddress.Parse("127.0.0.1");
// Port we want to connect to int remotePort = 5001;
// ** STEP 1 ** Create the UdpClient instance
UdpClient sender = new UdpClient();
try
{ // ** STEP 2 ** Connect to remote host
sender.Connect(remoteAddress, remotePort);
Console.WriteLine("Sending datagram: {0}", datagram);
byte [] bytes = Encoding.ASCII.GetBytes(datagram);
// ** STEP 3 ** Send data to connected host
sender.Send(bytes, bytes.Length);
// ** STEP 4 ** Close the connection
sender.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Higher-level UDP-based Protocols in
different service application design
Application-layer protocol
Description/Use
RTF (Real-time Protocol)
Real time media
NFS (Network File System)
Remote file server
SNMP (Simple Network Management
Protocol)
Network management
RIP (Routing Information Protocol)
Routing protocol
DNS (Domain Name Service)
Hostname resolution
TFTP (Trivial File Transfer Protocol)
File transfer application
RPC (Remote Procedure Call)
Typical client-server model
LDAP (Lightweight Directory Access
Protocol)
Directory services
Higher-level UDP-based Protocols
•
•
•
•
•
•
•
•
•
•
•
•
Real-time Protocol (RTP)
RTP is an application-layer protocol designed for delivering real-time media such as audio/video over
unicast or multicast private and public IP networks. One example is Microsoft NetMeeting, which uses
RTP to send real-time information across the Internet.
The RTP-based application implements RTP by using UDP protocol and by adding some functionality. The
added functionality provides sequence numbering, payload identification, source identification, and
time-stamping.
Network File System (NFS)
Another popular application that uses UDP is Network File System, which provides transparent file access
to files and file-systems across the network. The advantage of NFS over FTP (File Transfer Protocol) is that
it provides transparent access to files. That is, NFS can access the portion of the file that is referenced by
an application or process.
NFS is built using Sun RFC, and uses the reserved UDP port number 2049 to perform file operations.
Simple Network Management Protocol (SNMP)
As its name indicates, Simple Network Management Protocol (SNMP) is a network-management protocol
widely used in networks. SNMP allows administrators to monitor and control remote hosts and gateways
on a network. The SNMP service can handle one or more requests from the host. It communicates
between a management program run by an administrator and the network management agent running
on a host. SNMP uses ports 161 and 162 for the manager and agent respectively.
Domain Name Service (DNS)
The Domain Name Service is a distributed database used by TCP/IP applications to map hostnames to IP
addresses. UDP is the preferred protocol for DNS applications, and it uses port 53 to send DNS queries to
a name server.
Trivial File Transfer Protocol (TFTP)
TFTP is an application-layer protocol useful for transferring files between remote hosts. This protocol is
intended to be used when bootstrapping diskless systems. It uses UDP port number 69 for its file transfer
activity.
Conclusion
• In this lecture, we discussed the basics of the UDP
protocol and saw how it compares with the TCP
protocol. We covered how to implement the UDP
protocol in .NET using the UdpClient class.
• We looked at the members of the UdpClient class,
and saw how to use them within our
programs.Lastly, we discussed some higher-level
UDP-based
protocols,
which
provide
a
demonstration of the sort of tasks that UDP is
ideally suited to perform.
Q&A?