Академический Документы
Профессиональный Документы
Культура Документы
COMPUTER NETWORKS
A Course Material on
COMPUTER NETWORKS
By
Mr. K.TAMILVANAN
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SASURIE COLLEGE OF ENGINEERING
VIJAYAMANGALAM 638 056
CS 2363
COMPUTER NETWORKS
QUALITY CERTIFICATE
: CS 2363
Subject
: COMPUTER NETWORKS
Class
: II Year EEE
Being prepared by me and it meets the knowledge requirement of the university curriculum.
This is to certify that the course material being prepared by Mr. K. Tamilvanan is of adequate quality. He
has referred more than five books among them minimum one is from aboard author.
Signature of HD
Name: Mr. S. Sriram
SEAL
CS 2363
COMPUTER NETWORKS
S.NO
CONTENTS
PAGE NO
1. 2
1.3
1.4
Introduction to networks
10
Network architecture
12
1.2.1 Protocols
12
1.2.2 Interfaces
12
OSI architecture
13
13
14
1.3.3 Socket
14
Network performance
15
1.5
16
1.6
Encoding
17
1.7
Framing
18
1.8
Error detection
18
1.9
Transmission
18
1.10
Ethernet
19
1.11
Rings
19
1.12
Switched networks
1.13
Wireless networks
1.14
Bridges
CS 2363
COMPUTER NETWORKS
2.1
Internetworking
21
2.2
Ip
22
22
22
23
Arp
24
27
2.5
27
2.6
28
2.7
Routing
29
2.8
Routing algorithms
30
30
32
33
Addressing
34
34
36
2.10
CIDR
37
2.11
Subnetting
37
2.3
2.4
2.9
Transport layer
40
3.2
41
3.3
42
42
CS 2363
COMPUTER NETWORKS
3.3.2 Tcp seq. #s and acks
42
3.4
Congestion control
43
3.5
Flow control
44
3.6
Queuing disciplines
44
3.7
49
50
58
unit IV - Transport layer
4.1
4.2
Data compression
59
4.1.1
60
Introduction to jpeg
64
4.2.1
65
Jpeg compression
Introduction to mpeg
66
67
69
4.4
Introduction to mp3
69
4.5
Cryptography
70
72
72
73
81
4.6
Symmetric key
81
4.7
Public-key
82
4.8
Authentication
84
4.9
Key distribution
86
4.3
CS 2363
COMPUTER NETWORKS
4.9.1 Key distribution mechanisms.
86
4.10
Key agreement
88
4.11
PGP
88
4.12
SSH
90
4.13
Transport security
91
4.14
IP security
92
4.15
Wireless security
94
4.16
Firewalls
96
107
5.2
E- mail
107
109
110
110
111
112
5.4
SNMP
113
5.5
FTP
113
5.6
Web services
116
5.7
Multimedia applications
117
5.8
Overlay network
119
125
II
Glossary
125
5.3
CS 2363
COMPUTER NETWORKS
III
134
IV
135
140
VI
144
VII
150
VIII
154
UNIT I
DATA COMMUNICATION
Introduction to networks network architecture network performance Direct link networks
encoding framing error detection transmission Ethernet Rings FDDI - Wireless
networks Switched networks bridges
1.1 INTRODUCTION TO NETWORKS
1.1.1Network Definition
A network can be defined as two or more computers connected together in such a way
that they can share resources.
The purpose of a network is to share resources.
A resource may be:
A file
A folder
A printer
A disk drive
Or just about anything else that exists on a computer.
CS 2363
COMPUTER NETWORKS
1.2.1 Protocols
Protocol defines the interfaces between the layers in the same system and with the layers
of peer system
Building blocks of a network architecture
Each protocol object has two different interfaces
service interface: operations on this protocol
peer-to-peer interface: messages exchanged with peer
CS 2363
COMPUTER NETWORKS
Description of Layers
Physical Layer
Handles the transmission of raw bits over a communication link
Data Link Layer
Collects a stream of bits into a larger aggregate called a frame
Network adaptor along with device driver in OS implement the protocol in this
layer
Frames are actually delivered to hosts
Network Layer
Handles routing among nodes within a packet-switched network
Unit of data exchanged between nodes in this layer is called a packet
The lower three layers are implemented on all network nodes
Transport Layer
Implements a process-to-process channel
Unit of data exchanges in this layer is called a message
Session Layer
Provides a name space that is used to tie together the potentially different
transport streams that are part of a single application
Presentation Layer
Concerned about the format of data exchanged between peers
CS 2363
COMPUTER NETWORKS
Application Layer
Standardize common type of exchanges
The transport layer and the higher layers typically run only on end-hosts and not on the
intermediate switches and routers
1.3.1 Internet Architecture
Defined by IETF
Three main features
Does not imply strict layering. The application is free to bypass the defined
transport layers and to directly use IP or other underlying networks
An hour-glass shape wide at the top, narrow in the middle and wide at the
bottom. IP serves as the focal point for the architecture
In order for a new protocol to be officially included in the architecture, there
needs to be both a protocol specification and at least one (and preferably two)
representative implementations of the specification
1.3.2 Application Programming Interface
Interface exported by the network
Since most network protocols are implemented (those in the high protocol stack) in
software and nearly all computer systems implement their network protocols as part of
the operating system, when we refer to the interface exported by the network, we are
generally referring to the interface that the OS provides to its networking subsystem
The interface is called the network Application Programming Interface (API)
Interface exported by the network
Since most network protocols are implemented (those in the high protocol stack) in
software and nearly all computer systems implement their network protocols as part of
the operating system, when we refer to the interface exported by the network, we are
generally referring to the interface that the OS provides to its networking subsystem
The interface is called the network Application Programming Interface (API)
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Bandwidth
Width of the frequency band
Number of bits per second that can be transmitted over a communication link
1 Mbps: 1 x 106 bits/second = 1x220 bits/sec
1 x 10-6 seconds to transmit each bit or imagine that a timeline, now each bit occupies 1
micro second space.
On a 2 Mbps link the width is 0.5 micro second.
Smaller the width more will be transmission per unit time.
Bits
transmitted at a particular bandwidth can be regarded as having some width:
(a) bits transmitted at 1Mbps (each bit 1 s wide);
(b) bits transmitted at 2Mbps (each bit 0.5 s wide).
Latency = Propagation + transmit + queue
Propagation = distance/speed of light
Transmit = size/bandwidth
One bit transmission => propagation is important
Large bytes transmission => bandwidth is important
Delay X Bandwidth
We think the channel between a pair of processes as a hollow pipe
Latency (delay) length of the pipe and bandwidth the width of the pipe
Delay of 50 ms and bandwidth of 45 Mbps
50 x 10-3 seconds x 45 x 106 bits/second
2.25 x 106 bits = 280 KB data.
Relative importance of bandwidth and latency depends on application
For large file transfer, bandwidth is critical
For small messages (HTTP, NFS, etc.), latency is critical
Variance in latency (jitter) can also affect some applications (e.g., audio/video
conferencing)
How many bits the sender must transmit before the first bit arrives at the receiver
if the sender keeps the pipe full
Takes another one-way latency to receive a response from the receiver
CS 2363
COMPUTER NETWORKS
If the sender does not fill the pipesend a whole delay bandwidth products
worth of data before it stops to wait for a signalthe sender will not fully utilize
the network
Infinite bandwidth
RTT dominates
Throughput = TransferSize / TransferTime
TransferTime = RTT + 1/Bandwidth x TransferSize
Its all relative
1-MB file to 1-Gbps link looks like a 1-KB packet to 1-Mbps link
1.5 DIRECT LINK NETWORKS
Gives the upper bound to the capacity of a link in terms of bits per second (bps) as a
function of signal-to-noise ratio of the link measured in decibels (dB).
C = Blog2(1+S/N)
Where B = 3300 300 = 3000Hz, S is the signal power, N the average noise.
The signal to noise ratio (S/N) is measured in decibels is related to dB = 10 x
log10(S/N). If there is 30dB of noise then S/N = 1000.
Now C = 3000 x log2(1001) = 30kbps.
All practical links rely on some sort of electromagnetic radiation propagating through a
medium or, in some cases, through free space
One way to characterize links, then, is by the medium they use
Typically copper wire in some form (as in Digital Subscriber Line (DSL) and
coaxial cable),
Another important link characteristic is the frequency
Measured in hertz, with which the electromagnetic waves oscillate
Distance between the adjacent pair of maxima or minima of a wave measured in
meters is called wavelength
Speed of light divided by frequency gives the wavelength.
Frequency on a copper cable range from 300Hz to 3300Hz; Wavelength for
300Hz wave through copper is speed of light on a copper / frequency
2/3 x 3 x 108 /300 = 667 x 103 meters.
Placing binary data on a signal is called encoding.
Modulation involves modifying the signals in terms of their frequency, amplitude,
and phase.
Optical fiber (as in both commercial fiber-to-the home services and many longdistance links in the Internets backbone), or
Air/free space (for wireless links)
CS 2363
COMPUTER NETWORKS
1.6 ENCODING
CS 2363
COMPUTER NETWORKS
Manchester encoding
Merging the clock with signal by transmitting Ex-OR of the NRZ encoded data
and the clock
Clock is an internal signal that alternates from low to high, a low/high pair is
considered as one clock cycle
In Manchester encoding
0: low high transition
1: high low transition
Problem with Manchester encoding
Doubles the rate at which the signal transitions are made on the link
Which means the receiver has half of the time to detect each pulse of the
signal
The rate at which the signal changes is called the links baud rate
In Manchester the bit rate is half the baud rate
4B/5B encoding
Insert extra bits into bit stream so as to break up the long sequence of 0s and 1s
Every 4-bits of actual data are encoded in a 5- bit code that is transmitted to the
receiver
5-bit codes are selected in such a way that each one has no more than one leading
0(zero) and no more than two trailing 0s.
No pair of 5-bit codes results in more than three consecutive 0s
1.7 .FRAMING
We are focusing on packet-switched networks, which means that blocks of data (called
frames at this level), not bit streams, are exchanged between nodes.
It is the network adaptor that enables the nodes to exchange frames.
When node A wishes to transmit a frame to node B, it tells its adaptor to transmit a frame
from the nodes memory. This results in a sequence of bits being sent over the link.
The adaptor on node B then collects together the sequence of bits arriving on the link and
deposits the corresponding frame in Bs memory.
Recognizing exactly what set of bits constitute a framethat is, determining where the
frame begins and endsis the central challenge faced by the adaptor
Byte-oriented Protocols
To view each frame as a collection of bytes (characters) rather than bits
BISYNC (Binary Synchronous Communication) Protocol
Developed by IBM (late 1960)
DDCMP (Digital Data Communication Protocol)
Used in DECNet
BISYNC sentinel approach
Frames transmitted beginning with leftmost field
Beginning of a frame is denoted by sending a special SYN (synchronize)
character
Data portion of the frame is contained between special sentinel character STX
(start of text) and ETX (end of text)
SOH : Start of Header
DLE : Data Link Escape
CS 2363
COMPUTER NETWORKS
Recent PPP which is commonly run over Internet links uses sentinel approach
Special start of text character denoted as Flag
01111110
Address, control : default numbers
Protocol for demux : IP / IPX
Payload : negotiated (1500 bytes)
Checksum : for error detection
Byte-counting approach
DDCMP
count : how many bytes are contained in the frame body
If count is corrupted
Framing error
Bit-oriented Protocol
HDLC : High Level Data Link Control
Beginning and Ending Sequences
01111110
HDLC Protocol
On the sending side, any time five consecutive 1s have been transmitted from the
body of the message (i.e. excluding when the sender is trying to send the
distinguished 01111110 sequence)
The sender inserts 0 before transmitting the next bit
HDLC Protocol
On the receiving side
5 consecutive 1s
Next bit 0 : Stuffed, so discard it
1 : Either End of the frame marker
Or Error has been introduced in the bitstream
Look at the next bit
If 0 ( 01111110 ) End of the frame marker
If 1 ( 01111111 ) Error, discard the whole frame
The receiver needs to wait for next
01111110 before it can start
receiving again
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
More recently 802.3 has been extended to include a 100-Mbps version called Fast
Ethernet and a 1000-Mbps version called Gigabit Ethernet.
An Ethernet segment is implemented on a coaxial cable of up to 500 m.
This cable is similar to the type used for cable TV except that it typically has an
impedance of 50 ohms instead of cable TVs 75 ohms.
Hosts connect to an Ethernet segment by tapping into it.
A transceiver (a small device directly attached to the tap) detects when the line is idle and
drives signal when the host is transmitting.
The transceiver also receives incoming signal.
The transceiver is connected to an Ethernet adaptor which is plugged into the host.
The protocol is implemented on the adaptor.
Multiple Ethernet segments can be joined together by repeaters.
A repeater is a device that forwards digital signals.
No more than four repeaters may be positioned between any pair of hosts.
An Ethernet has a total reach of only 2500 m.
Any signal placed on the Ethernet by a host is broadcast over the entire network
Signal is propagated in both directions.
Repeaters forward the signal on all outgoing segments.
Terminators attached to the end of each segment absorb the signal.
Ethernet uses Manchester encoding scheme.
New Technologies in Ethernet
Instead of using coax cable, an Ethernet can be constructed from a thinner cable
known as 10Base2 (the original was 10Base5)
10 means the network operates at 10 Mbps
Base means the cable is used in a baseband system
2 means that a given segment can be no longer than 200 m
New Technologies in Ethernet
Another cable technology is 10BaseT
T stands for twisted pair
Limited to 100 m in length
With 10BaseT, the common configuration is to have several point to point
segments coming out of a multiway repeater, called Hub
Access Protocol for Ethernet
The algorithm is commonly called Ethernets Media Access Control (MAC).
It is implemented in Hardware on the network adaptor.
Frame format
Preamble (64bit): allows the receiver to synchronize with the signal (sequence of
alternating 0s and 1s).
Host and Destination Address (48bit each).
Packet type (16bit): acts as demux key to identify the higher level protocol.
Data (up to 1500 bytes)
Minimally a frame must contain at least 46 bytes of data.
Frame must be long enough to detect collision.
CRC (32bit)
Ethernet Addresses
CS 2363
COMPUTER NETWORKS
Each host on an Ethernet (in fact, every Ethernet host in the world) has a unique Ethernet
Address.
The address belongs to the adaptor, not the host.
It is usually burnt into ROM.
Ethernet addresses are typically printed in a human readable format
As a sequence of six numbers separated by colons.
Each number corresponds to 1 byte of the 6 byte address and is given by a pair of
hexadecimal digits, one for each of the 4-bit nibbles in the byte
Leading 0s are dropped.
For example, 8:0:2b:e4:b1:2 is
00001000 00000000 00101011 11100100 10110001 00000010
To ensure that every adaptor gets a unique address, each manufacturer of Ethernet
devices is allocated a different prefix that must be prepended to the address on every
adaptor they build
AMD has been assigned the 24bit prefix 8:0:20
1.11.RINGS
A ring toplogy network developed in the late 1960s. Supported mainly by IBM.
Pushed into the background by Ethernet in the 1990s.
a LAN protocol which resides at the data link layer (DLL) of the OSI
model
Shielded Twisted Pair with unique hermaphroditic connectors (IBM Type 1)
or
Symmetric pair.
Speed:
4 Mbps (1985)
16 Mpbs (1989, IBM
Ring operation
When nobody is transmitting a token circles.
When a station needs to transmit data, it converts the token into a data frame.
When the sender receives its own data frame, it converts the frame back into a token.
If an error occurs and no token frame, or more than one, is present, a special station
(Active Monitor) detects the problem and removes and/or reinserts tokens as necessary.
The Abort frame: used to abort transmission by the sending station
FDDI
CS 2363
COMPUTER NETWORKS
according to the osi-rm, fddi specifies layer 1 (physical layer) and part of layer 2 (data
link control layer)
the physical layer handles the transmission of raw bits over a communications link
the data link control (dlc) layer is responsible for maintaining the integrity of information
exchanged between two points
high bandwidth (10 times more than ethernet)
larger distances between fddi nodes because of very low attenuation ( 0.3 db/km) in
fibers
improved signal-to-noise ratio because of no interference from external radio frequencies
and electromagnetic noise
ber typical of fiber-optic systems (10^-11) is substantially better than that in copper (10^5) and microwave systems (10^-7)
very difficult to tap signals form a fiber cable
high cost of optical components required for transmission/reception of signals (especially
for single mode fiber networks)
more complex to implement than existing low speed lan technologies such as ieee 802.3
and ieee 802.5
office automation at the desktop
backbones for factory automation
backend data center applications
campus lan interconnection
intercampus backbones or metropolitan area networks (mans)
interconnection of private branch exchanges (pbxs)
workgroup and departmental lans
integrated transport for multimedia applications
1.12 SWITCHED NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Learning Bridges
Observe that there is no need to forward all the frames that a bridge receives
Broadcast and Multicast
Forward all broadcast/multicast frames
Current practice
Learn when no group members downstream
Accomplished by having each member of group G send a frame to bridge
multicast address with G in source field
Limitation of Bridges
Do not scale
Spanning tree algorithm does not scale
Broadcast does not scale,Do not accommodate heterogeneity
UNIT II DATA LINKLAYER
Internetworking IP - ARP Reverse Address Resolution Protocol Dynamic Host
Configuration Protocol Internet Control Message Protocol Routing Routing algorithms
Addressing Subnetting CIDR Inter domain routing IPv6
2.1 INTERNETWORKING
An arbitrary collection of networks interconnected to provide some sort of hosthost to packet delivery service
2.2 IP
IP stands for Internet Protocol
Key tool used today to build scalable, heterogeneous internetworks
It runs on all the nodes in a collection of networks and defines the infrastructure
that allows these nodes and networks to function as a single logical internetwork
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
2.3 ARP
Address Translation Protocol (ARP)
Map IP addresses into physical addresses
destination host
next hop router
Techniques
encode physical address in host part of IP address
table-based
ARP (Address Resolution Protocol)
table of IP to physical address bindings
broadcast request if IP address not in table
target machine responds with its physical address
table entries are discarded if not refreshed
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Reliable Flooding
store most recent LSP from each node
forward LSP to all nodes but one that sent it
generate new LSP periodically; increment SEQNO
start SEQNO at 0 when reboot
decrement TTL of each stored LSP; discard when TTL=0
2.9 ADDRESSING
2.9.1 Global Addresses
Properties
globally unique
hierarchical: network + host
4 Billion IP address, half are A type, is B type, and 1/8 is C type
Format
Dot notation
10.3.2.4
128.96.33.81
192.12.69.77
2.9.2 IP Datagram Forwarding
Strategy
every datagram contains destination's address
if directly connected to destination network, then forward to host
if not directly connected to destination network, then forward to some router
forwarding table maps network number into next hop
each host has a default router
each router maintains a forwarding table
Example (router R2)
Algorithm
if (NetworkNum of destination = NetworkNum of one of my interfaces) then
deliver packet to destination over that interface
else
if (NetworkNum of destination is in my forwarding table) then
deliver packet to NextHop router
else
deliver packet to default router
For a host with only one interface and only a default router in its forwarding table, this simplifies
to
if (NetworkNum of destination = my NetworkNum)then
deliver packet to destination directly
else
deliver packet to default router
2.10 CIDR
CS 2363
COMPUTER NETWORKS
Classless Addressing
Classless Inter-Domain Routing
A technique that addresses two scaling concerns in the Internet
The growth of backbone routing table as more and more network numbers
need to be stored in them
Potential exhaustion of the 32-bit address space
Address assignment efficiency
Arises because of the IP address structure with class A, B, and C addresses
Forces us to hand out network address space in fixed-size chunks of three
very different sizes
A network with two hosts needs a class C address
Address assignment efficiency = 2/255 = 0.78
A network with 256 hosts needs a class B address
ADDRESS ASSIGNMENT EFFICIENCY = 256/65535 =
0.39
Problem with this solution
Excessive storage requirement at the routers.
If a single AS has, say 16 class C network numbers assigned to it,
Every Internet backbone router needs 16 entries in its routing tables for that AS
This is true, even if the path to every one of these networks is the same
If we had assigned a class B address to the AS
The same routing information can be stored in one entry
Efficiency = 16 255 / 65, 536 = 6.2%
CIDR tries to balance the desire to minimize the number of routes that a router needs to
know against the need to hand out addresses efficiently.
CIDR uses aggregate routes
Uses a single entry in the forwarding table to tell the router how to reach a lot of
different networks
Breaks the rigid boundaries between address classes
Consider an AS with 16 class C network numbers.
Instead of handing out 16 addresses at random, hand out a block of contiguous class C
addresses
Suppose we assign the class C network numbers from 192.4.16 through 192.4.31
Observe that top 20 bits of all the addresses in this range are the same (11000000
00000100 0001)
We have created a 20-bit network number (which is in between class B network
number and class C number)
Requires to hand out blocks of class C addresses that share a common prefix
CS 2363
COMPUTER NETWORKS
2.11 SUBNETTING
Add another level to address/routing hierarchy: subnet
Subnet masks define variable partition of host part of class A and B addresses
Subnets visible only within site
Forwarding Algorithm
D = destination IP address
for each entry < SubnetNum, SubnetMask, NextHop>
D1 = SubnetMask & D
if D1 = SubnetNum
if NextHop is an interface
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
socket
door
application
writes data
application
reads data
TCP
send buffer
TCP
receive buffer
socket
door
segment
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Drop-tail
Arriving packets get dropped when queue is full regardless of flow or importance
Lock-out problem
Allows a few flows to monopolize the queue space
Send more, get more No implicit policing
Full queues
TCP detects congestion from loss
Forces network to have long standing queues in steady-state
Queueing delays bad for time sensitive traffic
Synchronization: end hosts react to same events
Full queue empty Full empty
Poor support for bursty traffic
Maintain running average of queue length
If avg < minth do nothing
Low queuing, send packets through
If avg > maxth, drop packet
Protection from misbehaving sources
Else mark packet in a manner proportional to queue length
Notify sources of incipient congestion
CongWin
Roughly,
CongWin is dynamic, function of perceived network congestion
loss event = timeout or 3 duplicate acks
TCP sender reduces rate (CongWin) after loss event
three mechanisms:
o AIMD
o slow start
o conservative after timeout events
multiplicative decrease: cut CongWin in half after loss event
Priority queueing can solve some problems
Starvation
Determining priorities is hard
Simpler techniques: Random drop
Packet arriving when queue is full causes some random packet to be dropped
Drop front
On full queue, drop packet at head of queue
Random drop and drop front solve the lock-out problem but not the full-queues problem
CS 2363
COMPUTER NETWORKS
congestion
window
24 Kbytes
16 Kbytes
8 Kbytes
time
CS 2363
COMPUTER NETWORKS
State
Commentary
ACK
receipt for
previously
unacked
data
Slow Start
(SS)
CongWin = CongWin +
MSS,
If (CongWin > Threshold)
set
state
to
Congestion
Avoidance
Resulting in a doubling
of CongWin every
RTT
ACK
receipt for
previously
unacked
data
Congestion
Avoidance
(CA)
CongWin
CongWin+MSS
(MSS/CongWin)
=
*
Additive
increase,
resulting in increase of
CongWin by 1 MSS
every RTT
Loss event
detected
by triple
duplicate
ACK
SS or CA
Threshold = CongWin/2,
CongWin = Threshold,
Set state to Congestion
Avoidance
Fast
recovery,
implementing
multiplicative
decrease.
CongWin
will not drop below 1
MSS.
Timeout
SS or CA
Threshold = CongWin/2,
CongWin = 1 MSS,
Set state to Slow Start
Duplicate
ACK
SS or CA
CongWin
and
Threshold not changed
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
In general, if you know the relative probability that each symbol will occur in the
data, then you can assign a different number of bits to each possible symbol in a
way that minimizes the number of bits it takes to encode a given block of data.
This is the essential idea of Huffman codes, one of the important early
developments in data compression.
4.1.1 Lossless Compression Techniques
Run length Encoding
Run length encoding (RLE) is a compression technique with a brute-force
simplicity.
The idea is to replace consecutive occurrences of a given symbol with
only one copy of the symbol, plus a count of how many times that symbol
occurshence the name run length.
For example, the string AAABBCDDDD would be encoded as
3A2B1C4D.
4.1.2Differential Pulse Code Modulation
Another simple lossless compression algorithm is Differential Pulse Code
Modulation (DPCM).
The idea here is to first output a reference symbol and then, for each
symbol in the data, to output the difference between that symbol and the
reference symbol.
For example, using symbol A as the reference symbol, the string
AAABBCDDDD would be encoded as A0001123333 since A is the same
as the reference symbol, B has a difference of 1 from the reference
symbol, and so on.
Dictionary based Methods
The final lossless compression method we consider is the dictionary-based
approach, of which the Lempel-Ziv (LZ) compression algorithm is the
best known.
The Unix compress and gzip commands use variants of the LZ algorithm.
The idea of a dictionary-based compression algorithm is to build a
dictionary (table) of variable-length strings (think of them as common
phrases) that you expect to find in the data, and then to replace each of
these strings when it appears in the data with the corresponding index to
the dictionary.
Dictionary based Methods
For example, instead of working with individual characters in text data,
you could treat each word as a string and output the index in the dictionary
for that word.
To further elaborate on this example, the word compression has the
index 4978 in one particular dictionary; it is the 4978th word in
/usr/share/dict/words.
To compress a body of text, each time the string compression appears, it
would be replaced by 4978.
CS 2363
COMPUTER NETWORKS
4.1.2
Given the increase in the use of digital imagery in recent yearsthis use was spawned
by the invention of graphical displays, not high-speed networksthe need for standard
representation formats and compression algorithms for digital imagery data has grown
more and more critical.
In response to this need, the ISO defined a digital image format known as JPEG, named
after the Joint Photographic Experts Group that designed it. (The Joint in JPEG stands
for a joint ISO/ITU effort.)
4.1.3
JPEG is the most widely used format for still images in use today.
At the heart of the definition of the format is a compression algorithm, which we describe
below.
Many techniques used in JPEG also appear in MPEG, the set of standards for video
compression and transmission created by the Moving Picture Experts Group.
Digital images are made up of pixels (hence the megapixels quoted in digital camera
advertisements).
Each pixel represents one location in the two-dimensional grid that makes up the image,
and for color images, each pixel has some numerical value representing a color.
There are lots of ways to represent colors, referred to as color spaces: the one most
people are familiar with is RGB (red, green, blue).
4.2 INTRODUCTION TO JPEG
4.2.1
JPEG Compression
DCT Phase
DCT is a transformation closely related to the fast Fourier transform (FFT). It
takes an
8 8 matrix of pixel values as input and outputs an 8 8 matrix of frequency
coefficients.
You can think of the input matrix as a 64-point signal that is defined in two
spatial dimensions (x and y); DCT breaks this signal into 64 spatial frequencies.
DCT, along with its inverse, which is performed during decompression, is defined
by the following formulas:
where pixel(x, y) is the grayscale value of the pixel at position (x, y) in the 88
block being compressed; N = 8 in this case
CS 2363
COMPUTER NETWORKS
Quantization Phase
The second phase of JPEG is where the compression becomes lossy.
DCT does not itself lose information; it just transforms the image into a form that
makes it easier to know what information to remove.
Quantization is easy to understandits simply a matter of dropping the insignificant bits of the
frequency coefficients
Quantization Phase
The basic quantization equation is
QuantizedValue(i, j) = IntegerRound(DCT(i, j)/Quantum(i, j))
Where
Decompression is then simply defined as
DCT(i, j) = QuantizedValue(i, j) Quantum(i, j)
Encoding Phase
The final phase of JPEG encodes the quantized frequency coefficients in a
compact form.
This results in additional compression, but this compression is lossless.
Starting with the DC coefficient in position (0,0), the coefficients are
processed in the zigzag sequence.
Along this zigzag, a form of run length encoding is usedRLE is applied
to only the 0 coefficients, which is significant because many of the later
coefficients are 0.
The individual coefficient values are then encoded using a Huffman code.
4.3 INTRODUCTION TO MPEG
4.3.1 Video Compression (MPEG)
We now turn our attention to the MPEG format, named after the Moving Picture Experts
Group that defined it.
To a first approximation, a moving picture (i.e., video) is simply a succession of still
imagesalso called frames or picturesdisplayed at some video rate.
Each of these frames can be compressed using the same DCT-based technique used in JPEG
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Joint stereo coding takes advantage of the fact that both channels of a stereo channel pair
contain similar information
These stereophonic irrelevancies and redundancies are exploited to reduce the total
bitrate
Joint stereo is used in cases where only low bitrates are available but stereo signals are
desired.
Encoder
A typical solution has two nested iteration loops
Distortion/Noise control loop (outer loop)
Rate control loop (inner loop)
CS 2363
Encryption key the text value required to encrypt and decrypt data
Encryption methodologies
Substitution Cipher
Plaintext characters are substituted to form ciphertext
A becomes R, B becomes G, etc.
Character rotation
Caesar rotated three to the right (A > D,
B > E, C > F, etc.)
A table or formula is used
ROT13 is a Caesar cipher
Image from Wikipedia (link Ch 5a)
Subject to frequency analysis attack
4.5.1 Transposition Cipher
Plaintext messages are transposed into ciphertext
Plaintext: ATTACK AT ONCE VIA NORTH BRIDGE
Write into columns going down
Read from columns to the right
Ciphertext:
AKCNBTAEORTTVRIAOITDCNAHG
Subject to frequency analysis attack
Monoalphabetic Cipher
One alphabetic character is
substituted or another
Subject to frequency analysis attack
COMPUTER NETWORKS
CS 2363
Running-key Cipher
Plaintext letters converted
to numeric (A=0, B=1,
etc.)
Plaintext values added
to key values giving
ciphertext
Modulo arithmetic is used
to keep results in range 026
Add 26 if results < 0; subtract 26 if results > 26
One-time Pad
Works like running key
cipher, except that key is
length of plaintext, and is
used only once
Highly resistant to
cryptanalysis
4.5.3 Types of
ecryption
Block cipher
Encrypts blocks of data, often 128 bits
Stream cipher
Operates on a continuous stream of data
Block Ciphers
Encrypt and decrypt a block of data at a time
Typically 128 bits
Typical uses for block ciphers
Files, e-mail messages, text communications, web
Well known encryption algorithms
DES, 3DES, AES, CAST, Twofish, Blowfish, Serpent
Block Cipher Modes of Operation
Electronic Code Book (ECB)
Cipher-block chaining (CBC)
Cipher feedback (CFB)
Output feedback (OFB)
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Counter (CTR)
Initialization Vector (IV)
Starting block of information needed to encrypt the first block of data
IV must be random and should not be re-used
WEP wireless encryption is weak because it re-uses the IV, in addition to making other
errors
CS 2363
COMPUTER NETWORKS
Only recipient can read it, using his or her private key
Provides confidentiality
Sign message
Hash message, encrypt hash with your private key
Anyone can verify the signature using your public key
Provides integrity and non-repudiation (sender cannot deny authorship)
Sign and encrypt
Both of the above
4.6 SYMMETRIC KEY
4.7 PUBLIC-KEY
Rapidly increasing needs for flexible and secure transmission of information require to
use new cryptographic methods.
The main disadvantage of the classical cryptography is the need to send a (long) key
through a super secure channel before sending the message itself.
CS 2363
COMPUTER NETWORKS
In secret-key (symetric key) cryptography both sender and receiver share the same secret
key.
In public-key ryptography there are two different keys:
a public encryption key
and
a secret decryption key (at the receiver side).
Basic idea: If it is infeasible from the knowledge of an encryption algorithm ek to
construct the corresponding description algorithm dk, then ek can be made public.
Toy example: (Telephone directory encryption)
Start: Each user U makes public a unique telephone directory tdU to encrypt messages for
U and U is the only user to have an inverse telephone directory itdU.
Encryption: Each letter X of a plaintext w is replaced, using the telephone directory tdU
of the intended receiver U, by the telephone number of a person whose name starts with letter X.
Decryption: easy for Uk, with an inverse telephone directory, infeasible for others.
Analogy:
Secret-key cryptography 1. Put the message into a box, lock it with a padlock and send
the box. 2. Send the key by a secure channel.
Public-key cryptography Open padlocks, for each user different one, are freely available.
Only legitimate user has key from his padlocks. Transmission: Put the message into the box of
the intended receiver, close the padlock
Main problem of the secret-key cryptography: a need to make a secure distribution
(establishment) of secret keys ahead of transmissions.
Diffie+Hellman solved this problem in 1976 by designing a protocol for secure key
establishment (distribution) over public channels.
Protocol: If two parties, Alice and Bob, want to create a common secret key, then
they first agree, somehow, on a large prime p and a primitive root q (mod p) and then
they perform, through a public channel, the following activities.
Alice chooses, randomly, a large 1 x < p -1 and computes
X = q x mod p.
Bob also chooses, again randomly, a large 1 y < p -1 and computes
Y = q y mod p.
Alice and Bob exchange X and Y, through a public channel, but keep x, y secret.
Alice computes Y x mod p and Bob computes X y mod p and then each of them has the
key
K = q xy mod p.
The following attack by a man-in-the-middle is possible against the Diffie-Hellman key
establishment protocol.
. Eve chooses an exponent z.
Eve sends q z to both Alice and Bob. (After that Alice believes she has received q x and
Bob believes he has received q y.)
Eve intercepts q x and q y.
When Alice sends a message to Bob, encrypted with KA, Eve intercepts it, decrypts it,
then encrypts it with KB and sends it to Bob
without any need forsecret key distribution
(Shamir's no-key algorithm)
Basic assumption: Each user X has its own
CS 2363
COMPUTER NETWORKS
4.8 AUTHENTICATION
fundamental security building block
basis of access control & user accountability
is the process of verifying an identity claimed by or for a system entity
has two steps:
identification - specify identifier
verification - bind entity (person) and identifier
distinct from message authentication
four means of authenticating user's identity
based one something the individual
knows - e.g. password, PIN
possesses - e.g. key, token, smartcard
is (static biometrics) - e.g. fingerprint, retina
does (dynamic biometrics) - e.g. voice, sign
can use alone or combined
all can provide user authentication
all have issues
Authentication Protocols
used to convince parties of each others identity and to exchange session keys
may be one-way or mutual
key issues are
confidentiality to protect session keys
timeliness to prevent replay attacks
where a valid signed message is copied and later resent
simple replay
repetition that can be logged
repetition that cannot be detected
backward replay without modification
countermeasures include
use of sequence numbers (generally impractical)
timestamps (needs synchronized clocks)
challenge/response (using unique nonce)
CS 2363
COMPUTER NETWORKS
One-Way Authentication
required when sender & receiver are not in communications at same time (eg. email)
have header in clear so can be delivered by email system
may want contents of body protected & sender authenticated
as discussed previously can use a two-level hierarchy of keys
usually with a trusted Key Distribution Center (KDC)
each party shares own master key with KDC
KDC generates session keys used for connections between parties
master keys used to distribute these to them
In designing the key distribution protocol, the authors took into consideration the
following requirements:
Security domain change: The Certifiction Authority (CA) of the receiving security
domain must be able to authenticate the agent which comes from another security
domain.
Trust establishment: The key distribution process should start with a very high trust
relationship with the Certifiction Authority (CA) .
Secure key distribution: The key distribution process should be conducted in a secure
manner (e.g., trusted path).
Efficiency: The key distribution process should not consume a lot of resources, such as
machines CPUs and network bandwidth.
Scalability: The key distribution process must be scalable enough, so that the mobile
agent can have the ability to roam widely.
Transparency: The mobile agents should not include a code which is proper to key
distribution.
This will ease programming as agents programmers will concentrate on the
programming logic rather than the key obtaining issues.
Portability: The protocol should not be platform specific and should be ported to any
mobile agent platform.
Ease of administration: The key distribution protocol should not be a burden on the
administrator. The protocol is an automated infrastructure that should require minimum
administrators intervention.
4.9.1 Key Distribution Mechanisms.
A. System Components:
A key distribution system for mobile agents includes the following components:
Agent: An agent is a software component which executes on behalf of a particular user
who is the user of the agent.
An agent can be mobile and move from one host server to another under its own
control to achieve tasks on these hosts servers.
CS 2363
COMPUTER NETWORKS
Agent Server: Each host, as part of the mobile agent platform, runs an execution
environment, the agent server.
Messaging System. A messaging system is part of an agent execution environment. It
provides facilities for agents to communicate both locally and remotely
The CA. It is a trusted third party which provides digital certificates for mobile agents,
users and agent servers.
All digital certificates are signed by the CA for further verification of their
authenticity and validity.
Keystore: Each agent server has a local database which is used to store and retrieve its
own private/public key pair and the digital certificate.
It also stores the digital certificate of the trusted CA and other agent severs, mobile
agents, and CAs with which the agent server has prior communication.
Similarly, each CA has a local keystore .
Security Domain: A security domain consists of a group of agent servers which are
under one common CA. In the security domain, the agent servers have the digital
certificate of their local CA stored in their local keystores.
When a mobile agent moves, it can move within the same security domain or changes a
security domain.
Flexibility in credentials
Modern, publically analysed/available cryptographic primitives
Freshness guarantees
PFS?
Mutual authentication
Identity hiding for supplicant/end-user
No key re-use
CS 2363
COMPUTER NETWORKS
Fast re-key
Fast handoff
Efficiency not an overarching concern:
Protocol runs only 1/2^N-1 packets, on average
DOS resistance
Credentials flexibility
Local security policy dictates types of credentials used by end-users
Legacy authentication compatibility extremely important in market
Examples:
username/password
Tokens (SecurID, etc)
X.509 certificates
Algorithms
Algorithms must provide confidentiality and integrity of the authentication and key
agreement.
Public-key encryption/signature
RSA
ECC
DSA
PFS support
D-H
Most cryptographic primitives require strong random material that is fresh.
Not a protocol issue, per se, but a design requirement nonetheless
Both sides of authentication/key agreement must be certain of identity of other party.
Symmetric RSA/DSA schemes (public-keys on both sides)
Asymmetric schemes
Legacy on end-user side
RSA/DSA on authenticator side
4.11 PGP
PGP provides a confidentiality and authentication service that can be used for file storage
and electronic mail applications.
PGP was developed be Phil Zimmermann in 1991 and since then it has grown in
popularity. There have been several updates to PGP.
A free versions of PGP is available over the Internet, but only for non-commercial use.
The latest (Jan. 2000) current version is 6.5.
Commercial versions of PGP are available from the PGP Division of Network Associates
For three years, Philip Zimmermann, was threatened with federal prosecution in the
United States for his actions. Charges were finally dropped in January 1996.
At the close of 1999, Network Associates, Inc. announced that it has been granted a full
license by the U.S. Government to export PGP world-wide, ending a decades-old ban.
PGP enables you to make your own public and secret key pairs.
PGP public keys are distributed and certified via an informal network called "the web of
trust".
CS 2363
COMPUTER NETWORKS
Most experts consider PGP very secure if used correctly. PGP is based on RSA, DSS,
Diffie-Hellman in the public encryption side, and CAST.128, IDEA, 3DES for
conventional encryption. Hash coding is done with SHA-1.
PGP has a wide range of applicability from corprorations that wish to enforce a
standardized scheme for encryptin files and messages to individuals who wish to
communicate securely with each others over the interent.
The actual operation of PGP consists of five services: authentication, confidentiality,
compression, e-mail compatibility and segmentation (Table 12.1.)
Authenticaiton
The digital signature service is illustrated in Fig 12.1a.
EC is used for conventional encryption, DC for decryption, and EP and ED
correspondingly for public key encryption and decryption.
The algorithms used are SHA-1 and RSA. Alternatively digital signatures can be
generated using DSS/SHA-1.
Normally digital signatures are attached to the files they sign, but there are exceptions
a detached signature can be used to detect a virus infection of an executable
program.
sometimes more than one party must sign the document.
a separate signature log of all messages is maintained
Confidentiality
Confidentiality service is illustrated in Fig 12.1b.
CS 2363
COMPUTER NETWORKS
Confidentiality can be use for storing files locally or transmitting them over insecure
channel.
The algorithms used are CAST-128 or alternatively IDEA or 3DES. The ciphers run in
CFB mode.
Each conventional key is used only once.
A new key is generated as a random 128-bit number for each message.
The key is encrypted with the receivers public key (RSA) and attached to the
message.
An alternative to using RSA for key encryption, ELGamal, a variant of DiffieHellman providing also encryption/decryption, can be used.
The use of conventional encryption is fast compared to encryption the whole message
with RSA.
The use of public key algorithm solves the use session key distribution problem. In email
application any kind of handshaking would not be practical.
4.12 SSH
protocol for secure network communications
designed to be simple & inexpensive
SSH1 provided secure remote logon facility
replace TELNET & other insecure schemes
also has more general client/server capability
Can be used for FTP, for example
SSH2 was documented in RFCs 4250 through 4254
SSH clients & servers are widely available (even in OSs)
CS 2363
COMPUTER NETWORKS
As a result (i) two sides now share a master key K. (ii) the server has been
authenticated to the client.
Then, encryption, MAC keys and IV are derived from the master key
End of key exchange
To signal the end of key exchange process
Encrypted and MACed using the new keys
Service Request: to initiate either user authentication or connection protocol
Authentication of client to server
First client and server agree on an authentication method
Then a sequence of exchanges to perform that method
Several authentication methods may be performed one after another
authentication methods
public-key
Client signs a message and server verifies
password
Client sends pasword which is encrypted and MACed using the keys
agreed
runs on SSH Transport Layer Protocol
assumes secure authentication connection
which is called tunnel
used for multiple logical channels
SSH communications use separate channels
either side can open with unique id number
flow controlled via sliding window mechanism
have three stages:
opening a channel, data transfer, closing a channel
CS 2363
COMPUTER NETWORKS
4.14 IP SECURITY
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
WEP uses RC4 encryption algorithm known as stream cipher to protect the
confidentiality of its data.
Stream cipher operates by expanding a short key into an infinite pseudo-random key
stream.
Sender XORs the key stream with plaintext to produce cipher text.
Receiver has the copy of the same key, and uses it to generate an identical key stream.
XORing the key stream with the cipher text yields the original message.
Passive Attacks
To decrypt the traffic based on statistical analysis (Statistical Attack)
Active Attacks
To inject new traffic from authorized mobile stations, based on known plaintext.
Active Attacks
To decrypt the traffic based on tricking the access point
Dictionary Attacks
Allow real time automated decryption of all traffic.
4.16 FIREWALLS
CS 2363
COMPUTER NETWORKS
Packet-filtering Router
Applies a set of rules to each incoming IP packet and then forwards or discards
the packet
Filter packets going in both directions
The packet filter is typically set up as a list of rules based on matches to fields in
the IP or TCP header
Two default policies (discard or forward)
Advantages:
Simplicity
Transparency to users
High speed
Disadvantages:
Difficulty of setting up packet filter rules
Lack of Authentication
CS 2363
COMPUTER NETWORKS
Resolvers
A Resolver maps a name to an address and vice versa.
Iterative Resolution
CS 2363
COMPUTER NETWORKS
Recursive Resolution
5.2 E- MAIL
What is an Email an electronic message transmitted over a network from one user to
another.
Can be as simple as a few lines of text, or include attachments such as pictures or
documents.
Email made up 75% of network traffic soon after the introduction of the internet.
The Header
Who sent the email.
To whom the mail is sent.
When the email was sent.
The email subject.
The size of the email.
The Body
Contains the message.
May also contain an attachment.
Attachments Different Architectural Models exist for constructing computer systems.
Some models include:
Peer-Peer
Pipe and Filter
Implicit Invocation
Client-Server
If not embedded within the body, attachments are sent along with the email.
How Email Works
CS 2363
COMPUTER NETWORKS
Provided Interface
Peer
Peer
Required Interface
Required Interface
Provided Interface
Peer
Required Interface
CS 2363
COMPUTER NETWORKS
Hypertext documents
Text
Links
Web
billions of documents
authored by millions of diverse people
edited by no one in particular
distributed over millions of computers, connected by variety of media
Citation,
Hyperlinking
Ramayana, Mahabharata, Talmud
branching, non-linear discourse, nested commentary,
Dictionary, encyclopedia
self-contained networks of textual nodes
joined by referential links
Memex [Vannevar Bush]
stands for memory extension
photoelectrical-mechanical storage and computing device
Aim: to create and help follow hyperlinks across documents
Hypertext
Coined by Ted Nelson
Xanadu hypertext: system with
robust two-way hyperlinks, version management, controversy management,
annotation and copyright management.
Initiated at CERN (the European Organization for Nuclear Research)
By Tim Berners-Lee
GUIs
Berners-Lee (1990)
Erwise and Viola(1992), Midas (1993)
Mosaic (1993)
a hypertext GUI for the X-window system
HTML: markup language for rendering hypertext
CS 2363
COMPUTER NETWORKS
HTTP: hypertext transport protocol for sending HTML and other data over the
Internet
CERN HTTPD: server of hypertext documents
5.3.1 Hypertext data
Semi-structured or unstructured
No schema
Large number of attributes
Purpose of crawling and indexing
quick fetching of large number of Web pages into a local repository
indexing based on keywords
Ordering responses to maximize users chances of the first few responses
satisfying his information need.
Earliest search engine: Lycos (Jan 1994)
Followed by.
Alta Vista (1995), HotBot and Inktomi, Excite
Yahoo! directory
to locate useful Web sites
Efforts for organizing knowledge into ontologies
Centralized: (Yahoo!)
Decentralized: About.COM and the Open Directory
5.3.2 Clustering and classification
Clustering
discover groups in the set of documents such that documents within a group are
more similar than documents across groups.
Subjective disagreements due to
different similarity measures
Large feature sets
Classification
For assisting human efforts in maintaining taxonomies
E.g.: IBM's Lotus Notes text processing system & Universal Database text
extenders
5.3.3 Hyperlink analysis
Take advantage of the structure of the Web graph.
Indicators of prestige of a page (E.g. citations)
HITS & PageRank
Bibliometry
bibliographic citation graph of academic papers
Topic distillation
Adapting to idioms of Web authorship and linking styles
Federations of crawling and search services
each specializing in specific topical areas.
Goal-driven Web resource discovery
language analysis does not scale to billions of documents
counter by throwing more hardware
CS 2363
COMPUTER NETWORKS
5.4 SNMP
SNMP is a tool (protocol) that allows for remote and local management of items on the
network including servers, workstations, routers, switches and other managed devices.
Comprised of agents and managers
Agent - process running on each managed node collecting information about the device it
is running on.
Manager - process running on a management workstation that requests information about
devices on the network.
Advantages of using SNMP
Standardized
universally supported
extendible
portable
allows distributed management access
lightweight protocol
SNMP is a client pull model
The management system (client) pulls data from the agent (server).
SNMP is a server push model
The agent (server) pushes out a trap message to a (client) management system
Ports & UDP
SNMP uses User Datagram Protocol (UDP) as the transport mechanism for SNMP
messages
CS 2363
COMPUTER NETWORKS
A map of the hierarchical order of all managed objects and how they are accessed
Nodes
Items in an SNMP Network are called nodes. There are different types of nodes.
Managed nodes
Management nodes
Typically a workstation running some network management & monitoring software
Nodes that are not manageable by SNMP
A node may not support SNMP, but may be manageable by SNMP through a proxy agent
running on another machine
5.5 FTP
The File Transfer Protocol (FTP) is a standard network protocol used to transfer
computer files from one host to another host over a TCP-based network, such as the Internet.
FTP is built on a client-server architecture and uses separate control and data connections
between the client and the server. FTP users may authenticate themselves using a clear-text signin protocol, normally in the form of a username and password, but can connect anonymously if
the server is configured to allow it. For secure transmission that protects the username and
password, and encrypts the content, FTP is often secured with SSL/TLS (FTPS). SSH File
Transfer Protocol (SFTP) is sometimes also used instead, but is technologically different.
The first FTP client applications were command-line applications developed before operating
systems had graphical user interfaces, and are still shipped with most Windows, Unix, and Linux
operating systems. Many FTP clients and automation utilities have since been developed for
desktops, servers, mobile devices, and hardware, and FTP has been incorporated into
productivity applications, such as Web page editors.
This stands for File Transfer Protocol. This is a popular way to transfer files from machine to
machine across a network
Ways to connect to an FTP server
Private user-only site. Allows only system users to connect via FTP and access their files.
Anonymous. Allows anyone on the network to connect to it and transfer files without
having an account.
ftp [sitename]
ftp starts the program and connects to the specified site
CS 2363
COMPUTER NETWORKS
cd [directory]
cd stands for change directory. This command will change to the spcified directory
pwd
Print working directory(tells the user which directory he/she is in)
dir [filespec]
List details about the file specification
At the command prompt type :
ftp papa.cdrom.com (this will start the ftp and connection to the site)
The system will respond with the message
>connected to sunsite.cnlab-switch.ch.
>220 warchive.cdrom.com.FTP server (Version wu-2.4.2(18)
>Thu nov 26 09:30:12 MET 2001) ready.
>Name (carchive.cdrom.com:usr):
Use dir to find the file:
Type:
dir l*
to get a listing of all files which start with l. You should see:
-rw-rw-r 1 2066 ftp-game 134868 Jun 30 2001 lan.txt
Because there is no d at the far left, you know that it is a file, not a directory.
The 134868 is the file size, it is 134,868 bytes (approx. 134 Kb). It was last modified on
the 30th of June 2001.
5.6 WEB SERVICES
Web services provide interoperability between various software applications running on
various platforms.
vendor, platform, and language agnostic
Web services leverage open standards and protocols. Protocols and data formats are text
based where possible
Easy for developers to understand what is going on.
By piggybacking on HTTP, web services can work through many common firewall
security measures without requiring changes to their filtering rules.
First the client discovers the service.
More in next lecture!
Typically, client then binds to the server.
By setting up TCP connection to the discovered address .
But binding not always needed.
Next build the SOAP request: (Marshaling)
Fill in what service is needed, and the arguments. Send it to server side.
CS 2363
COMPUTER NETWORKS
XML is the standard for encoding the data (but is very verbose and results in
HUGE overheads)
SOAP router routes the request to the appropriate server(assuming more than one
available server)
Can do load balancing here.
Server unpacks the request, (Demarshaling) handles it, computes result.
Result sent back in the reverse direction: from the server to the SOAP router back to the
client.
Marshalling Issues
Data exchanged between client and server needs to be in a platform independent format.
Endianness differ between machines.
Data alignment issue (16/32/64 bits)
Multiple floating point representations.
Pointers
(Have to support legacy systems too)
Discovery
This is the problem of finding the right service
In our example, we saw one way to do it with a URL
Web Services community favors what they call a URN: Uniform Resource Name
But the more general approach is to use an intermediary: a discovery service
Discovery and naming
Many settings, like the big data centers run by large corporations, have rather
standard structure. Can we automate discovery?
How to debug if applications might sometimes bind to the wrong service?
Delegation and migration are very tricky
Should a system automatically launch services on demand?
Client has opinions
I want current map data for Disneyland showing line-lengths for the rides right
now
Service has opinions
Amazon.com would like requests from Ithaca to go to the NJ-3 datacenter, and if
possible, to the same server instance within each clustered service
CS 2363
COMPUTER NETWORKS
There are a number of fields where multimedia could be of use. Examples are: Business
Education
Entertainment
Home
Public Places
Business
Use and Applications
Sales / Marketing Presentation
Trade show production
Staff Training Application
Company Kiosk
Education
Use and Applications
Courseware / Simulations
E-Learning / Distance Learning
Information Searching
Entertainment
Use and Applications
Games (Leisure / Educational)
Movies
Video on Demand
Online
Home
Use and Applications
Television
Satellite TV
SMS services (chats, voting, reality TV
Public Places
Use and Applications
Information Kiosk
Smart Cards, Security
Briefing Products
Reference Products
Database Products
Education and Training Products
Kiosk
Entertainment and Games
Small, straightforward, linear products used to present information quickly and concisely.
Characteristic of briefing product:
Short Development Cycle
Limited Number of Presentations
Usage of text to present information with limited use of graphic, audio and video.
Have few navigational controls. (mouse click and button press to move from one
page to another)
CS 2363
COMPUTER NETWORKS
Content and the format are suitable for the audience and fulfill the purpose of the
presentation.
Good briefing presentation depends on:
The understanding of the presented subject.
Seamless integration of content.
Consistent layout
Example:
Corporate Presentation
Sales Presentation
Educational Lectures
Basic classes of reference product:
Generalized Content (dictionary/encyclopedia)
Broad treatment of content at a limited depth
Detailed Content
Focus on specific area and provide extensive information
5.8 OVERLAY NETWORK
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
SOLUTION :
CS 2363
COMPUTER NETWORKS
(a) Propagation delay on the link is (55 109)/(3 108) = 184 seconds. Thus the RTT is 368
seconds.
(b) The delay bandwidth product for the link is = 184 128 103 = 2.81 MB.
(c) After a picture is taken it must be transmitted on the link, and be completely propagated
before Mission Control can interpret it. Transmit delay
for 5 MB of data is 41,943,040 bits/128103 = 328 seconds. Thus, the total time required is
transmit delay + propagation delay = 328 + 184 = 512 seconds.
4.Calculate the latency (from first bit sent to last bit received) for:
(a) A 1-Gbps Ethernet with a single store-and-forward switch in the path, and a packet size of
5,000 bits. Assume that each link introduces a propagation delay of 10 s and that the switch
begins retransmitting immediately after it has finished receiving the packet
(b) Same as (a) but with three switches.
(c) Same as (b) but assume the switch implements cut-through switching: it is able to begin
retransmitting the packet after the first 128 bits have been received.
SOLUTION :
(a) For each link, it takes 1 Gbps/5 kb = 5 s to transmit the packet on the link, after which it
takes an additional 10 s for the last bit to propagate across the link. Thus, for a LAN with only
one switch that starts forwarding only after receiving the whole packet, the total transfer delay is
two transmit delays + two propagation delays = 30 s.
(b) For three switched and thus four links, the total delay is four transmit delays + four
propagation delays = 60 s.
(c) For cut-through, a switch need only decode the first 128 bits before beginning to forward.
This takes 128 ns. This delay replaces the switch transmit delays in the previous answer for a
total delay of one Transmit delay + three cut-through decoding delays + four propagation delays
=45.384 s.
5. For the following, as in the previous problem, assume that no data compression is done.
Calculate the bandwidth necessary for transmitting in real time:
(a) HDTV high-definition video at a resolution of 1,920 1,080, 24 bits/pixel, 30 frames/sec.
(b) Plain old telephone service (POTS) voice audio of 8-bit samples at 8 KHz.
(c) GSM mobile voice audio of 260-bit samples at 50 Hz.
(d) HDCD high-definition audio of 24-bit samples at 88.2 kHz.
SOLUTION :
(a) 1,9201,0802430 = 1,492,992,000 1.5 Gbps.
(b) 88,000 = 64 Kbps.
(c) 260 50 = 13 Kbps.
(d) 2488,200 = 216,800 2.1 Mbps.
6. Show the 4B/5B encoding, and the resulting NRZI signal, for the following bit sequence:
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
10.Given the extended LAN shown in Figure 3.34, assume that bridge B1 suffers catastrophic
failure. Indicate which ports are not selected by the spanning tree algorithm after the recovery
process and a new tree has been formed.
SOLUTION :
The following list shows the mapping between LANs and their designated bridges.
B1 dead[B7]
B2 A,B,D
B3 E,F,G,H
B4 I
B5 idle
B6 J
B7 C
11.Suppose we have the forwarding tables shown in Table 4.13 for nodes A and F, in a network
where all links have cost 1. Give a diagram of the smallest network consistent with these tables.
SOLUTION :
The following is an example network topology.
CS 2363
COMPUTER NETWORKS
GLOSSARY
ASN
Abstract Syntax Notation One. In conjunction with BER, a presentation-formatting standard
devised by the ISO as part of the OSI architecture.
AT M :
Asynchronous transfer mode. A connection-oriented network technology that usessmall, fixedsize packets (calledcells) to carry data.
ATMARP:
Address Resolution Protocol as enhanced for ATM networks.
ATM Forum:
A key ATM standards-setting body.
Authentication:
Security protocol by which two suspicious parties prove to each other that they are who they
claim to be.
Autonomous system (as):
A group of networks and routers, subject to a common au-thority and using the same intradomain
routing protocol.
Bandwidth:
A measure of the capacity of a link or connection, usually given in units of bits per second.
Bellman-Ford:
A name for the distance-vector routing algorithm, from the names of the inventors.
BER:
Basic encoding rules. Rules for encoding data types defined by ASN.1.best-effort delivery:
The service model of the current Internet architecture. Delivery of a message is attempted but is
not guaranteed.
BGP:
Border Gateway Protocol. An interdomain routing protocol by which autonomous systems
exchange reachability information. The most recent version is BGP-4.
BISYNC:
Binary Synchronous Communication. A byte-oriented link-level protocol de-veloped in the late
1960s by IBM.
Bit stuffing:
A technique used to distinguish control sequences and data on the bit level.
Used by the HDLC protocol.
Block:
An OS term used to describe a situation in which a process suspends executionwhile awaiting
some event, such as a change in the state of a semaphore
.Bluetooth:
A short-range wireless standard used to connect computers, mobile phones,and peripheral
devices, among other things.
Bridge:
A device that forwards link-level frames from one physical network to another,sometimes called
a LAN switch. Contrast with repeater and router
CS 2363
COMPUTER NETWORKS
Broadcast:
A method of delivering a packet to every host on a particular network or in-ternet. May be
implemented in hardware (e.g., Ethernet) or software (e.g., IP broadcast).
CA:
Certification authority (also known as certificate authority). An entity that signssecurity
certificates, thereby promising that the public key contained in the certificate belongs to the
entity named in the certificate.
CBC:
Cipher block chaining. A cryptographic mode in which each plaintext block is XORed with the
previous block of ciphertext before encryption.
CBR:
Constant bit rate. A class of service in ATM that guarantees transmission of dataat a constant bit
rate, thus emulating a dedicated transmission link. Contrast with ABR,UBR,and VBR.
CCITT:
The now defunct Comit Consultif International de Telegraphique et Telephonique a unit of the
International Telecommunications Union (ITU) of the United Nations.Now replaced by ITU-T.
CDMA:
Code Division Multiple Access, a form of multiplexing used in wireless networks.
CDN:
Content distribution network. A collection of surrogate web servers, distributed across the
Internet, that respond to web HTTP requests in place of the server. The goal of widely
distributing the surrogate servers is to have a surrogate close to the client, making
it possible to respond to requests more quickly.
Cell:
A 53-byte ATM packet, capable of carrying up to 48 bytes of data.
Certificate:
A document digitally signed by one entity that contains the name and public key of another
entity. Used to distribute public keys.
Channel:
A generic communication term used in this book to denote a logical process-to-process
connection.
Checksum:
Typically a ones complement sum over some or all of the bytes of a packet,computed and
appended to the packet by the sender. The receiver recomputes the check-sum and compares it to
the one carried in the message. Checksums are used to detect errors in a packet and may also be
used to verify that the packet has been delivered to the correct host. The term checksum is also
sometimes (imprecisely) used to refer generically to error-detecting codes. chipping code:
Random sequence of bits that is XORed with the data stream to imple- ment the direct sequence
technique of spread spectrum.
CIDR:
Classless interdomain routing. A method of aggregating routes that treats a block of contiguous
Class C IP addresses as a single network.
Circuit switching:
A general strategy for switching data through a network. It involves establishing a dedicated path
(circuit) between the source and destination. Contrast with packet switching .
CS 2363
COMPUTER NETWORKS
Client:
The requester of a service in a distributed system.
CLNP:
Connectionless Network Protocol. The ISO counterpart to the Internets IP.
Clock recovery:
The process of deriving a valid clock from a serially transmitted digital signal.
Concurrent logical channels:
Multiplexing several stop-and-wait logical channels onto a single point-to-point link. No delivery
order is enforced. This mechanism was used by the IMP-IMP protocol of the ARPANET.
Congestion:
A state resulting from too many packets contending for limited resources (e.g., link bandwidth
and buffer space on routers or switches), which may force the router (switch) to discard packets.
Congestion control:
Any network resource management strategy that has, as its goal, the alleviation or avoidance of
congestion. A congestion-control mechanism may be imple- mented on the routers (switches)
inside the network, by the hosts at the edges of the network, or by a combination of both.
Connection:
In general, a channel that must be established prior to use (e.g., by the trans- mission of some
setup information). For example, TCP provides a connection abstrac- tion that offers reliable,
ordered delivery of a byte stream. Connection-oriented networks, such as ATM, are often said to
provide a virtual circuit abstraction.
Connectionless protocol:
A protocol in which data may be sent without any advance
setup. IP is an example of such a protocol.
Context switch:
An operation in which an operating system suspends the execution of one process and begins the
execution of another. A context switch involves saving the state of the former process (e.g., the
contents of all registers) and loading the state of the latter process.
Controlled load:
One of the service classes available in the Internets Integrated Services architecture.
Domain:
Can refer either to a context in the hierarchical DNS namespace (e.g., the edu domain) or to a
region of the Internet that is treated as a single entity for the purpose of hierarchical routing. The
latter is equivalent to autonomous system
.DoS:
Denial of service. A situation in which an attacking node floods a target node with so much work
(so many packets) that it effectively keeps legitimate users from accessing the node, hence, they
are denied service.
DS3:
A 44.7-Mbps transmission link service offered by the phone company. Also called T3.
DSL:
Digital subscriber line. A family of standards for transmitting data over twisted pair telephone
lines at multimegabit-per-second speeds.
Duplicate ACK:
A retransmission of a TCP acknowledgment. The duplicate ACK does not acknowledge any new
data. The receipt of multiple duplicate ACKs triggers the TCP fast retransmit mechanism.
CS 2363
COMPUTER NETWORKS
DVMRP:
Distance Vector Multicast Routing Protocol. Multicast routing protocol origi-nally used in the
MBone.
DWDM:
Dense wavelength division multiplexing. Multiplexing multiple light waves (colors) onto a
single physical fiber. The technique is dense in the sense that a large number of optical
wavelengths can be supported.
ECN:
Explicit congestion notification. A technique by which routers inform end hosts about congestion
by setting a flag in packets they are forwarding. Used in conjunction with active queue
management algorithms like RED.
EF:
Expedited forwarding. One of the per-hop behaviors proposed for Differentiated Services.
EGP:
Exterior Gateway Protocol. An early interdomain routing protocol of the Inter-net, which was
used by exterior gateways (routers) of autonomous systems to exchange
routing information with other ASs. Replaced by BGP.
Encapsulation:
The operation, performed by a lower-level protocol, of attaching a protocol-specific header
and/or trailer to a message passed down by a higher-level pro- tocol. As a message travels down
the protocol stack, it gathers a sequence of headers, of which the outermost corresponds to the
protocol at the bottom of the stack.
Encryption:
The act of applying a transforming function to data, with the intention that only the receiver of
the data will be able to read it (after applying the inverse function,
Decryption
Encryption generally depends on either a secret shared by the sender and receiver or on a
public/private key pair.
Ethernet:
A popular local area network technology that uses CSMA/CD and has a band- width of 10 Mbps.
An Ethernet itself is just a passive wire; all aspects of Ethernet trans- mission are completely
implemented by the host adaptors.
Exponential backoff:
A retransmission strategy that doubles the timeout value each time a packet is retransmitted.
Exposed node problem:
Situation that occurs on a wireless network where two nodes receive signals from a common
source, but each is able to reach other nodes that do not receive this signal.
Extended LAN:
A collection of LANs connected by bridges.
Fabric:
The part of a switch that actually does the switching, that is, moves packets from input to output.
Contrast with port
Fair queuing (FQ):
A round-robin-based queuing algorithm that prevents a badly be- haved process from capturing
an arbitrarily large portion of the network capacity.
CS 2363
COMPUTER NETWORKS
Fast retransmit:
A strategy used by TCP that attempts to avoid timeouts in the presence of lost packets. TCP
retransmits a segment after receiving three consecutive duplicate ACKs, acknowledging the data
up to (but not including) that segment.
FDDI:
Fiber Distributed Data Interface. A token ring networking technology designed to run over
optical fiber.
FEC:
Forward error correction. A general strategy for recovering from bit errors in- troduced into data
packets without having to retransmit the packet. Redundant information is included with each
packet that can be used by the receiver to determine which bits in a packet are incorrect. Contrast
with ARQ.
Forwarding equivalence class.
A set of packets that are to receive the same for- warding treatment at a router. MPLS labels are
normally associated with FECs.
Firewall:
A router that has been configured to filter (not forward) packets from certain sources. Used to
enforce a security policy.
Flow control:
A mechanism by which the receiver of data throttles the transmission rate of the sender, so that
data will not arrive too quickly to be processed. Contrast with congestion control
Flowspec:
Specification of a flows bandwidth and delay requirements presented to the network to establish
a reservation. Used with RSVP.
Forwarding:
The operation performed by a router on every packet: receiving it on an input, deciding what
output to send it to, and sending it there.
Forwarding table:
The table maintained in a router that lets it make decisions on how to forward packets. The
process of building up the forwarding table is called routing ,and thus the forwarding table is
sometimes called a routing table . In some implementations, the routing and forwarding tables
are separate data structures.
Fragmentation/reassembly:
A method for transmission of messages larger than the net- works MTU. Messages are
fragmented into small pieces by the sender and reassembled by the receiver.
Frame:
Another name for a packet, typically used in reference to packets sent over a single link rather
than a whole network. An important problem is how the receiver detects the beginning and
ending of a frame, a problem known as framing.
Frame Relay:
A connection-oriented public packet-switched service offered by the phone company.
Frequency hopping:
A spread spectrum technique that involves transmitting data over a random sequence of
frequencies.
FTP:
CS 2363
COMPUTER NETWORKS
File Transfer Protocol. The standard protocol of the Internet architecture for trans-ferring files
between hosts. Built on top of TCP.
GMPLS:
Generalized MPLS. Allows IP to run natively over optically-switched networks.
GPRS:
General Packet Radio Service. A packet transmission service provided by cellular wireless
networks.
GSM:
Global System for Mobile communication. Digital cellular phone system being deployed
throughout the world (less so in the United States and Canada). Similar to PCS, which is being
deployed throughout the United States and Canada.
Gopher:
An Internet information service.Session control protocol often used for Internet telephony.
Handle:
In programming, an identifier or pointer that is used to access an object.hardware address:
The link-level address used to identify the host adaptor on the local network.
HDLC:
High-Level Data Link Control protocol. An ISO-standard link-level protocol. It uses bit stuffing
to solve the framing problem.
Hidden node problem:
Situation that occurs on a wireless network where two nodes are sending to a common
destination, but are unaware that the other exists.
Hierarchical routing:
A multilevel routing scheme that uses the hierarchical structure of the address space as the basis
for making forwarding decisions. For example, packets might first be routed to a destination
network and then to a specific host on that network.
HiPPI:
High Performance Parallel Interface. An ANSI-standard network technology ca-pable of Gbps
transmission rates, typically used to connect supercomputers to peripheral devices. Used in same
way as Fibre Channel
Host:
A computer attached to one or more networks that supports users and runs appli-cation
programs.
HTML:
HyperText Markup Language. A language used to construct World Wide Web pages.
HTTP:
HyperText Transport Protocol. An application-level protocol based on a re-quest/reply paradigm
and used in the World Wide Web. HTTP uses TCP connections to transfer data.
IAB:
Internet Architecture Board. The main body that oversees the development of the Internet
architecture.
IBGP:
Interior BGP. The protocol used to exchange interdomain routing information among routers in
the same domain.
ICMP:
CS 2363
COMPUTER NETWORKS
Internet Control Message Protocol. This protocol is an integral part of IP. It allows a router or
destination host to communicate with the source, typically to report an error in IP datagram
processing.
IEEE:
Institute for Electrical and Electronics Engineers. A professional society for engi-neers that also
defines network standards, including the 802 series of LAN standards.
Link-level protocol:
A protocol that is responsible for delivering frames over a directly connected network (e.g., an
Ethernet, token ring, or point-to-point link). Also called link-layer protocol.
Link state:
A lowest-cost-path algorithm used in routing. Information on directly con-nected neighbors and
current link costs are flooded to all routers; each router uses this information to build a view of
the network on which to base forwarding decisions. The OSPF routing protocol uses a link-state
algorithm. Contrast with distance vector
LSR:
Label-switching router. A router that runs IP control protocols, but uses the label switching
forwarding algorithm of MPLS.
MAC:
Media access control. Algorithms used to control access to shared-media networks like Ethernet
and FDDI.
MACA:
Multiple access with collision avoidance. Distributed algorithm used to mediate access to a
shared media.
MACAW:
Multiple access with collision avoidance for wireless. Enhancement of the gen-eral MACA
algorithm to better support wireless networks. Used by 802.11.
MAN:
Metropolitan area network. A network based on any of several new network tech- nologies that
operate at high speeds (up to several Gbps) and across distances wide enough to span a
metropolitan area. Contrast with SAN, LAN, and WAN.
Manchester:
A bit-encoding scheme that transmits the exclusive-OR of the clock and the NRZ-encoded data.
Used on the Ethernet.
MBone:
Multicast backbone. A logical network imposed over the top of the Internet, in which multicastenhanced routers use tunneling to forward multicast datagrams across the Internet.
MD5:
Message Digest version 5. An efficient cryptographic checksum algorithm com-monly used to
verify that the contents of a message are unaltered.
MIB:
Management information base. Defines the set of network-related variables that
maybereadorwrittenonanetworknode.TheMIBisusedinconjunctionwithSNMP.
MIME:
Multipurpose Internet Mail Extensions. Specifications for converting binary data (such as image
files) to ASCII text, which allows it to be sent via email.
Mosaic:
CS 2363
COMPUTER NETWORKS
A once-popular and free graphical World Wide Web browser developed at the National Center
for Supercomputing Applications at the University of Illinois.
MP3:
MPEG Layer 3. Audio compression standard used with MPEG.
MPEG:
Moving Picture Experts Group. Typically used to refer to an algorithm for com-pressing video
streams developed by the MPEG.
MPLS:
Multiprotocol Label Switching. A collection of techniques used to effectively im-plement IP
routers on top of level 2 (e.g., ATM) switches.
MSAU:
Multistation access unit. A device used in token ring networks to connect several stations to the
ring and remove them in the event of failure.
MSDP:
Multicast Source Discovery Protocol. A protocol used to facilitate interdomain multicast.
MTU:
Maximum transmission unit. The size of the largest packet that can be sent over a physical
network.
Multicast:
A special form of broadcast in which packets are delivered to a specified sub- group of network
hosts.
Multiplexing:
Combining distinct channels into a single, lower-level channel. For ex-ample, separate TCP and
UDP channels are multiplexed into a single host-to-host IP channel. The inverse operation,
Demultiplexing , takes place on the receiving host.
Name resolution:
The action of resolving host names (which are easy for humans to read) into their corresponding
addresses (which machines can read). See
NAT:
Network address translation. A technique for extending the IP address space that involves
translating between globally understood IP addresses and local-only addresses at the edge of a
network or site.
NDR:
Network Data Representation. The data-encoding standard used in the Distrib- uted Computing
Environment (DCE), as defined by the Open Software Foundation.NDR uses a receiver-makesright strategy and inserts an architecture tag at the front of each message.
Network-level protocol:
A protocol that runs over switched networks, directly above the link level.
NFS:
Network File System. A popular distributed file system developed by Sun Microsys- tems. NFS
is based on SunRPC, an RPC protocol developed by Sun.
NIST:
National Institute for Standards and Technology. The official U.S. standardization body.node:
A generic term used for individual computers that make up a network. Nodes include generalpurpose computers, switches, and routers.
NRZ:
CS 2363
COMPUTER NETWORKS
Nonreturn to zero. A bit-encoding scheme that encodes a 1 as the high signal and a 0 as the low
signal.
NRZI:
Nonreturn to zero inverted. A bit-encoding scheme that makes a transition from the current
signal to encode a 1 and stays at the current signal to encode a 0.
NSF:
National Science Foundation. An agency of the U.S. government that funds sci-entific research
in the United States, including research on networks and on the Internet infrastructure.
Nv:
Network video. A videoconferencing application.
OC:
Optical carrier. The prefix for various rates of SONET optical transmission. For example, OC-1
refers to the SONET standard for 51.84-Mbps transmission over fiber. An OC-nsignal differs
from an STS- n signal only in that the OC- n signal is scrambled for optical transmission.
ONC:
Open Network Computing. A version of SunRPC that is being standardized for the Internet.
Optical switch:
A switching device that forwards optical lightwaves from input port to output port without
converting to electrical format.
OSF:
Open Software Foundation. A consortium of computer vendors that have defined standards for
distributed computing, including the NDR presentation format.
OSI:
Open Systems Interconnection. The seven-layer network reference model devel- oped by the
ISO. Guides the design of ISO and ITU-T protocol standards.
OSPF:
Open Shortest Path First. A routing protocol developed by the IETF for the Inter-net architecture.
OSPF is based on a link-state algorithm, in which every node constructs a topography of the
Internet and uses it to make forwarding decisions. Today known as Open Group.
Overlay:
A virtual (logical) network running on top of an existing physical network. Overlay nodes
communicate with each other through tunnels rather than over physical links. Overlays are often
used to deploy new network services since they do not require the cooperation of the existing
network infrastructure.
Packet:
A data unit sent over a packet-switched network. .
Packet switching:
A general strategy for switching data through a network. Packet switching uses store-andforward switching of discrete data units called packets, and im-plies statistical multiplexing
Participants:
A generic term used to denote the processes, protocols, or hosts that are sending messages to
each other.
PAW S :
Protection against wrapped sequence numbers. Engineering transport protocol with a large
enough sequence number space to protect against the numbers wrapping around on a network
where packets can be delayed for a long period of time.
CS 2363
COMPUTER NETWORKS
PCS:
Personal Communication Services. New digital cellular phone system being de- ployed
throughout the United States and Canada. Similar to GSM, which is being de-ployed throughout
the rest of the world.
PDU:
Protocol data unit. Another name for a packet or frame.
Peer:
A counterpart on another machine that a protocol module interoperates with to implement some
communication service.
peer-to-peer networks:
A general class of applications that integrate application logic
(e.g., file storage) with routing. Popular examples include Napster and Gnutella. Research prototypes often use distributed hash tables.
PEM:
Privacy Enhanced Mail. Extensions to Internet email that support privacy and integrity
protection.
Pretty Good Privacy.
A collection of public domain software that provides privacy and authentication capabilities
using RSA and that uses a mesh of trust for public key distribution.
PHB:
Per-hop behavior. Behavior of individual routers in the Differentiated Services ar-chitecture. AF
and EF are two proposed PHBs.
Physical-level protocol:
The lowest layer of the OSI protocol stack. Its main function is to encode bits onto the signals
that are propagated across the physical transmission media.
Piconet:
Wireless network spanning short distances (e.g., 10m). Used to connect office computers
(laptops, printers, PDAs, workstations, etc.) without cables.
PIM:
Protocol Independent Multicast. A multicast routing potocol that can be built on top of different
unicast routing protocols.
Ping:
A Unix utility used to test the RTT to various hosts over the Internet. Ping sends an ICMP
ECHO_REQUEST message, and the remote host sends an ECHO_ RESPONSE message back.
PIO:
Programmed input/output. An approach to connecting hosts to I/O devices, in which the CPU
reads data from and writes data to the I/O device. Also see
Poison reverse:
Used in conjunction with split horizon . A heuristic technique to avoid routing loops in distancevector routing protocols.
Port:
A generic term usually used to mean the point at which a network user attaches to the network.
On a switch, a port denotes the input or output on which packets are received and sent.
POTS:
Plain old telephone service. Used to specify the existing phone service, in contrast to ISDN,
ATM, or other technologies that the telephone companies offer now or may offer in the future.
CS 2363
COMPUTER NETWORKS
PPP:
Point-to-Point Protocol. Data link protocol typically used to connect computers over a dial-up
line.
Process:
An abstraction provided by an operating system to enable different operations to take place
concurrently. For example, each user application usually runs inside its own
process, while various operating system functions take place in other processes.
promiscuous mode:
Protocol:
A specification of an interface between modules running on different ma- chines, as well as the
communication service that those modules implement. The term is also used to refer to an
implementation of the module that meets this specification. To distinguish between these two
uses, the interface is often called a protocol specification .
Proxy:
An agent sitting between a client and server that intercepts messages and provides some service.
For example, a proxy can stand in for a server by responding to client requests, perhaps using
data it has cached, without contacting the server.
Pseudoheader:
AsubsetoffieldsfromtheIPheaderthatarepasseduptotransportpro- tocols TCP and UDP for use in
their checksum calculation. The pseudoheader contains source and destination IP addresses and
IP datagram length, thus enabling detection of corruption of these fields or delivery of a packet
to an incorrect address.
Public key encryption:
Any of several encryption algorithms (e.g., RSA)iwhich each participant has a private key
(shared with no one else) and a public key (available to everyone). A secure message is sent to a
user by encrypting the data with that users public key; possession of the private key is required
to decrypt the message, and so only the receiver can read it.
QoS:
Quality of service. Packet delivery guarantees provided by a network architecture.Usually related
to performance guarantees, such as bandwidth and delay. The Internet offers a best-effort
delivery service, meaning that every effort is made to deliver a packet but delivery is not
guaranteed.
RED:
Random early detection. A queuing discipline for routers in which, when conges-tion is
anticipated, packets are randomly dropped to alert the senders to slow down.rendezvous point:
A router used by PIM to allow receivers to learn about senders.
Repeater:
A device that propagates electrical signals from one Ethernet cable to another. There can be a
maximum of two repeaters between any two hosts in an Ethernet. Re- peaters forward signals,
whereas bridges forward frames ,and routers and switches forward packets
REST:
Representational State Transfer. An approach to building web services that uses HTTP as the
generic application protocol.
Reverse-path broadcast (RPB):
A technique used to eliminate duplicate broadcast packets.
RFC:
CS 2363
COMPUTER NETWORKS
Request for Comments. Internet reports that contain, among other things, specifi- cations for
protocols like TCP and IP.
RIO:
RED with In and Out. A packet drop policy based on RED, but involving two drop curves: one
for packets that have been marked as being in profile and one for packets that have been
marked out of profile. Designed to be used to implement differentiated services.
RIP:
Routing Information Protocol. An intradomain routing protocol supplied with Berkeley Unix.
Each router running RIP dynamically builds its forwarding table based on a distance-vector
algorithm.
Router:A network node connected to two or more networks that forwards packets from one
network to another. Contrast with bridge,repeater,,and switch .
Routing:
The process by which nodes exchange topological information to build correct forwarding tables.
RPC:
Remote Procedure Call. Synchronous request/reply transport protocol used in many client/server
interactions.
RPR:
Resilient Packet Ring. A type of ring network that is mostly used in metropolitan
area networks.
RSA:
A public-key encryption algorithm named after its inventors: Rivest, Shamir, and Adleman.
RSVP:
Resource Reservation Protocol. A protocol for reserving resources in the network. RSVP uses
the concept of soft state in routers and puts responsibility for making reserva- tions on receivers
instead of on senders.
RTCP:
Real-time Transport Control Protocol. Control protocol associated with RTP.
RTP:
Real-time Transport Protocol. An end-to-end protocol used by multimedia appli-cations that
have real-time constraints.
RTT:
Round-trip time. The time it takes for a bit of information to propagate from one end of a link or
channel to the other and back again; in other words, double the latency of the channel.
SAN:
Storage area network. A network that spans the components of a computer system (e.g., display,
camera, disk). Includes interfaces like HiPPI and Fibre Channel. Contrast with LAN, MAN,
And WAN.
Schema:
A specification of how to structure and interpret a set of data. Schema are defined for XML
documents.
Scrambling:
The process of XORing a signal with a pseudorandom bitstream before transmission to cause
enough signal transitions to allow clock recovery. Scrambling is used in SONET.
SDP:
CS 2363
COMPUTER NETWORKS
Session Description Protocol. An application layer protocol used to learn about the available
audio/video channels. It reports the name and purpose of the session, start and end times for the
session, the media types (e.g., audio, video) that comprise the ses-sion, and detailed information
needed to receive the session (e.g., the multicast address,transport protocol, and port numbers to
be used).
Segment:
A TCP packet. A segment contains a portion of the byte stream that is being sent by means of
TCP.
semaphore:
A variable used to support synchronization between processes. Typically aprocess Blocks on a
semaphore while it waits for some other process to signal the semaphore.
Server:
The provider of a service in a client/server distributed system.
SONET:
Synchronous Optical Network. A clock-based framing standard for digital trans-mission over
optical fiber. It defines how telephone companies transmit data over optical networks.
Source routing:
Routing decisions performed at the source before the packet is sent. The route consists of the list
of nodes that the packet should traverse on the way to the destination.
Source-specific multicast:
A mode of multicast in which a group may have only a single sender.
Sparse mode multicast:
A mode used in PIM when relatively few hosts or routers need to receive multicast data for a
certain group.
Split horizon:
A method of breaking routing loops in a distance-vector routing algo-rithm. When a node sends a
routing update to its neighbors, it does not send those routes it learned from each neighbor back
to that neighbor. Split horizon is used with poison reverse
Spread spectrum:
Encoding technique that involves spreading a signal over a wider fre-quency than necessary, so
as to minimize the impact of interference.
SSL:
Secure Socket Layer. A protocol layer that runs over TCP to provide authentication and
encryption of connections. Also known as Transport Layer Security (TLS).
Statistical multiplexing:
Demand-based multiplexing of multiple data sources over a shared link or channel.
Stop-and-wait:
A reliable transmission algorithm in which the sender transmits a packet and waits for an
acknowledgment before sending the next packet. Compare with sliding window and concurrent
logical channels
STS:
Synchronous Transport Signal. The prefix for various rates of SONET transmis-sion. For
example, STS-1 refers to the SONET standard for 51.84-Mbps transmission.
Subnetting:
CS 2363
COMPUTER NETWORKS
The use of a single IP network address to denote multiple physical networks.Routers within the
subnetwork use a subnet mask to discover the physical network to which a packet should be
forwarded. Subnetting effectively introduces a third level to
the two-level hierarchical IP address.
Switch:
A network node that forwards packets from inputs to outputs based on header information in
each packet. Differs from a router mainly in that it typically does not interconnect networks of
different types.
Switching fabric:
The component of a switch that directs packets from their inputs to the correct outputs.
TCP:
Transmission Control Protocol. Connection-oriented transport protocol of the In-ternet
architecture. TCP provides a reliable, byte-stream delivery service.
TDMA:
Time Division Multiple Access. A form of multiplexing used in cellular wireless networks. Also
the name of a particular wireless standard.
Te l n e t :
Remote terminal protocol of the Internet architecture. Telnet allows you to in-teract with a
remote system as if your terminal is directly connected to that machine.
Throughput:
The observed rate at which data is sent through a channel. The term is often used
interchangeably with bandwidth
TLS:
Transport Layer Security. Security services that can be layered on top of a transport protocol like
TCP. It is often used by HTTP to perform secure transactions on the World Wide Web. Derived
from SSL
Token bucket:
A way to characterize or police the bandwidth used by a flow. Conceptu-ally, processes
accumulate tokens over time, and they must spend a token to transmit a byte of data and then
must stop sending when they have no tokens left. Thus, overall bandwidth is limited, with the
accommodation of some burstiness.
Token ring:
A physical network technology in which hosts are connected in a ring. A to- ken (bit pattern)
circulates around the ring. A given node must possess the token before it is allowed to transmit.
802.5 and FDDI are examples of token ring networks.
Transport protocol:
An end-to-end protocol that enables processes on different hosts to communicate. TCP is the
canonical example.
TTL:
Time to live. Usually a measure of the number of hops (routers) an IP datagram can visit before
it is discarded.
Tunneling:
Encapsulating a packet using a protocol that operates at the same layer as the packet. For
example, multicast IP packets are encapsulated inside unicast IP packets to tunnel across the
Internet to implement the MBone. Tunneling will also be used during the transition from IPv4 to
IPv6.
CS 2363
COMPUTER NETWORKS
Ty m n e t :
An early network in which a virtual circuit abstraction was maintained across a set of routers.
UBR:
Unspecified bit rate. The no frills service class in ATM, offering best-effort cell delivery.
Contrast with ABR, CBR,and VBR.
UDP:
User Datagram Protocol. Transport protocol of the Internet architecture that pro-vides a
connectionless datagram service to application-level processes.
UMTS:
Universal Mobile Telecommunications System. Cellular wireless standard based on wideband
CDMA that offers relatively high data rates.
Unicast:
Sending a packet to a single destination host. Contrast with broadcast and multicast
URI:
Uniform Resource Identifier. A generalization of the URL. Used for example, in conjunction
with SIP to set up audio/visual sessions.
URL:
Uniform Resource Locator. A text string used to identify the location of Internet resources. A
typical URL looks like http://www.cisco.com . In this URL, http is the protocol to use to access
the resource located on host www.cisco.com .
Vat:
Audioconferencing tool used on the Internet that runs over RTP.
VBR:
Variable bit rate. One of the classes of service in ATM, intended for applications with bandwidth
requirements that vary with time, such as compressed video. Contrast with ABR, CBR,and
UBR.
VCI:
Virtual circuit identifier. An identifier in the header of a packet that is used for virtual circuit
switching. In the case of ATM, the VPI and VCI together identify the end-to-end connection.
Vic:
Unix-based videoconferencing tool that uses RTP.
Virtual circuit:
The abstraction provided by connection-oriented networks such as ATM.Messages must usually
be exchanged between participants to establish a virtual circuit (and perhaps to allocate resources
to the circuit) before data can be sent. Contrast with datagram
Virtual clock:
A service model that allows the source to reserve resources on routers using a rate-based
description of its needs. Virtual clock goes beyond the best-effort delivery service of the current
Internet.
VPI:
Virtual path identifier. An 8-bit or 12-bit field in the ATM header. VPI can be used to hide
multiple virtual connections across a network inside a single virtual path, thus decreasing the
amount of connection state that the switches must maintain. .
VPN:
CS 2363
COMPUTER NETWORKS
Virtual private network. A logical network overlaid on top of some existing net-work. For
example, a company with sites around the world may build a virtual network on top of the
Internet rather than lease lines between each site.
WAN:
Wide area network. Any physical network technology that is capable of spanning long distances
(e.g., cross-country). Compare with SAN, LAN, and MAN.
Weighted fair queuing (WFQ):
A variation of fair queuing in which each flow can be given a different proportion of the network
capacity.
Well-known port:
A port number that is, by convention, dedicated for use by a particular server. For instance, the
Domain Name Server receives messages at well-known UDP and TCP port 53 on every host.
WSDL:
Web Services Description Language. A component of the web services frame-work for
specifying and implementing application protocols.
WWW:
World Wide Web. A hypermedia information service on the Internet.
X.25:
The ITU packet-switching protocol standard
CS 2363
COMPUTER NETWORKS
UNIT I
DATA COMMUNICATION
PART A (2 MARKS)
1. Define Data communication
2. What are the five important components of data communications?
3. With the example define half duplex communication.
4. Name four topologies of computer networks.
5. Suggest two points to improve the performance of network.
6. Define Distributed Systems and Distributed Processing
7. What are the design factors for transmission media?
8. What is the advantage and disadvantage of RS-232?
9. What is the Null modem?
10. Define Topology
11. Define standards and why are standards needed?
12. Who are the standard committee?
13. Define OSI model and what are the seven l yers of OSI Model?
14. Different types of encoding
PART B(16 MARKS)
1.
2.
3.
4.
5.
CS 2363
COMPUTER NETWORKS
UNIT II
DATA LINKLAYER
PART A (2 Marks)
1.What are the responsibilities of data link layer?
2.Mention the types of errors.
3.What is redundancy?
4.List out the available detection methods.
5.Write short notes on VRC, LRC, and CRC
6.Write short notes on CRC generator & checker.
7.Define checksum.
8. Write short notes on error correction.
9. Mention the types of error correcting methods.
10. What is the purpose of hamming code?
11. Mention the categories of flow control.
12. What is the function of stop and wait flow control?
13. Mention the advantage and disadvantage of stop and wait flow control. 14. Define ARQ.
15. Mention the function of go-back N-ARQ. 16. What is selective reject ARQ?
16. Define Data link layer.
17. Define HDLC.
18. List the types of stations is HDLC.
19. What are the different communication modes in HDLC?
20. Mention the types of frames in HDLC.
21. What is meant by bit stuffing?
22. What is meant by LAN and Mention the various architecture in a LAN? 23. Define a
standard 802.3
23. Write short notes on FDDI.
24. What is piggy backing?
PART B (16 Mark )
1. Explain error detection and error correction techniques.
2. Explain error control mechanism.
3. Explain
the
flow
control
mechanism
4. Explain the timers and time registers in FDDI.
5. Explain about Ethernet.
6. Explain about HDLC.
7. Explain about SONET and Bridges.
CS 2363
COMPUTER NETWORKS
UNIT III
NETWORK LAYER
PART A (2 Marks)
1.What are the network support layers and the user support layers?
2.What are the functions of LLC?
3.What are the functions of MAC?
4.What is protocol data unit?
5.What are headers and trailers and how do they get added and removed?
6.What are the responsibilities of network layer?
7.What is a virtual circuit?
8.What are data grams?
9.What are the two types of implementation formats in virtual circuits?
10.What is meant by switched & permanent virtual circuit?
11.Define Routers.
12.What is meant by hop count?
13.How can the routing be classified?
14.What is time-to-live or packet lifetime?
15.Write the keys for understanding the distance vector & link state routing.
16.How the packet cost referred in distance vector and link state routing?
17.How the routers get the information about neighbor?
18.What are the four internetworking devices?
19.Define IP address.
20.Define Gateway.
21.What is LSP?
22.Define Subnetting.
23.Define Masking.
PART B (16 Marks)
1.Explain the two approaches of packet switching techniques.
2.Explain IP addressing method.
3.Define routing & explain distance vector routing and link state routing.
4.Define Routers and explain the type of routers.
5. Explain sub netting
6. Write short notes about repeaters, routers and gateways.
CS 2363
COMPUTER NETWORKS
UNIT-IV
TRANSPORT LAYER
PART A (2 Marks)
1.What is function of transport layer?
2.What are the four aspectswwwrelated to the reliable delivery of data?
3.What is meant by segment?
4. What are the types of multiplexing?
5. Define transport layer/
6.Define Multiplexing.
7.What are the two possible transport services?
8.The transport layer creates the connection between source and destination. What are the three
events involved in the connection?
9.What are the techniques used in multiplexing?
10.What is meant by congestion?
11.Why the congestion occur in network?
12.How will the congestion be avoided?
13.What is the function of BECN BIT?
14.What is the function of FECN?
15.What is meant by quality of service?
16.What are the two categories of QOS attributes?
17.What are the networks & user related attributes?.
18.What is frame & framing bits?
19.What is interleaving?
PART B ( 16 Marks )
1.Explain the duties of transport layer.
2.Explain UDP & TCP.
3.Explain about congestion control.
4.Explain leaky bucket and token bucket algorithm
CS 2363
COMPUTER NETWORKS
UNIT V
APPLICATION LAYER
PART A (2 Marks)
1.What is the purpose of Domain Name System?
2.Discuss the three main division of the domain name space.
3.Discuss the TCP connections needed in FTP.
4.Discuss the basic model of FTP. com
5.What is the function of SMTP?
6.What is the difference between a user agent (UA) and a mail transfer agent?
7.How does MIME enhance SMTP? .
8.Why is an application such as POP neededxams9for electronic messaging?
9.Give the format of HTTP request message.
10.Give the format of HTTP response message.
11.Write down the three types of WWW documents.
12.What is the purpose of HTML?
13.Define CGI.
14.Name four factors needed for a secure.network.
15.How is a secret key differentwwwfrom public key?
16.What is a digital signature?
17.Define permutation.
18.Define substitution & transposition encryption.
19.What is meant by cryptography?
PART B (16 Marks)
1.Explain the functions of SMTP.
2.Write short notes on FTP.
3.Explain about HTTP.
4.Explain the WWW in detail.
5.Explain the type of Conventional encryption/decryption method.
15.
16.
17.
9.
10.
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
UNIT-I
DATA COMMUNICATION
PART A (2 Marks)
1. Define Data communication
2.What are the five important components of data
communications?
3.Define full duplex
4.Define Topology
5.What is Network?
6. With the example define half duplex communication.
7. Name four topologies of computer networks.
8. Suggest two points to improve the performance of network.
9.Define Distributed syetm
10. What are the design factors for transmission media?
11. Define Transit time and Response time
12. Define Network architecture
13. Why are protocols needed and what are the key elements?
14. What is the advantage and disadvantage of RS-232?
.
PART B (16 Marks)
Explain ISO/ OSI reference model ith neat diagram (16)
What is meant by topology? and explain the topologies of
the network. (16)
Explain the categories
of net orks. (16)
www
Explain line coding (digital to digital conversion). (16)
Discuss in detail with the RS232 interfacing sequences.
(16)
CS 2363
COMPUTER NETWORKS
UNIT -II
DATA LINKLAYER
PART A (2 Marks)
1.What are the responsibilities of data link layer?
2.Mention the types of errors.
3.What is redundancy?
4.List out the available detection methods.
5.Write short notes on VRC, LRC, and CRC
6.Write short notes on CRC generator & checker.
7.Define checksum.
www.xams9.com
8. Write short notes on error correction.
9. Mention the types of error correcting methods.
10. What is the purpose of hamming code?
11. Mention the categories of flow control.
12. What is the function of stop and wait flow control?
13. Mention the advantage and disadvantage of stop and wait flow control. 14. Define ARQ.
15. Mention the function of go-back N-ARQ. 16. What is selective reject ARQ?
17. Define HDLC.
18. List the types of stations is HDLC.
19. What are the different communication modes in HDLC?
20. Mention the types of frames in HDLC.
21. What is meant by bit stuffing?
22. What is meant by LAN and Mention the various architecture in a LAN? 23. Define a
standard 802.3
24. Write short notes on FDDI.
25. What is piggy backing?
com
.
PART B (16 Mark )
1.Explain error detection and error correction techniques. (16)
2.Explain error control mechanism. (16)
3.Explain the flow control mechanism (16)
4.Explain the timers and time registers in FDDI. (16)
xams9
.
5.Explain about Ethernet. (16)
6.Explain about HDLC. (16)
7.Explain about SONET and Bridges. (16)
www
UNIT -III
NETWORK LAYER
PART A (2 Marks)
1.What are the network support layers and the user support layers?
2.What are the functions of LLC?
3.What are the functions of MAC?
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
Foo::~Foo()
{
// This is the implementation of the destructor
}
int main()
{
Foo foo(14); // call first constructor
Foo foo2(12, 16); // call overloaded constructor
Foo foo3(foo); // call the copy constructor
return 0;
// destructors called in backwards-order
// here, automatically
1. POLYMORPHISM AND DATA ABSTRACTION
4.1 POLYMORPHISM
many forms
Greek
o "poly" many
o "morph" form
the same method can be called on different objects
they may respond to it in different ways
all Vehicles have a move method
Cars and Truck drive
Airplanes fly
#include "Vehicle.h"
int main(){
Vehicle v ("Transporter 54");
Airplane a("Tornado 2431", 14);
LandVehicle lv("My wheels");
Car c("Ford Anglia 22");
Truck t("Red pickup");
v.move();
a.move();
lv.move();
c.move();
t.move();
}
OUTPUT
Vehicle constructor
Vehicle constructor
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Airplane constructor
Vehicle constructor
Land vehicle constructor
Vechicle constructor
Land vehicle constructor
Car constructor
Vechicle constructor
Land vehicle constructor
Truck constructor
Vehicle Transporter 54 moving
Airplane Tornado 2431 flying
Land Vehicle My wheels driving
Land Vehicle Ford Anglia 22 driving
Land Vehicle Red pickup driving
Polymorphic behaviour
to get polymorphic behaviour, we would like the version of move() to be determined at
run-time
if moveVehicle is sent an Airplane object, it should get it to fly
do this by using the virtual keyword in the first (base class) declaration of the
polymorphic method
class Vehicle {
protected:
string name;
public:
// other members
virtual void move() { cout << "Vehicle " << name << " moving" << endl; }
};
now it works
Vehicle Transporter 54 moving
Airplane Tornado 2431 flying
Land Vehicle My wheels driving
Land Vehicle Ford Anglia 22 driving
Land Vehicle Red pickup driving
polymorphism allows us to use a pointer to a derived type object wherever a pointer to
base type is expected
Car c("Ford Anglia 22");
Vehicle * v2 = &c;
v2->move();
Vehicle & v3 = c;
v3.move();
only works for pointer and reference types
they store an address same size for all objects
Airplane a("Tornado 2431", 14);
Vehicle v2 = a;
v2.move();
trying to fit an airplane into a space meant for any vehicle
CS 2363
COMPUTER NETWORKS
can call the move() method, but we've lost the wingspan member variable
Polymorphism:
Ability for objects of different classes to respond differently to the same function
call
Base-class pointer (or reference) calls a virtual function
C++ chooses the correct overridden function in object
Suppose print not a virtual function
Employee e, *ePtr = &e;
HourlyWorker h, *hPtr = &h;
ePtr->print();
//call
base-class
print
function
hPtr->print();
//call
derived-class
print
function
ePtr=&h;
//allowable
implicit
conversion
ePtr->print(); // still calls base-class print
This is where you build a fence around program segments, preventing some parts of the
program from seeing how tasks are being accomplished.
Any use of globals causes side effects that may not be predictable, reducing the viability
of procedural abstraction
4.2.2Modular Abstraction
With modular abstraction, we build a screen surrounding the internal structure of our
program prohibiting programmers from accessing the data except through specified
functions.
Many times data structures (e.g., structures) common to a module are placed in a header
files along with prototypes (allows external references)
4.2.3 Data Abstraction
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
The representation of objects of the type is hidden from the program units that use
these objects, so the only operations possible are those provided in the type's
definition
CS 2363
COMPUTER NETWORKS
Returning the end of the sequence is the standard idiom for not found or unsuccessful
CS 2363
3. CONTAINERS
(hold sequences in difference ways)
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
find()
void f(vector<int>& v, int x)
// works for vector of ints
{
vector<int>::iterator p = find(v.begin(),v.end(),x);
if (p!=v.end()) { /* we found x */ }
//
}
void f(list<string>& v, string x)
// works for list of strings
{
list<string>::iterator p = find(v.begin(),v.end(),x);
if (p!=v.end()) { /* we found x */ }
//
}
void f(set<double>& v, double x)
// works for set of doubles
{
set<double>::iterator p = find(v.begin(),v.end(),x);
if (p!=v.end()) { /* we found x */ }
//
}
}
CS 2363
COMPUTER NETWORKS
1. TEMPLATES
Templates are a way to make code more reusable. Trivial examples include creating
generic data structures which can store arbitrary data types. Templates are of great utility to
programmers, especially when combined with multiple inheritance and operator overloading.
The Standard Template Library (STL) provides many useful functions within a framework of
connected templates.
As templates are very expressive they may be used for things other than generic
programming. One such use is called template metaprogramming, which is a way of preevaluating some of the code at compile-time rather than run-time. Further discussion here only
relates to templates as a method of generic programming.
By now you should have noticed that functions that perform the same tasks tend to look
similar. For example, if you wrote a function that prints an int, you would have to have the int
declared first. This way, the possibility of error in your code is reduced, however, it gets
somewhat annoying to have to create different versions of functions just to handle all the
different data types you use. For example, you may want the function to simply print the input
variable, regardless of what type that variable is. Writing a different function for every possible
input type (double, char *, etc. ...) would be extremely cumbersome. That is where templates
come in.
Templates solve some of the same problems as macros, generate "optimized" code at
compile time, but are subject to C++'s strict type checking.
Parameterized types, better known as templates, allow the programmer to create one function
that can handle many different types. Instead of having to take into account every data type, you
have one arbitrary parameter name that the compiler then replaces with the different data types
that you wish the function to use, manipulate, etc.
Templates are instantiated at compile-time with the source code.
Templates are type safe.
Templates allow user-defined specialization.
Templates allow non-type parameters.
Templates use lazy structural constraints.
Templates support mix-ins.
Syntax for Templates
Templates are pretty easy to use, just look at the syntax:
template <class TYPEPARAMETER>
(or, equivalently, and preferred by some)
template <typename TYPEPARAMETER>
Function template
There are two kinds of templates. A function template behaves like a function that can accept
arguments of many different types. For example, the Standard Template Library contains the
function template max(x, y) which returns either x or y, whichever is larger. max() could be
defined like this:
template <typename TYPEPARAMETER>
TYPEPARAMETER max(TYPEPARAMETER x, TYPEPARAMETER y)
{
CS 2363
COMPUTER NETWORKS
if (x < y)
return y;
else
return x;
}
This template can be called just like a function:
std::cout << max(3, 7); // outputs 7
The compiler determines by examining the arguments that this is a call to max(int, int)
and instantiates a version of the function where the type TYPEPARAMETER is int.
This works whether the arguments x and y are integers, strings, or any other type for which it
makes sense to say "x < y". If you have defined your own data type, you can use operator
overloading to define the meaning of < for your type, thus allowing you to use the max()
function. While this may seem a minor benefit in this isolated example, in the context of a
comprehensive library like the STL it allows the programmer to get extensive functionality for a
new data type, just by defining a few operators for it. Merely defining < allows a type to be used
with the standard sort(), stable_sort(), and binary_search() algorithms; data structures such as
sets, heaps, and associative arrays; and more.
As a counterexample, the standard type complex does not define the < operator, because
there is no strict order on complex numbers. Therefore max(x, y) will fail with a compile error if
x and y are complex values. Likewise, other templates that rely on < cannot be applied to
complex data. Unfortunately, compilers historically generate somewhat esoteric and unhelpful
error messages for this sort of error. Ensuring that a certain object adheres to a method protocol
can alleviate this issue.
{TYPEPARAMETER} is just the arbitrary TYPEPARAMETER name that you want to
use in your function. Some programmers prefer using just T in place of TYPEPARAMETER.
Let us say you want to create a swap function that can handle more than one data type...
something that looks like this:
template <class SOMETYPE>
void swap (SOMETYPE &x, SOMETYPE &y)
{
SOMETYPE temp = x;
x = y;
y = temp;
}
The function you see above looks really similar to any other swap function, with the
differences being the template <class SOMETYPE> line before the function definition and the
instances of SOMETYPE in the code. Everywhere you would normally need to have the name or
class of the datatype that you're using, you now replace with the arbitrary name that you used in
the template <class SOMETYPE>. For example, if you had SUPERDUPERTYPE instead of
SOMETYPE, the code would look something like this:
template <class SUPERDUPERTYPE>
void swap (SUPERDUPERTYPE &x, SUPERDUPERTYPE &y)
{
SUPERDUPERTYPE temp = x;
x = y;
y = temp;
CS 2363
COMPUTER NETWORKS
}
As you can see, you can use whatever label you wish for the template
TYPEPARAMETER, as long as it is not a reserved word.
Class template
A class template extends the same concept to classes. Class templates are often used to
make generic containers. For example, the STL has a linked list container. To make a linked list
of integers, one writes list<int>. A list of strings is denoted list<string>. A list has a set of
standard functions associated with it, which work no matter what you put between the brackets.
If you want to have more than one template TYPEPARAMETER, then the syntax would be:
template <class SOMETYPE1, class SOMETYPE2, ...>
1.1.1 Templates and Classes
Let us say that rather than create a simple templated function, you would like to use
templates for a class, so that the class may handle more than one datatype. You may have noticed
that some classes are able to accept a type as a parameter and create variations of an object based
on that type (for example the classes of the STL container class hierarchy). This is because they
are declared as templates using syntax not unlike the one presented below:
template <class T> class Foo
{
public:
Foo();
void some_function();
T some_other_function();
private:
int member_variable;
T parametrized_variable;
};
Defining member functions of a template class is somewhat like defining a function
template, except for the fact, that you use the scope resolution operator to indicate that this is the
template classes' member function. The one important and non-obvious detail is the requirement
of using the template operator containing the parametrized type name after the class name.
The following example describes the required syntax by defining functions from the example
class above.
template <class T> Foo<T>::Foo()
{
member_variable = 0;
}
template <class T> void Foo<T>::some_function()
{
cout << "member_variable = " << member_variable << endl;
}
template <class T> T Foo<T>::some_other_function()
{
return parametrized_variable;
CS 2363
COMPUTER NETWORKS
}
As you may have noticed, if you want to declare a function that will return an object of
the parametrized type, you just have to use the name of that parameter as the function's return
type.
Advantages and disadvantages
Some uses of templates, such as the max() function, were previously filled by function-like
preprocessor macros.
// a max() macro
#define max(a,b) ((a) < (b) ? (b) : (a))
Both macros and templates are expanded at compile time. Macros are always expanded
inline; templates can also be expanded as inline functions when the compiler deems it
appropriate. Thus both function-like macros and function templates have no run-time overhead.
However, templates are generally considered an improvement over macros for these purposes.
Templates are type-safe. Templates avoid some of the common errors found in code that makes
heavy use of function-like macros. Perhaps most importantly, templates were designed to be
applicable to much larger problems than macros. The definition of a function-like macro must fit
on a single logical line of code.
There are three primary drawbacks to the use of templates. First, many compilers
historically have very poor support for templates, so the use of templates can make code
somewhat less portable. Second, almost all compilers produce confusing, unhelpful error
messages when errors are detected in template code. This can make templates difficult to
develop. Third, each use of a template may cause the compiler to generate extra code (an
instantiation of the template), so the indiscriminate use of templates can lead to code bloat,
resulting in excessively large executables.
The other big disadvantage of templates is that to replace a #define like max which acts
identically with dissimilar types or function calls is impossible. Templates have replaced using
#defines for complex functions but not for simple stuff like max(a,b). For a full discussion on
trying to create a template for the #define max, see the paper "Min, Max and More" that Scott
Meyer wrote for C++ Report in January 1995.
The biggest advantage of using templates, is that a complex algorithm can have a simple
interface that the compiler then uses to choose the correct implementation based on the type of
the arguments. For instance, a searching algorithm can take advantage of the properties of the
container being searched. This technique is used throughout the C++ standard library.
Linkage problems
While linking a template-based program consisting over several modules spread over a
couple files, it is a frequent and mystifying situation to find that the object code of the modules
won't link due to 'unresolved reference to (insert template member function name here) in (...)'.
The offending function's implementation is there, so why is it missing from the object code? Let
us stop a moment and consider how can this be possible.
Assume you have created a template based class called Foo and put its declaration in the
file Util.hpp along with some other regular class called Bar:
template <class T> Foo
{
public:
Foo();
T some_function();
CS 2363
COMPUTER NETWORKS
T some_other_function();
T some_yet_other_function();
T member;
};
class Bar
{
Bar();
void do_something();
};
Now, to adhere to all the rules of the art, you create a file called Util.cc, where you put all the
function definitions, template or otherwise:
#include "Util.hpp"
template <class T> T Foo<T>::some_function()
{
...
}
template <class T> T Foo<T>::some_other_function()
{
...
}
template <class T> T Foo<T>::some_yet_other_function()
{
...
}
and, finally:
void Bar::do_something()
{
Foo<int> my_foo;
int x = my_foo.some_function();
int y = my_foo.some_other_function();
}
Next, you compile the module, there are no errors, you are happy. But suppose there is an
another (main) module in the program, which resides in MyProg.cc:
#include "Util.hpp"
// imports our utility classes' declarations, including the template
int main()
{
Foo<int> main_foo;
int z = main_foo.some_yet_other_function();
return 0;
}
CS 2363
COMPUTER NETWORKS
This also compiles clean to the object code. Yet when you try to link the two modules
together, you get an error saying there is an undefined reference to Foo<int>::some_yet_other
function() in MyProg.cc. You defined the template member function correctly, so what is the
problem?
As you remember, templates are instantiated at compile-time. This helps avoid code
bloat, which would be the result of generating all the template class and function variants for all
possible types as its parameters. So, when the compiler processed the Util.cc code, it saw that the
only variant of the Foo class was Foo<int>, and the only needed functions were:
int Foo<int>::some_function();
int Foo<int>::some_other_function();
No code in Util.cc required any other variants of Foo or its methods to exist, so the
compiler generated no code other than that. There is no implementation of
some_yet_other_function() in the object code, just as there is no implementation for
double Foo<double>::some_function();
or
string Foo<string>::some_function();
The MyProg.cc code compiled without errors, because the member function of Foo it
uses is correctly declared in the Util.hpp header, and it is expected that it will be available upon
linking. But it is not and hence the error, and a lot of nuisance if you are new to templates and
start looking for errors in your code, which ironically is perfectly correct.
The solution is somewhat compiler dependent. For the GNU compiler, try experimenting
with the -frepo flag, and also reading the template-related section of 'info gcc' (node "Template
Instantiation": "Where is the Template?") may prove enlightening. In Borland, supposedly, there
is a selection in the linker options, which activates 'smart' templates just for this kind of problem.
The other thing you may try is called explicit instantiation. What you do is create some
dummy code in the module with the templates, which creates all variants of the template class
and calls all variants of its member functions, which you know are needed elsewhere. Obviously,
this requires you to know a lot about what variants you need throughout your code. In our simple
example this would go like this:
1. Add the following class declaration to Util.hpp:
class Instantiations
{
private:
void Instantiate();
};
2. Add the following member function definition to Util.cc:
void Instantiations::Instantiate()
{
Foo<int> my_foo;
my_foo.some_yet_other_function();
// other explicit instantiations may follow
}
we never need to actually instantiate the Instantiations class, or call any of its methods.
The fact that they just exist in the code makes the compiler generate all the template variations
which are required. Now the object code will link without problems.
CS 2363
COMPUTER NETWORKS
There is still one, if not elegant, solution. Just move all the template functions' definition code to
the Util.hpp header file. This is not pretty, because header files are for declarations, and the
implementation is supposed to be defined elsewhere, but it does the trick in this situation. While
compiling the MyProg.cc (and any other modules which include Util.hpp) code, the compiler
will generate all the template variants which are needed, because the definitions are readily
available.
1.1.2 Template Meta-programming overview
Template meta-programming (TMP) refers to uses of the C++ template system to
perform computation at compile-time within the code. It can, for the most part, be considered to
be "programming with types" in that, largely, the "values" that TMP works with are specific
C++ types. Using types as the basic objects of calculation allows the full power of the typeinference rules to be used for general-purpose computing.
1.1.3 Compile-time programming
The preprocessor allows certain calculations to be carried out at compile time, meaning
that by the time the code has finished compiling the decision has already been taken, and can be
left out of the compiled executable. The following is a very contrived example:
#define myvar 17
#if myvar % 2
cout << "Constant is odd" << endl;
#else
cout << "Constant is even" << endl;
#endif
This kind of construction does not have much application beyond conditional inclusion of
platform-specific code. In particular there's no way to iterate, so it can not be used for general
computing. Compile-time programming with templates works in a similar way but is much more
powerful, indeed it is actually Turing complete.
Traits classes are a familiar example of a simple form of template meta-programming:
given input of a type, they compute as output properties associated with that type (for example,
std::iterator_traits<> takes an iterator type as input, and computes properties such as the iterator's
difference_type, value_type and so on).
1.1.4 The nature of template meta-programming
Template meta-programming is much closer to functional programming than ordinary
idiomatic C++ is. This is because 'variables' are all immutable, and hence it is necessary to use
recursion rather than iteration to process elements of a set. This adds another layer of challenge
for C++ programmers learning TMP: as well as learning the mechanics of it, they must learn to
think in a different way.
Limitations of Template Meta-programming
Because template meta-programming evolved from an unintended use of the template
system, it is frequently cumbersome. Often it is very hard to make the intent of the code clear to
a maintainer, since the natural meaning of the code being used is very different from the purpose
to which it is being put. The most effective way to deal with this is through reliance on idiom; if
you want to be a productive template meta-programmer you will have to learn to recognize the
common idioms.
It also challenges the capabilities of older compilers; generally speaking, compilers from
around the year 2000 and later are able to deal with much practical TMP code. Even when the
CS 2363
COMPUTER NETWORKS
compiler supports it, the compile times can be extremely large and in the case of a compile
failure the error messages are frequently impenetrable.
Some coding standards may even forbid template meta-programming, at least outside of thirdparty libraries like Boost.
History of TMP
Historically TMP is something of an accident; it was discovered during the process of
standardizing the C++ language that its template system happens to be Turing-complete, i.e.,
capable in principle of computing anything that is computable. The first concrete demonstration
of this was a program written by Erwin Unruh which computed prime numbers although it did
not actually finish compiling: the list of prime numbers was part of an error message generated
by the compiler on attempting to compile the code.[1] TMP has since advanced considerably,
and is now a practical tool for library builders in C++, though its complexities mean that it is not
generally appropriate for the majority of applications or systems programming contexts.
#include <iostream>
template <int p, int i>
class is_prime {
public:
enum { prim = ( (p % i) && is_prime<p, i - 1>::prim ) };
};
template <int p>
class is_prime<p, 1> {
public:
enum { prim = 1 };
};
template <int i>
class Prime_print {
// primary template for loop to print prime numbers
public:
Prime_print<i - 1> a;
enum { prim = is_prime<i, i - 1>::prim };
void f() {
a.f();
if (prim)
{
std::cout << "prime number:" << i << std::endl;
}
}
};
template<>
class Prime_print<1> { // full specialization to end the loop
public:
enum { prim = 0 };
void f() {}
};
CS 2363
COMPUTER NETWORKS
#ifndef LAST
#define LAST 18
#endif
int main()
{
Prime_print<LAST> a;
a.f();
}
1.1.5 Building blocks
Values
The 'variables' in TMP are not really variables since their values cannot be altered, but you can
have named values that you use rather like you would variables in ordinary programming. When
programming with types, named values are typedefs:
struct ValueHolder
{
typedef int value;
};
You can think of this as 'storing' the int type so that it can be accessed under the value name.
Integer values are usually stored as members in an enum:
struct ValueHolder
{
enum { value = 2 };
};
This again stores the value so that it can be accessed under the name value. Neither of these
examples is any use on its own, but they form the basis of most other TMP, so they are vital
patterns to be aware of.
Functions
A function maps one or more input parameters into an output value. The TMP analogue to this is
a template class:
template<int X, int Y>
struct Adder
{
enum { result = X + Y };
};
This is a function that adds its two parameters and stores the result in the result enum member.
You can call this at compile time with something like Adder<1, 2>::result, which will be
expanded at compile time and act exactly like a literal 3 in your program.
Branching
A conditional branch can be constructed by writing two alternative specialisations of a template
class. The compiler will choose the one that fits the types provided, and a value defined in the
instantiated class can then be accessed. For example, consider the following partial
specialisation:
emplate<typename X, typename Y>
struct SameType
CS 2363
COMPUTER NETWORKS
{
enum { result = 0 };
};
template<typename T>
struct SameType<T, T>
{
enum { result = 1 };
};
This tells us if the two types it is instantiated with are the same. This might not seem very useful,
but it can see through typedefs that might otherwise obscure whether types are the same, and it
can be used on template arguments in template code. You can use it like this:
if (SameType<SomeThirdPartyType, int>::result)
{
// ... Use some optimised code that can assume the type is an int
}
else
{
// ... Use defensive code that doesn't make any assumptions about the type
}
The above code isn't very idiomatic: since the types can be identified at compile-time, the
if() block will always have a trivial condition (it'll always resolve to either if (1) { ... } or if (0) {
... }). However, this does illustrate the kind of thing that can be achieved.
Recursion
Since you don't have mutable variables available when you're programming with
templates, it's impossible to iterate over a sequence of values. Tasks that might be achieved with
iteration in standard C++ have to be redefined in terms of recursion, i.e. a function that calls
itself. This usually takes the shape of a template class whose output value recursively refers to
itself, and one or more specialisations that give fixed values to prevent infinite recursion. You
can think of this as a combination of the function and conditional branch ideas described above.
Calculating factorials is naturally done recursively: 0!=1, and for n>0, n!=n(n1)!. In TMP, this
corresponds to a class template "factorial" whose general form uses the recurrence relation, and a
specialization of which terminates the recursion.
First, the general (unspecialized) template says that factorial<n>::value is given by
n*factorial<n-1>::value:
template <unsigned n>
struct factorial
{
enum { value = n * factorial<n-1>::value };
};
Next, the specialization for zero says that factorial<0>::value evaluates to 1:
template <>
struct factorial<0>
{
enum { value = 1 };
};
CS 2363
COMPUTER NETWORKS
And now some code that "calls" the factorial template at compile-time:
int main() {
// Because calculations are done at compile-time, they can be
// used for things such as array sizes.
int array[ factorial<7>::value ];
}
Observe that the factorial<N>::value member is expressed in terms of the factorial<N> template,
but this can't continue infinitely: each time it is evaluated, it calls itself with a progressively
smaller (but non-negative) number. This must eventually hit zero, at which point the
specialisation kicks in and evaluation doesn't recurse any further.
Example: Compile-time "If"
The following code defines a meta-function called "if_"; this is a class template that can be used
to choose between two types based on a compile-time constant, as demonstrated in main below:
template <bool Condition, typename TrueResult, typename FalseResult>
class if_;
template <typename TrueResult, typename FalseResult>
struct if_<true, TrueResult, FalseResult>
{
typedef TrueResult result;
};
template <typename TrueResult, typename FalseResult>
struct if_<false, TrueResult, FalseResult>
{
typedef FalseResult result;
};
int main()
{
typename if_<true, int, void*>::result number(3);
typename if_<false, int, void*>::result pointer(&number);
typedef typename if_<(sizeof(void *) > sizeof(uint32_t)), uint64_t, uint32_t>::result
integral_ptr_t;
integral_ptr_t converted_pointer = reinterpret_cast<integral_ptr_t>(pointer);
}
On line 18, we evaluate the if_ template with a true value, so the type used is the first of
the provided values. Thus the entire expression if_<true, int, void*>::result evaluates to int.
Similarly, on line 19 the template code evaluates to void *. These expressions act exactly the
same as if the types had been written as literal values in the source code.
Line 21 is where it starts to get clever: we define a type that depends on the value of a
platform-dependent sizeof expression. On platforms where pointers are either 32 or 64 bits, this
will choose the correct type at compile time without any modification, and without preprocessor
macros. Once the type has been chosen, it can then be used like any other type.
CS 2363
COMPUTER NETWORKS
2.GENERIC PROGRAMMING
In the simplest definition, generic programming is a style of computer programming in which
algorithms are written in terms of types to-be-specified-later that are then instantiated when
needed for specific types provided as parameters. This approach, pioneered by ML in 1973,[citation
needed]
permits writing common functions or types that differ only in the set of types on which
they operate when used, thus reducing duplication. Such software entities are known as generics
in Ada, Delphi, Eiffel, Java, C#, F#, Swift, and Visual Basic .NET; parametric polymorphism in
ML, Scala and Haskell (the Haskell community also uses the term "generic" for a related but
somewhat different concept); templates in C++ and D; and parameterized types in the influential
1994 book Design Patterns. The authors of Design Patterns note that this technique, especially
when combined with delegation, is very powerful but that "[dynamic], highly parameterized
software is harder to understand than more static software."[1]
Define software components with type parameters
A sorting algorithm has the same structure, regardless of the types being sorted
Stack primitives have the same semantics, regardless of the
objects stored on the stack.
Most common use: algorithms on containers: updating, iteration, search
C model: macros (textual substitution)
Ada model: generic units and instantiations
C++ model: templates
Construct
parameter
supplying parameter
2.1 Type parameter
The generic type declaration specifies the class of types for which an instance of the
generic will work:
type T is private;
-- any type with assignment (Non-limited)
type T is limited private; -- any type (no required operations)
type T is range <>;
-- any integer type (arithmetic operations)
type T is (<>);
-- any discrete type (enumeration or
-- integer)
type T is digits <>;
-- any floating-point type
CS 2363
COMPUTER NETWORKS
A generic sorting routine should apply to any array whose components are comparable,
i.e. for which an ordering predicate exists. This class includes more that the numeric
types:
generic
type T is private;
-- parameter
with function < (X, Y : T) return Boolean; -- parameter
type Arr is array (Integer range <>) of T; -- parameter
procedure Sort (A : in out Arr);
Supplying subprogram parameters
CS 2363
COMPUTER NETWORKS
The actual must have a matching signature, not necessarily the same name:
procedure Sort_Up
is new Sort (Integer, <, );
procedure Sort_Down is new Sort (Integer, >, );
type Employee is record .. end record;
function Senior (E1, E2 : Employee) return Boolean;
function Rank is new Sort (Employee, Senior, );
Value parameters
Useful to parametrize containers by size:
generic
type Elem is private;
-- type parameter
Size : Positive;
-- value parameter
package Queues is
type Queue is private;
procedure Enqueue (X : Elem; On : in out Queue);
procedure Dequeue (X : out Elem; From : in out Queue);
function Full (Q : Queue) return Boolean;
function Empty (Q : Queue) return Boolean;
private
type Contents is array (Natural range <>) of Elem;
type Queue is record
Front, Back: Natural;
C : Contents (0 .. Size);
end record;
end Queues
The Standard Template Library (STL), part of the C++ Standard Library, offers
collections of algorithms, containers, iterators, and other fundamental components, implemented
as templates, classes, and functions essential to extend functionality and standardization to C++.
STL main focus is to provide improvements implementation standardization with emphasis in
performance and correctness.
Instead of wondering if your array would ever need to hold 257 records or having
nightmares of string buffer overflows, you can enjoy vector and string that automatically extend
to contain more records or characters. For example, vector is just like an array, except that
vector's size can expand to hold more cells or shrink when fewer will suffice. One must keep in
mind that the STL does not conflict with OOP but in itself is not object oriented; In particular it
makes no use of runtime polymorphism (i.e., has no virtual functions).
The true power of the STL lies not in its container classes, but in the fact that it is a
framework, combining algorithms with data structures using indirection through iterators to
allow generic implementations of higher order algorithms to work efficiently on varied forms of
data. To give a simple example, the same std::copy function can be used to copy elements from
one array to another, or to copy the bytes of a file, or to copy the whitespace-separated words in
"text like this" into a container such as std::vector<std::string>.
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
{
if(left[5] < right[5])
{
return( right );
}
// else
return left ;
}
int* pick_array_with_biggest_fifth_element(int * left,int * right)
{
if(left[5] < right[5])
{
return( right );
}
// else
return left ;
}
int vector_demo(void)
{
cout << "vector demo" << endl;
vector<int> left(7);
vector<int> right(7);
left[5] = 7;
right[5] = 8;
cout << left[5] << endl;
cout << right[5] << endl;
vector<int> biggest(pick_vector_with_biggest_fifth_element( left, right ) );
cout << biggest[5] << endl;
return 0;
}
int array_demo(void)
{
cout << "array demo" << endl;
int left[7];
int right[7];
left[5] = 7;
right[5] = 8;
cout << left[5] << endl;
cout << right[5] << endl;
int * biggest =
pick_array_with_biggest_fifth_element( left, right );
cout << biggest[5] << endl;
CS 2363
COMPUTER NETWORKS
return 0;
}
int main(void)
{
vector_demo();
array_demo();
}
Member Functions
The vector class models the Container concept, which means it has begin(), end(), size(),
max_size(), empty(), and swap() methods.
informative
o vector::front - Returns reference to first element of vector.
o vector::back - Returns reference to last element of vector.
o vector::size - Returns number of elements in the vector.
o vector::empty - Returns true if vector has no elements.
standard operations
o vector::insert - Inserts elements into a vector (single & range), shifts later
elements up. Inefficient.
o vector::push_back - Appends (inserts) an element to the end of a vector,
allocating memory for it if necessary. Amortized O(1) time.
o vector::erase - Deletes elements from a vector (single & range), shifts later
elements down. Inefficient.
o vector::pop_back - Erases the last element of the vector, (possibly reducing
capacity - usually it isn't reduced, but this depends on particular STL
implementation). Amortized O(1) time.
o vector::clear - Erases all of the elements. Note however that if the data elements
are pointers to memory that was created dynamically (e.g., the new operator was
used), the memory will not be freed.
allocation/size modification
o vector::assign - Used to delete a origin vector and copies the specified elements to
an empty target vector.
o vector::reserve - Changes capacity (allocates more memory) of vector, if needed.
In many STL implementations capacity can only grow, and is never reduced.
o vector::capacity - Returns current capacity (allocated memory) of vector.
o vector::resize - Changes the vector size.
iteration
o vector::begin - Returns an iterator to start traversal of the vector.
o vector::end - Returns an iterator that points just beyond the end of the vector.
o vector::at - Returns a reference to the data element at the specified location in the
vector, with bounds checking.
vector<int> v;
for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it/* increment operand is used to move to
next element*/) {
cout << *it << endl;
}
CS 2363
COMPUTER NETWORKS
vector::Iterators
std::vector<T> provides Random Access Iterators; as with all containers, the primary access to
iterators is via begin() and end() member functions. These are overloaded for const- and nonconst containers, returning iterators of types std::vector<T>::const_iterator and
std::vector<T>::iterator respectively.
vector examples
/* Vector sort example */
#include <iostream>
#include <vector>
int main()
{
using namespace std;
cout << "Sorting STL vector, \"the easier array\"... " << endl;
cout << "Enter numbers, one per line. Press ctrl-D to quit." << endl;
vector<int> vec;
int tmp;
while (cin>>tmp) {
vec.push_back(tmp);
}
cout << "Sorted: " << endl;
sort(vec.begin(), vec.end());
int i = 0;
for (i=0; i<vec.size(); i++) {
cout << vec[i] << endl;;
}
return 0;
}
The call to sort above actually calls an instantiation of the function template std::sort,
which will work on any half-open range specified by two random access iterators.
If you like to make the code above more "STLish" you can write this program in the following
way:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
using namespace std;
cout << "Sorting STL vector, \"the easier array\"... " << endl;
CS 2363
COMPUTER NETWORKS
cout << "Enter numbers, one per line. Press ctrl-D to quit." << endl;
istream_iterator<int> first(cin);
istream_iterator<int> last;
vector<int> vec(first, last);
sort(vec.begin(), vec.end());
cout << "Sorted: " << endl;
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"));
return 0;
}
3.1.4 Linked lists
The STL provides a class template called list (part of the standard namespace (std::)) which
implements a non-intrusive doubly-linked list. Linked lists can insert or remove elements in the
middle in constant time, but do not have random access. One useful feature of std::list is that
references, pointers and iterators to items inserted into a list remain valid so long as that item
remains in the list.
list examples
/* List example - insertion in a list */
#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
void print_list(std::list<int> const& a_filled_list)
{
using namespace std;
ostream_iterator<int> out(cout, " ");
copy(a_filled_list.begin(), a_filled_list.end(), out);
}
int main()
{
std::list<int> my_list;
my_list.push_back(1);
my_list.push_back(10);
print_list(my_list); //print : 1 10
std::cout << std::endl;
my_list.push_front(45);
CS 2363
COMPUTER NETWORKS
print_list(my_list); //print : 45 1 10
return 0;
}
Associative Containers (key and value)
This type of container point to each element in the container with a key value, thus
simplifying searching containers for the programmer. Instead of iterating through an array or
vector element by element to find a specific one, you can simply ask for people["tero"]. Just like
vectors and other containers, associative containers can expand to hold any number of elements.
3.1.5 Maps and Multimaps
map and multimap are associative containers that manage key/value pairs as elements as seen
above. The elements of each container will sort automatically using the actual key for sorting
criterion. The difference between the two is that maps do not allow duplicates, whereas,
multimaps does.
map - unique keys
multimap - same key can be used many times
set - unique key is the value
multiset - key is the value, same key can be used many times
/* Map example - character distribution */
#include <iostream>
#include <map>
#include <string>
#include <cctype>
using namespace std;
int main()
{
/* Character counts are stored in a map, so that
* character is the key.
* Count of char a is chars['a']. */
map<char, long> chars;
cout << "chardist - Count character distributions" << endl;
cout << "Type some text. Press ctrl-D to quit." << endl;
char c;
while (cin.get(c)) {
// Upper A and lower a are considered the same
c=tolower(static_cast<unsigned char>(c));
chars[c]=chars[c]+1; // Could be written as ++chars[c];
}
cout << "Character distribution: " << endl;
string alphabet("abcdefghijklmnopqrstuvwxyz");
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
#include <vector>
#include <algorithm>
#include <iostream>
// Define the Functor for AccumulateSquareValues
template<typename T>
struct AccumulateSquareValues
{
AccumulateSquareValues() : sumOfSquares()
{
}
void operator()(const T& value)
{
sumOfSquares += value*value;
}
T Result() const
{
return sumOfSquares;
}
T sumOfSquares;
};
std::vector<int> intVec;
intVec.reserve(10);
for( int idx = 0; idx < 10; ++idx )
{
intVec.push_back(idx);
}
AccumulateSquareValues<int> sumOfSquare = std::for_each(intVec.begin(),
intVec.end(),
AccumulateSquareValues<int>() );
std::cout << "The sum of squares for 1-10 is " << sumOfSquare.Result() << std::endl;
// note: this problem can be solved in another, more clear way:
// int sum_of_squares = std::inner_product(intVec.begin(), intVec.end(), intVec.begin(), 0);
Algorithms
The STL also provides several useful algorithms, in the form of template functions, that are
provided to, with the help of the iterator concept, manipulate the STL containers (or derivations).
The STL algorithms aren't restricted to STL containers, for instance:
#include <algorithm>
int array[10] = { 2,3,4,5,6,7,1,9,8,0 };
int* begin = &array[0];
int* end = &array[0] + 10;
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
potential_credit_transfer(initial_credit_transfer)
{}
bool operator<(Account const& account) const { return credit < account.credit; }
int potential_credit() const { return credit + potential_credit_transfer; }
std::string const& owner() const { return owner_name; }
};
struct CompareAccountCredit
{
bool operator()(Account const& account1, Account const& account2) const
{ return account1 < account2; }
};
struct CompareAccountPotentialCredit
{
bool operator()(Account const& account1, Account const& account2) const
{ return account1.potential_credit() < account2.potential_credit(); }
};
int main()
{
Account account1("Dennis Ritchie", 1000, 250), account2("Steeve Jobs", 500, 10000),
result_comparison;
result_comparison = std::min(account1, account2, CompareAccountCredit());
std::cout << "min credit of account is : " + result_comparison.owner() << std::endl;
result_comparison = std::min(account1, account2, CompareAccountPotentialCredit());
std::cout << "min potential credit of account is : " + result_comparison.owner() <<
std::endl;
return 0;
}
3.1.8 Allocators
Allocators are used by the Standard C++ Library (and particularly by the STL) to allow
parameterization of memory allocation strategies.
The subject of allocators is somewhat obscure, and can safely be ignored by most
application software developers. All standard library constructs that allow for specification of an
allocator have a default allocator which is used if none is given by the user.
Custom allocators can be useful if the memory use of a piece of code is unusual in a way
that leads to performance problems if used with the general-purpose default allocator. There are
also other cases in which the default allocator is inappropriate, such as when using standard
containers within an implementation of replacements for global operators new and delete.
CS 2363
COMPUTER NETWORKS
4. INHERITANCE
Introduction
Inheritance
Single Inheritance
Class inherits from one base class
Multiple Inheritance
Class inherits from multiple base classes
Three types of inheritance:
public: Derived objects are accessible by the base class objects (focus of
this chapter)
private: Derived objects are inaccessible by the base class
protected: Derived classes and friends can access protected members of
the base class
Base and Derived Classes
Base class
Derived classes
Student
Shape
Loan
Fig. 19.1
GraduateStudent
UndergraduateStudent
Circle
Triangle
Rectangle
CarLoan
HomeImprovementLoan
ClassesMortgageLoan
FacultyMember
StaffMember
CheckingAccount
SavingsAccount
CS 2363
COMPUTER NETWORKS
public
Employee
...
};
Class CommissionWorker inherits from class Employee
friend functions not inherited
private members of base class not accessible from derived class
Protected Members
protected inheritance
Intermediate level of protection between public and private inheritance
Derived-class members can refer to public and protected members of the base
class simply by using the member names
Note that protected data breaks encapsulation
Example: Base Class
class base {
int x;
public:
void setx(int n) { x = n; }
CS 2363
COMPUTER NETWORKS
The keyword public tells the compiler that base will be inherited such that:
y all public members of the base class will also be public members of derived.
However, all private elements of base will remain private to it and are not directly accessible by
derived.
Example: main()
int main() {
derived ob;
ob.setx(10);
ob.sety(20);
ob.showx();
ob.showy();
}
4.1.2 Types
Single Level Inheritance
Multiple Inheritance
Hierarchical inheritance
Multilevel
Inheritance
Hybrid Inheritance.
Single Level Inheritance
#include <iostream.h>
Class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
Class D: public B
{
int c;
public:
void mul();
void display();
};
Void B :: get_ab()
{ a=5;b=10; }
Int B :: get_a()
{ return a;}
Void B :: show_a()
CS 2363
{ count<< a=<<a<<
\
n ;}
Void D :: mul()
{ c=b*
get_a();}
Void D :: display()
{
Count<< a=<<get_a()
Count<< b=<<b
Count<< c=<<c
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
return 0
Multiple Inheritance
#include <iostream.h>
Class M
{
Protected:
Int m;
Public :
Void get_m(int);
};
Class N
{
Protected:
Int n;
Public :
Void get_n(int);
};
Class P :public M,public N
{
Public :
Void display();
};
COMPUTER NETWORKS
CS 2363
Void M :: get_m(int x)
{
M=x;
}
Void N::get_n(int y)
{
N=y;
}
Void P:: dis play()
{
Count<<m=<<m<<
\
n;
Count<<n=<<n<<
\
n;
Count<<m*n=<<m*n<<
\
n;
}
int main()
{
P p1;
P1.get_m(10);
P1.get_n(20);
P1.display();
Return 0
Hierarchical inheritance
class first
{
int x=10,y=20;
void display()
{
System.out.println("This is the method in
class one");
System.out.println("Value of X= "+x);
System.out.println("Value of Y= "+y);
}
}
class two extends first
{
void add()
{
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
super();
System.out.println(
"Constructor of Class C has been called");
}
}
class Constructor_Call
{
public static void main(String[] args)
{
System.out.println("
-----Welcome to Constructor call Demo
-----")
C objc = new C();
Hybrid Inheritance
class stud
{
Protected:
int rno;
Public:
Void getno(int n)
{
Rno=n;
}
Void display_rno()
{
Cout<<Roll_no=<<rno<<
\
n
;
}
};
Class test: Public stud
{
Protected:
Int sub1,sub2;
Public:
Void get_mark(int m1,int m2)
{
Sub1=m1;
Sub2=m2;
}
Void display_mark()
COMPUTER NETWORKS
CS 2363
{ Cout<<sub1<<sub1<<
\
n; Cout<<sub2<<
sub2<<
\
n;
}
};
Class sports
{
Protected:
Float score;
Public :
Void get_score(float s)
{ Score=s;
}
Void put_score()
{ Cout<<Sort :<<score<<
\
n;
}
};
Class result: public test ,public sports
{
Float total;
Public:
Void display();
};
Void result::display()
{
Total=sub1+sub2+score;
display_rno();
display_mark();
put_score();
cout<< total score:<<total<<
\
n;
}
int main()
{ Result s r1;
r1. getno(123);
r1. get_mark(60,80)
r1.get_score(6);
r1.display()
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
5. EXCEPTION HANDLING
Exception handling is a construct designed to handle the occurrence of exceptions, that is
special conditions that changes the normal flow of program execution. Since when designing a
programming task (a class or even a function), one cannot always assume that application/task
will run or be completed correctly (exit with the result it was intended to). It may be the case that
it will be just inappropriate for that given task to report an error message (return an error code) or
just exit. To handle these types of cases, C++ supports the use of language constructs to separate
error handling and reporting code from ordinary code, that is, constructs that can deal with these
exceptions (errors and abnormalities) and so we call this global approach that adds uniformity to
program design the exception handling.
An exception is said to be thrown at the place where some error or abnormal condition is
detected. The throwing will cause the normal program flow to be aborted, in a raised exception.
An exception is thrown programmatic, the programmer specifies the conditions of a throw.
In handled exceptions, execution of the program will resume at a designated block of
code, called a catch block, which encloses the point of throwing in terms of program execution.
The catch block can be, and usually is, located in a different function/method than the point of
throwing. In this way, C++ supports non-local error handling. Along with altering the program
flow, throwing of an exception passes an object to the catch block. This object can provide data
that is necessary for the handling code to decide in which way it should react on the exception.
Consider this next code example of a try and catch block combination for clarification:
void AFunction()
{
// This function does not return normally,
// instead execution will resume at a catch block.
// The thrown object is in this case of the type char const*,
// i.e. it is a C-style string. More usually, exception
// objects are of class type.
throw "This is an exception!";
}
void AnotherFunction()
{
// To catch exceptions, you first have to introduce
// a try block via " try { ... } ". Then multiple catch
// blocks can follow the try block.
// " try { ... } catch(type 1) { ... } catch(type 2) { ... }"
try
{
AFunction();
// Because the function throws an exception,
// the rest of the code in this block will not
// be executed
}
catch(char const* pch) // This catch block
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
iResult = iTmpResult;
}
return 0;
}
Stack unwinding
Consider the following code
void g()
{
throw std::exception();
}
void f()
{
std::string str = "Hello"; // This string is newly allocated
g();
}
int main()
{
try
{
f();
}
catch(...)
{}
}
The flow of the program:
main() calls f()
f() creates a local variable named str
str constructor allocates a memory chunk to hold the string "Hello"
f() calls g()
g()throws an exception
f() does not catch the exception.
Because the exception was not caught, we now need to exit f() in a clean fashion.
At this point, all the destructors of local variables previous to the throw
are calledThis is called 'stack unwinding'.
The destructor of str is called, which releases the memory occupied by it.
As you can see, the mechanism of 'stack unwinding' is essential to prevent resource
leakswithout it, str would never be destroyed, and the memory it used would be lost
until the end of the program (even until the next loss of power, or cold boot depending on
the Operative System memory management).
main() catches the exception
The program continues.
The 'stack unwinding' guarantees destructors of local variables (stack variables) will be
called when we leave its scope.
CS 2363
COMPUTER NETWORKS
Throwing objects
There are several ways to throw an exception object.
Throw a pointer to the object:
void foo()
{
throw new MyApplicationException();
}
void bar()
{
try
{
foo();
}
catch(MyApplicationException* e)
{
// Handle exception
}
}
But now, who is responsible to delete the exception? The handler? This makes code
uglier. There must be a better way!
How about this:
void foo()
{
throw MyApplicationException();
}
void bar()
{
try
{
foo();
}
catch(MyApplicationException e)
{
// Handle exception
}
}
But now, the catch handler that catches the exception, does it by value, meaning that a
copy constructor is called. This can cause the program to crash if the exception caught was a
bad_alloc caused by insufficient memory. In such a situation, seemingly safe code that is
assumed to handle memory allocation problems results in the program crashing with a failure of
the exception handler. Moreover, catching by value may cause the copy to have different
behavior because of object slicing.
The correct approach is:
void foo()
{
CS 2363
COMPUTER NETWORKS
throw MyApplicationException();
}
void bar()
{
try
{
foo();
}
catch(MyApplicationException const& e)
{
// Handle exception
}
}
This method has all the advantagesthe compiler is responsible for destroying the
object, and no copying is done at catch time!
The conclusion is that exceptions should be thrown by value, and caught by (usually const)
reference.
5.1.1 Constructors and destructors
When an exception is thrown from a constructor, the object is not considered instantiated, and
therefore its destructor will not be called. But all destructors of already successfully constructed
base and member objects of the same master object will be called. Destructors of not yet
constructed base or member objects of the same master object will not be executed. Example:
class A : public B, public C
{
public:
D sD;
E sE;
A(void)
:B(), C(), sD(), sE()
{
}
};
Let's assume the constructor of base class C throws. Then the order of execution is:
B
C (throws)
~B
Let's assume the constructor of member object sE throws. Then the order of execution is:
B
C
sD
sE (throws)
~sD
~C
~B
CS 2363
COMPUTER NETWORKS
Thus if some constructor is executed, one can rely on that all other constructors of the same
master object executed before, were successful. This enables one, to use an already constructed
member or base object as an argument for the constructor of one of the following member or
base objects of the same master object.
What happens when we allocate this object with new?
Memory for the object is allocated
The object's constructor throws an exception
o The object was not instantiated due to the exception
The memory occupied by the object is deleted
The exception is propagated, until it is caught
The main purpose of throwing an exception from a constructor is to inform the program/user that
the creation and initialization of the object did not finish correctly. This is a very clean way of
providing this important information, as constructors do not return a separate value containing
some error code (as an initialization function might).
In contrast, it is strongly recommended not to throw exceptions inside a destructor. It is
important to note when a destructor is called:
as part of a normal deallocation (exit from a scope, delete)
as part of a stack unwinding that handles a previously thrown exception.
In the former case, throwing an exception inside a destructor can simply cause memory leaks due
to incorrectly deallocated object. In the latter, the code must be more clever. If an exception was
thrown as part of the stack unwinding caused by another exception, there is no way to choose
which exception to handle first. This is interpreted as a failure of the exception handling
mechanism and that causes the program to call the function terminate.
To address this problem, it is possible to test if the destructor was called as part of an exception
handling process. To this end, one should use the standard library function uncaught_exception,
which returns true if an exception has been thrown, but hasn't been caught yet. All code executed
in such a situation must not throw another exception.
Situations where such careful coding is necessary are extremely rare. It is far safer and
easier to debug if the code was written in such a way that destructors did not throw exceptions at
all.
Writing exception safe code
Exception safety
A piece of code is said to be exception-safe, if run-time failures within the code will not produce
ill effects, such as memory leaks, garbled stored data, or invalid output. Exception-safe code
must satisfy invariants placed on the code even if exceptions occur. There are several levels of
exception safety:
1. Failure transparency, also known as the no throw guarantee: Operations are guaranteed to
succeed and satisfy all requirements even in presence of exceptional situations. If an
exception occurs, it will not throw the exception further up. (Best level of exception
safety.)
2. Commit or rollback semantics, also known as strong exception safety or no-change
guarantee: Operations can fail, but failed operations are guaranteed to have no side
effects so all data retain original values.
3. Basic exception safety: Partial execution of failed operations can cause side effects, but
invariants on the state are preserved. Any stored data will contain valid values even if
data has different values now from before the exception.
CS 2363
COMPUTER NETWORKS
4. Minimal exception safety also known as no-leak guarantee: Partial execution of failed
operations may store invalid data but will not cause a crash, and no resources get leaked.
5. No exception safety: No guarantees are made. (Worst level of exception safety)
5.1.2 Partial handling
Consider the following case:
void g()
{
throw "Exception";
}
void f()
{
int* pI = new int(0);
g();
delete pI;
}
int main()
{
f();
return 0;
}
Can you see the problem in this code? If g() throws an exception, the variable pI is never deleted
and we have a memory leak.
To prevent the memory leak, f() must catch the exception, and delete pI. But f() can't handle the
exception, it doesn't know how!
What is the solution then? f() shall catch the exception, and then re-throw it:
void g()
{
throw "Exception";
}
void f()
{
int* pI = new int(0);
try
{
g();
}
catch (...)
{
delete pI;
throw; // This empty throw re-throws the exception we caught
// An empty throw can only exist in a catch block
}
CS 2363
COMPUTER NETWORKS
delete pI;
}
int main()
{
f();
return 0;
}
There's a better way though; using RAII classes to avoid the need to use exception handling.
Guards
If you plan to use exceptions in your code, you must always try to write your code in an
exception safe manner. Let's see some of the problems that can occur:
Consider the following code:
void g()
{
throw std::exception();
}
void f()
{
int* pI = new int(2);
*pI = 3;
g();
// Oops, if an exception is thrown, pI is never deleted
// and we have a memory leak
delete pI;
}
int main()
{
try
{
f();
}
catch(...)
{}
return 0;
}
Can you see the problem in this code? When an exception is thrown, we will never run the line
that deletes pI!
What's the solution to this? Earlier we saw a solution based on f() ability to catch and re-throw.
But there is a neater solution using the 'stack unwinding' mechanism. But 'stack unwinding' only
applies to destructors for objects, so how can we use it?
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
o
An example:
float divide(float fNumerator, float fDenominator)
{
if (fDenominator == 0.0)
{
throw DivisionByZeroException();
}
return fNumerator/fDenominator;
}
enum MathOperators {DIVISION, PRODUCT};
float operate(int iAction, float fArgLeft, float fArgRight)
{
if (iAction == DIVISION)
{
return divide(fArgLeft, fArgRight);
}
else if (iAction == PRODUCT))
{
// call the product function
// ...
}
// No match for the action! iAction is an invalid agument
throw InvalidArgumentException();
}
int main(int iArgc, char* a_pchArgv[])
{
try
{
operate(atoi(a_pchArgv[0]), atof(a_pchArgv[1]), atof(a_pchArgv[2]));
}
catch(MathematicalException& )
{
// Handle Error
}
catch(MyApplicationException& )
{
// This will catch in InvalidArgumentException too.
// Display help to the user, and explain about the arguments.
}
CS 2363
COMPUTER NETWORKS
return 0;
}
5.1.3 Exception specifications
The range of exceptions that can be thrown by a function are an important part of that function's
public interface. Without this information, you would have to assume that any exception could
occur when calling any function, and consequently write code that was extremely defensive.
Knowing the list of exceptions that can be thrown, you can simplify your code since it doesn't
need to handle every case.
This exception information is specifically part of the public interface. Users of a class don't need
to know anything about the way it is implemented, but they do need to know about the
exceptions that can be thrown, just as they need to know the number and type of parameters to a
member function. One way of providing this information to clients of a library is via code
documentation, but this needs to be manually updated very carefully. Incorrect exception
information is worse than none at all, since you may end up writing code that is less exceptionsafe than you intended to.
C++ provides another way of recording the exception interface, by means of exception
specifications. An exception specification is parsed by the compiler, which provides a measure
of automated checking. An exception specification can be applied to any function, and looks like
this:
double divide(double dNumerator, double dDenominator) throw (DivideByZeroException);
You can specify that a function cannot throw any exceptions by using an empty exception
specification:
void safeFunction(int iFoo) throw();
Shortcomings of exception specifications
C++ does not programmatically enforce exception specifications at compile time. For example,
the following code is legal:
void DubiousFunction(int iFoo) throw()
{
if (iFoo < 0)
{
throw RangeException();
}
}
Rather than checking exception specifications at compile time, C++ checks them at run
time, which means that you might not realize that you have an inaccurate exception specification
until testing or, if you are unlucky, when the code is already in production.
If an exception is thrown at run time that propagates out of a function that doesn't allow
the exception in its exception specification, the exception will not propagate any further and
instead, the function RangeException() will be called. The RangeException() function doesn't
return, but can throw a different type of exception that may (or may not) satisfy the exception
specification and allow exception handling to carry on normally. If this still doesn't recover the
situation, the program will be terminated.
Many people regard the behavior of attempting to translate exceptions at run time to be
worse than simply allowing the exception to propagate up the stack to a caller who may be able
to handle it. The fact that the exception specification has been violated does not mean that the
CS 2363
COMPUTER NETWORKS
caller can't handle the situation, only that the author of the code didn't expect it. Often there will
be a catch (...) block somewhere on the stack that can deal with any exception.
Noexcept specifiers
5.1.4 Run-Time Type Information (RTTI)
RTTI refers to the ability of the system to report on the dynamic type of an object and to
provide information about that type at runtime (as opposed to at compile time), when utilized
consistently can be a powerful tool to ease the work of the programmer in managing resources.
dynamic_cast
Consider what you have already learned about the dynamic_cast keyword and let's say that we
have the following class hierarchy:
class Interface
{
public:
virtual void GenericOp() = 0;// pure virtual function
};
class SpecificClass : public Interface
{
public:
virtual void GenericOp();
virtual void SpecificOp();
};
Let's say that we also have a pointer of type Interface*, like so:
Interface* ptr_interface;
Supposing that a situation emerges that we are forced to presume but have no guarantee that the
pointer points to an object of type SpecificClass and we would like to call the member
SpecificOp() of that class. To dynamically convert to a derived type we can use dynamic_cast,
like so:
SpecificClass* ptr_specific = dynamic_cast<SpecificClass*>(ptr_interface);
if( ptr_specific ){
// our suspicions are confirmed -- it really was a SpecificClass
ptr_specific->SpecificOp();
}else{
// our suspicions were incorrect -- it is definitely not a SpecificClass.
// The ptr_interface points to an instance of some other child class of the base InterfaceClass.
ptr_interface->GenericOp();
};
With dynamic_cast, the program converts the base class pointer to a derived class pointer and
allows the derived class members to be called. Be very careful, however: if the pointer that you
are trying to cast is not of the correct type, then dynamic_cast will return a null pointer.
We can also use dynamic_cast with references.
SpecificClass& ref_specific = dynamic_cast<SpecificClass&>(ref_interface);
This works almost in the same way as pointers. However, if the real type of the object being cast
is not correct then dynamic_cast will not return null (there's no such thing as a null reference).
Instead, it will throw a std::bad_cast exception.
CS 2363
COMPUTER NETWORKS
typeid
Syntax
typeid( object );
The typeid operator, used to determine the class of an object at runtime. It returns a reference to a
std::type_info object, which exists until the end of the program, that describes the "object". If the
"object" is a dereferenced null pointer, then the operation will throw a std::bad_typeid exception.
Objects of class std::bad_typeid are derived from std::exception, and thrown by typeid and
others.
The use of typeid is often preferred over dynamic_cast<class_type> in situations where just the
class information is needed, because typeid, applied on a type or non de-referenced value is a
constant-time procedure, whereas dynamic_cast must traverse the class derivation lattice of its
argument at runtime. However, you should never rely on the exact content, like for example
returned by std::type_info::name(), as this is implementation specific with respect to the compile.
It is generally only useful to use typeid on the dereference of a pointer or reference (i.e.
typeid(*ptr) or typeid(ref)) to an object of polymorphic class type (a class with at least one
virtual member function). This is because these are the only expressions that are associated with
run-time type information. The type of any other expression is statically known at compile time.
Example
#include <iostream>
#include <typeinfo> //for 'typeid' to work
class Person {
public:
// ... Person members ...
virtual ~Person() {}
};
class Employee : public Person {
// ... Employee members ...
};
int main () {
Person person;
Employee employee;
Person *ptr = &employee;
// The string returned by typeid::name is implementation-defined
std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compiletime)
std::cout << typeid(ptr).name() << std::endl;
// Person * (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl;
// Employee (looked up dynamically at runtime
//
because it is the dereference of a
//
pointer to a polymorphic class)
}
Output (exact output varies by system):
CS 2363
COMPUTER NETWORKS
Person
Employee
Person*
Employee
CS 2363
COMPUTER NETWORKS
1. Templates
1. Template Meta-Programming (TMP)
2. Standard Template Library (STL)
3. Smart Pointers
4. Exception Handling
5. Run-Time Type Information (RTTI)
Retrieved
from
"http://en.wikibooks.org/w/index.php?title=C%2B%2B_Programming/Chapter_Advanced_Featu
res_Print_version&oldid=1790166"
This page was last modified on 8 May 2010, at 03:26.
Text is available under the Creative Commons Attribution-ShareAlike License.;
additional terms may apply. By using this site, you agree to the Terms of Use and Privacy
Policy.
CS 2363
COMPUTER NETWORKS
Declaring a variable of a class type creates an object. You can have many variables of the
same type (class).
Instantiation
Once an object of a certain class is instantiated, a new memory location is created for it to
store its data members and code
You can instantiate many objects from a class type.
Ex) Circle c; Circle *
6.1.2 Implementing class methods
CS 2363
COMPUTER NETWORKS
CS 2363
Type
COMPUTER NETWORKS
Size
Minimum Value
Maximum Value
in Bytes
float
4
1.4E-45
3.4028235E38
double 8
4.9E-324
1.7976931348623157E308
Example declarations:
float salesTax;
double interestRate;
double paycheck, sumSalaries;
char Data Type
One Unicode character (16 bits - 2 bytes)
Type Size
Minimum Value
Maximum Value
in Bytes
char
2
character
character
encoded as 0
encoded as FFFF
Example declarations:
char finalGrade;
char newline, tab, doubleQuotes;
boolean Data Type
Two values only:
true
false
Used for decision making or as "flag" variables
Example declarations:
boolean isEmpty;
boolean passed, failed;
Assigning Values to Variables
Assignment operator =
Value on the right of the operator is assigned to the variable on the left
Value on the right can be a literal (text representing a specific value), another
variable, or an expression (explained later)
Syntax:
dataType variableName = initialValue;
Or
dataType variable1 = initialValue1,
variable2 = initialValue2, ;
Literals
int, short, byte
Optional initial sign (+ or -) followed by digits 0 9 in any combination.
long
Optional initial sign (+ or -) followed by digits 09 in any combination, terminated with an L or
l.
***Use the capital L because the lowercase l can be confused with the number 1.
Floating-Point Literals
float
Optional initial sign (+ or -) followed by a floating-point number in fixed or scientific format,
terminated by an F or f.
CS 2363
COMPUTER NETWORKS
double
Optional initial sign (+ or -) followed by a floating-point number in fixed or scientific format.
Assigning the Values of Other Variables
Syntax:
dataType variable2 = variable1;
Rules:
1. variable1 needs to be defined before this statement appears in the source code
2. variable1 and variable2 need to be compatible data types; in other words, the precision of
variable1 must be lower than or equal to that of variable2.
Compatible Data Types
Any type in right column can be assigned to type in left column:
Data Type
Compatible Data Types
byte
byte
short
byte, short
int
byte, short, int, char
long
byte, short, int, long, char
float
float, byte, short, int, long, char
double
float, double, byte, short, int, long, char
boolean
boolean
char
char
3. ARRAYS
Declaring an Array Variable
CS 2363
COMPUTER NETWORKS
System.out.println(Array Length=+primes.length);
.
SAMPLE RUN:
Enter a Size for Array:
4
Array Length=4
Default Initialization
When array is created, array elements are initialized
Numeric values (int, double, etc.) to 0
Boolean values to false
Char values to \u0000 (unicode for blank character)
Class types to null
Accessing Array Elements
Index of an array is defined as
Positive int, byte or short values
Expression that results into these types
Any other types used for index will give error
long, double, etc.
Incase Expression results in long, then type cast to int
Indexing starts from 0 and ends at N-1
primes[2]=0;
int k = primes[2];
Validating Indexes
JAVA checks whether the index values are valid at runtime
If index is negative or greater than the size of the array then an
IndexOutOfBoundException will be thrown
Program will normally be terminated unless handled in the try {} catch {}
long[] primes = new long[20];
primes[25]=33;
.
Runtime Error:
Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 25
at MorePrimes.main(MorePrimes.java:6)
3.1 Initializing Arrays
Initialize and specify size of array while declaring an array variable
int[] primes={2,3,5,7,11,13,17}; //7 elements
You can initialize array with an existing array
int[] even={2,4,6,8,10};
int[] value=even;
One array but two array variables!
Both array variables refer to the same array
Array can be accessed through either variable name
Array Length
Refer to array length using length
A data member of array object
array_variable_name.length
CS 2363
COMPUTER NETWORKS
.
Sample Code:
long[] primes = new long[20];
System.out.println(primes.length);
Output: 20
Sample Program
class MinAlgorithm
{
public static void main ( String[] args )
{
int[] array = { -20, 19, 1, 5, -1, 27, 19, 5 } ;
int min=array[0]; // initialize the current minimum
for ( int index=0; index < array.length; index++ )
if ( array[ index ] < min )
min = array[ index ] ;
System.out.println("The minimum of this array is: " + min );
}
}
3.2 Arrays of Arrays
Two-Dimensional arrays
float[][] temperature=new float[10][365];
10 arrays each having 365 elements
First index: specifies array (row)
Second Index: specifies element in that array (column)
In JAVA float is 4 bytes, total Size=4*10*365=14,600 bytes
3.3 Multidimensional Arrays
A farmer has 10 farms of beans each in 5 countries, and each farm has 30 fields!
Three-dimensional array
long[][][] beans=new long[5][10][30];
//beans[country][farm][fields]
4.OPERATORS
The Assignment Operator and Expressions
Arithmetic Operators
Operator Precedence
Integer Division and Modulus
Division by Zero
Mixed-Type Arithmetic and Type Casting
Shortcut Operators
4.1 Assignment Operator
Syntax:
target = expression;
expression: operators and operands that evaluate to a single value
--value is then assigned to target
CS 2363
COMPUTER NETWORKS
Operation
addition
subtraction
multiplication
division
Order of
evaluation
Operation
()
left - right
* / %
left - right
multiplication, division,
modulus
CS 2363
COMPUTER NETWORKS
+ -
left - right
addition, subtraction
right - left
assignment
Example
Equivalent
+=
a += 3;
a = a + 3;
-=
a -= 10;
a = a - 10;
*=
a *= 4;
a = a * 4;
/=
a /= 7;
a = a / 7;
%=
a %= 10;
a = a % 10;
5.CONTROL STATEMENTS
5.1 Types
if else
switch
while
do while
CS 2363
COMPUTER NETWORKS
for
break
continue
return
Labeled break, continue
if-else
if(conditional_statement){
statement to be executed if conditions becomes true
}else{
statements to be executed if the above condition becomes false
}
Switch
switch(byte/short/int){
case expression:
statements
case expression:
statements
default:
statement
}
while loop
while(condition_statementtrue){
Statements to be executed when the condition becomes true and execute them repeatedly until
condition becomes false.
}
E.g.
int x =2;
while(x>5){
system.out.println(value of x:+x);
x++;
}
do while loop
do{
statements to be executed at least once without looking at the condition.
The statements will be exeucted until the condition becomes true.
}while(condition_statement);
for loop
for(initialization; condition; increment/decrement){
statements to be executed until the condition becomes false
}
E.g:
for(int x=0; x<10;x++){
System.out.println(value of x:+x);
}
Break
CS 2363
COMPUTER NETWORKS
Break is used in the loops and when executed, the control of the execution will come out
of the loop.
for(int i=0;i<50;i++){
if(i%13==0){
break;
}
System.out.println(Value of i:+i);
}
Continue
Continue makes the loop to skip the current execution and continues with the next
iteration.
for(int i=0;i<50;i++){
if(i%13==0){
continue;
}
System.out.println(Value of i:+i);
}
Return
return statement can be used to cause execution to branch back to the caller of the method
Labeled break,continue
Labeled break and continue statements will break or continue from the loop that is
mentioned.
Used in nested loops.
Primitive data types
char, byte, short, int, long, float, double, boolean
Building blocks for more complicated types
All variables must have a type before being used
Strongly typed language
Primitive types portable, unlike C and C++
In C/C++, write different versions of programs
Data types not guaranteed to be identical
ints may be 2 or 4 bytes, depending on system
WORA - Write once, run anywhere
Default values
boolean gets false, all other types are 0
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
class Dog {
String name;
int age;
Dog(String n, int age) {
name = n;
this.age = age;
}
void bark() {
System.out.println("Woof!");
}
void wakeTheNeighbors( ) {
int i = 50;
while (i > 0) {
bark( );
i = i 1;
}
}
public static void main(String[ ] args) {
Dog fido = new Dog("Fido", 5);
CS 2363
COMPUTER NETWORKS
fido.wakeTheNeighbors();
}
} // ends the class
6.5Method Definitions
Method definition format
return-value-type
method-name(
parameter-list
{
declarations
and
}
Method-name: any valid identifier
Return-value-type: data type of the result
void - method returns nothing
Can return at most one value
Parameter-list: comma separated list, declares parameters
Method call must have proper number and type of parameters
Declarations and statements: method body (block)
Variables can be declared inside blocks (can be nested)
Method cannot be defined inside another function
Program control
When method call encountered
Control transferred from point of invocation to method
Returning control
If nothing returned: return;
Or until reaches right brace
If value returned: return expression;
Returns the value of expression
Example user-defined method:
public
int
square(
int
y
{
return y * y
}
Calling methods
Three ways
Method name and arguments
Can be used by methods of same class
square( 2 );
Dot operator - used with objects
g.drawLine( x1, y1, x2, y2 );
Dot operator - used with static methods of classes
Integer.parseInt( myString );
More Chapter 26
More GUI components
Content Pane - on-screen display area
)
statements
CS 2363
COMPUTER NETWORKS
7.INHERITANCE BASICS
1. Reusability is achieved by INHERITANCE
2. Java classes Can be Reused by extending a class. Extending an existing class is nothing
but reusing properties of the existing classes.
3. The class whose properties are extended is known as super or base or parent class.
4. The class which extends the properties of super class is known as sub or derived or child
class
5. A class can either extends another class or can implement an interface
class B extends A { .. }
A super class
B sub class
class B implements A { .. }
A interface
B sub class
Various Forms of Inheritance
7.1Defining a Subclass
Syntax :
class <subclass name> extends <superclass name>
{
variable declarations;
method declarations;
}
Extends keyword signifies that properties of the super class are extended to sub class
Sub class will not inherit private members of super class
Access Control
CS 2363
COMPUTER NETWORKS
Access Modifiers
Access Location
public
protected
friendly
private
Same Class
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Other Classes in
Same package
Yes
Yes
Yes
No
other
Yes
Yes
No
No
Non-subclasses in other
packages
Yes
No
No
No
Subclasses
packages
in
Inheritance Basics
When super class has a Unparametrized constructor
class A
{
A()
{
System.out.println("This is constructor of class A");
}
} // End of class A
class B extends A
{
B()
{
super();
CS 2363
COMPUTER NETWORKS
CS 2363
}
}
class B extends A
{
B()
{
System.out.println("This is class B");
}
}
class inherit2
{
public static void main(String args[])
{
B b1 = new B();
}
}
/*
E:\Java>javac xyz1.java
xyz1.java:12: A() has private access in A
{
^
1 error
*/
class A
{
private A()
{
System.out.println("This is class A");
}
A()
{
System.out.println("This is class A");
}
}
class B extends A
{
B()
{
System.out.println("This is class B");
}
}
class inherit2
{
public static void main(String args[])
{
B b1 = new B();
COMPUTER NETWORKS
CS 2363
}}
/*
E:\Java>javac xyz2.java
xyz2.java:7: A() is already defined in A
A()
^
xyz2.java:16: A() has private access in A
{
^
2 errors
*/
When Super class has a parametrized constructor.
class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is constructor of class A");
}
}
class B extends A
{
private int b;
private double c;
B(int b,double c)
{
this.b=b;
this.c=c;
System.out.println("This is constructor of class B");
}
}
B b1 = new B(10,8.6);
D:\java\bin>javac inhtest.java
inhtest.java:15: cannot find symbol
symbol : constructor A()
location: class A
{
^
1 errors
class A
{
private int a;
protected String name;
A(int a, String n)
{
COMPUTER NETWORKS
CS 2363
this.a = a;
this.name = n;
}
void print()
{
System.out.println("a="+a);
}
}
class B extends A
{
int b;
double c;
B(int a,String n,int b,double c)
{
super(a,n);
this.b=b;
this.c =c;
}
void show()
{
//System.out.println("a="+a);
print();
System.out.println("name="+name);
System.out.println("b="+b);
System.out.println("c="+c);
}
} class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is constructor of class A");
}
void show()
{
System.out.println("a="+a);
}
void display()
{
System.out.println("hello This is Display in A");
}
}
class B extends A
{
private int b;
COMPUTER NETWORKS
CS 2363
private double c;
B(int a,int b,double c)
{
super(a);
this.b=b;
this.c=c;
System.out.println("This is constructor of class B");
}
void show()
{
super.show();
System.out.println("b="+b);
System.out.println("c="+c);
display();
}
}
class inhtest1
{
public static void main(String args[])
{
B b1 = new B(10,8,4.5);
b1.show();
}
}
/* OutPut
D:\java\bin>java inhtest1
This is constructor of class A
This is constructor of class B
a=10
b=8
c=4.5
hello This is Display in A
*/
7.2 Types
Single inheritance
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Multiple Inheritance
Multiple Inheritance refers to the concept of one class extending (Or inherits) more
than one base class. The inheritance we learnt earlier had the concept of one base class or parent.
The problem with multiple inheritance is that the derived class will have to manage the
dependency on two base classes.
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit
from a derived class, thereby making this derived class the base class for the new class. As you
can see in below flow diagram C is subclass or child class of B and B is a child class of A. For
more details and example refer Multilevel inheritance in Java.
CS 2363
COMPUTER NETWORKS
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}
4) Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example
class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D. Read
More at Hierarchical Inheritance in java with example program.
Hybrid Inheritance
In simple terms you can say that Hybrid inheritance is a combination of Single and
Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can be
achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you
heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.
Read the full article here hybrid inheritance in java with example program.
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
public S1( )
{
System.out.println("This is Class S1");
}
}
Name the source file as S1.java and compile it and store the S1.class file in mypackageA
directory
Make class S2 belongs to mypackageA
package mypackage.mypackageA;
public class S2
{
public S2( )
{
System.out.println("This is Class S2");
}
}
Name the source file as S2.java and compile it and store the S2.class file in mypackageA
directory
Make class A belongs to IJK
package mypackage.mypackageB.IJK;
public class A
{
public A( )
{
System.out.println("This is Class A in IJK");
}
}
Name the source file as A.java and compile it and store the A.class file in IJK directory
1.1 Importing the Package
import statement allows the importing of package
Library packages are automatically imported irrespective of the location of compiling and
executing program
JRE looks at two places for user created packages
(i) Under the current working directory
(ii) At the location specified by CLASSPATH
environment variable
Most ideal location for compiling/executing a program is immediately above the package
structure.
Example
importing
import mypackage.mypackageA.ABC
import mypackage.mypackageA.ABC.*;
class packagetest
{
public static void main(String args[])
{
B b1 = new B();
CS 2363
COMPUTER NETWORKS
C c1 = new C();
}
}
import mypackage.mypackageA.ABC.*;
Import mypackage.mypackageB.IJK.*;
class packagetest
{
public static void main(String args[])
{
A a1 = new A();
}
}
mypackage.mypackageA.ABC.A a1 = new mypackage.mypackageA.ABC.A();
OR
mypackage.mypackageB.IJK.A a1 = new mypackage.mypackageB.IJK.A();
1.2CLASSPATH Environmental Variables
CLASSPATH Environmental Variable lets you define path for the location of the root of
the package hierarchy
Consider the following statement :
package mypack;
What should be true in order for the program to find mypack.
(i) Program should be executed from the location immediately above mypack
OR
(ii) mypack should be listed in the set of directories for CLASSPATH
2. INTERFACE
An interface may be considered a pure abstract class.
{
CS 2363
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
One may use the fact that the data fields (attributes) in an interface are static and final to
create enumerated types
public interface Months {
int
JANUARY = 1, FEBRUARY = 2, MARCH = 3, APRIL = 4,MAY = 5,
JUNE = 6, JULY = 7, AUGUST = 8, SEPTEMBER = 9, OCTOBER = 10,
NOVEMBER = 11, DECEMBER = 12;
}
In an application you may have code that uses this interface
if (!(Months.MAY || Months.JUNE || Months.JULY || Months.AUGUST))
System.out.println(Eat Oysters! );
3.EXCEPTION HANDLING
ClassNotFoundException
IOException
ArithmeticException
Exception
AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object
Throwable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
13
import java.util.*;
VirtualMachineError
Error
public class HandleExceptionDemo
{
public static void main(String[]
args) {
AWTError
Scanner scanner = new Scanner(System.in);
boolean continueInput = true;
Several more classes
do {
try {
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
If an exception occurs on this line,
the rest of lines in the try block are
skipped and the control is
transferred to the catch block.
CS 2363
COMPUTER NETWORKS
3.1Throwing Exceptions
When the program detects an error, the program can create an instance of an appropriate
exception type and throw it. This is known as throwing an exception. Here is an example,
throw new TheException();
TheException
ex
=
new
TheException();
throw ex;
/** Set a new radius */
public void setRadius(double newRadius)
throws IllegalArgumentException {
if (newRadius >= 0)
radius = newRadius;
else
throw new IllegalArgumentException(
"Radius cannot be negative");
}
3.2Catching Exceptions
try {
statements; // Statements that may throw exceptions
}
catch (Exception1 exVar1) {
handler for exception1;
}
catch (Exception2 exVar2) {
handler for exception2;
}
...
catch (ExceptionN exVar3) {
handler for exceptionN;
}
F An error message appears on the console, but the GUI application continues running.
F Write a program that creates a user interface to perform integer divisions. The user enters
two numbers in the text fields Number 1 and Number 2. The division of Number 1 and
Number 2 is displayed in the Result field when the Divide button is clicked.
CS 2363
4.MULTITHREADED PROGRAMMING
4.1A single threaded program
class ABC
{
.
public void main(..)
{
..
}
}
4.2 A Multithreaded Program
COMPUTER NETWORKS
CS 2363
Multithreading Multiprocessors
An example
class MyThread extends Thread { // the thread
public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx1 {
// a program that utilizes the thread
public static void main(String [] args ) {
MyThread t = new MyThread();
// due to extending the Thread class (above)
// I can call start(), and this will call
// run(). start() is a method in class Thread.
t.start();
} // end main()
}
// end class ThreadEx1
class MyThread implements Runnable
{
.....
public void run()
{
// thread body of execution
}
COMPUTER NETWORKS
CS 2363
}
Creating Object:
MyThread myObject = new MyThread();
Creating Thread Object:
Thread thr1 = new Thread( myObject );
Start Execution:
thr1.start();
class MyThread implements Runnable {
public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx2 {
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
// due to implementing the Runnable interface
// I can call start(), and this will call run().
t.start();
} // end main()
}
// end class ThreadEx2
4.3Life Cycle of Thread
COMPUTER NETWORKS
CS 2363
COMPUTER NETWORKS
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t From ThreadA: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\t From ThreadB: j= "+j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t From ThreadC: k= "+k);
}
System.out.println("Exit from C");
}
}
class ThreadTest
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
[raj@mundroo] threads [1:76] java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
CS 2363
COMPUTER NETWORKS
From ThreadA: i= 5
Exit from A
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
5.STRINGS
string: An object storing a sequence of text characters.
Unlike most other objects, a String is not created with new.
String name = "text";
String name = expression;
Examples:
String
name
int
x
int
y
String point = "(" + x + ", " + y + ")";
"Marla
=
=
5.1 Indexes
Characters of a string are numbered with 0-based indexes:
String name = "P. Diddy";
The first character's index is always 0
The last character's index is 1 less than the string's length
The individual characters are values of type char (seen later)
5.2String methods
indexOf(str)
length()
Singer";
3;
5;
CS 2363
COMPUTER NETWORKS
substring(index1, index2)
or
substring(index1)
toLowerCase()
toUpperCase()
//
index 012345678901
String s1 = "Stuart Reges";
String s2 = "Marty Stepp";
System.out.println(s1.length());
// 12
System.out.println(s1.indexOf("e")); // 8
System.out.println(s1.substring(7, 10)) // "Reg"
String s3 = s2.substring(2, 8);
System.out.println(s3.toLowerCase()); // "rty st"
Given the following string:
//
index 0123456789012345678901
String book = "Building Java Programs";
How would you extract the word "Java" ?
How would you extract the first word from any string?
5.3 Modifying strings
Methods
like
substring,
toLowerCase,
a new string, rather than modifying the current string.
String s = "lil bow wow";
s.toUpperCase();
System.out.println(s); // lil bow wow
To modify a variable, you must reassign it:
String s = "lil bow wow";
s = s.toUpperCase();
System.out.println(s); // LIL BOW WOW
Strings as parameters
public class StringParameters {
public static void main(String[] args) {
sayHello("Marty");
String teacher = "Helene";
sayHello(teacher);
}
public static void sayHello(String name) {
System.out.println("Welcome, " + name);
}
}
etc.
create/return
CS 2363
COMPUTER NETWORKS
Output:
Welcome, Marty
Welcome, Helene
Strings as user input
Scanner's next method reads a word of input as a String.
Scanner console = new Scanner(System.in);
System.out.print("What is your name? ");
String name = console.next();
name = name.toUpperCase();
System.out.println(name + " has " + name.length() +
" letters and starts with " + name.substring(0, 1));
Output:
What is your name? Madonna
MADONNA has 7 letters and starts with M
The nextLine method reads a line of input as a String.
System.out.print("What is your address? ");
String address = console.nextLine();
Comparing strings
Relational operators such as < and == fail on objects.
Scanner console = new Scanner(System.in);
System.out.print("What is your name? ");
String name = console.next();
if (name == "Barney") {
System.out.println("I love you, you love me,");
System.out.println("We're a happy family!");
}
This code will compile, but it will not print the song.
== compares objects by references (seen later), so it often gives false even when
two Strings have the same letters.
The equals method
Objects are compared using a method named equals.
Scanner console = new Scanner(System.in);
System.out.print("What is your name? ");
String name = console.next();
if (name.equals("Barney")) {
System.out.println("I love you, you love me,");
System.out.println("We're a happy family!");
}
Technically this is a method that returns a value of type boolean,
the type used in logical tests.
6.JAVA I/O THE BASICS
Java I/O is based around the concept of a stream
Ordered sequence of information (bytes) coming from a source, or going to a
sink
Simplest stream reads/writes only a single byte, or an array of bytes at a time
CS 2363
COMPUTER NETWORKS
Designed to be platform-independent
The stream concept is very generic
Can be applied to many different types of I/O
Files, Network, Memory, Processes, etc
The java.io package contains all of the I/O classes.
Many classes specialised for particular kinds of stream operations, e.g. file I/O
Reading/writing single bytes is quite limited
So, it includes classes which provide extra functionality
e.g. buffering, reading numbers and Strings (not bytes), etc.
Results in large inheritance hierarchy, with separate trees for input and output stream
classes
6.1Java I/O InputStream
CS 2363
COMPUTER NETWORKS
while (b != -1)
{
//do something
b = in.read();
}
in.close();
Weve omitted exception handling in the previous examples
Almost all methods on the I/O classes (including constructors) can throw an IOException
or a subclass.
Always wrap I/O code in trycatch blocks to handle errors.
6.2 I/O OutputStream