Computer Networks Lab Record-Final
Computer Networks Lab Record-Final
NTRODUCTI
ONTONETWORKPROGRAMMI
NGLAB
NETWORKI
NGBASI
CS
Comput
ernet
wor
kingi
stheengi
neer
ingdi
sci
pli
neconcer
nedwi
th
communi
cat
ionbet
weencomput
ersy
stemsordev
ices.
I
tist
hepr
act
iceofl
i
nki
ngcomput
ingdev
icest
oget
herwi
thhar
dwar
eand
sof
twar
ethatsuppor
tsdat
acommuni
cat
ionsacr
osst
hesedev
ices.
KEYCONCEPTSANDTERMS
PacketAmessageordat
auni
tthati
str
ansmi
tt
edbet
weencommuni
cat
ingpr
ocesses.
Host: Acomput
ersy
stem t
hati
saccessedbyauserwor
kingatar
emot
elocat
ion.I
t
i
sther
emot
epr
ocesswi
thwhi
chapr
ocesscommuni
cat
es.I
tmayal
sober
efer
redas
Peer
.
Channel
:Communi
cat
ionpat
hcr
eat
edbyest
abl
i
shi
ngaconnect
ionbet
weenendpoi
nts.
Net
wor
kAgr
oupoft
woormor
ecomput
ersy
stemsl
i
nkedt
oget
her
Ser
ver
:Incomput
ernet
wor
king,
aser
veri
sacomput
erdesi
gnedt
opr
ocessr
equest
s
anddel
i
verdat
atoot
hercomput
ersov
eral
ocal
net
wor
kort
heI
nter
net
.
I
ter
ati
veser
ver
s:Thi
sser
verknowsaheadoft
imeabouthowl
ongi
ttakest
o
handl
eeachr
equest&ser
verpr
ocesshandl
eseachr
equesti
tsel
f.
Concurr
entserver
s:Theamountofworkrequi
redt
ohandl
earequesti
s
unknown,sotheserv
erstar
tsanot
herpr
ocesstohandl
eeachrequest
.
Cl
ient
:ACl
i
enti
sanappl
i
cat
iont
hatr
unsonaper
sonalcomput
erorwor
kst
ati
onand
r
eli
esonaser
vert
oper
for
m someoper
ati
ons.
Net
wor
kAddr
ess:Net
wor
kaddr
essesgi
vecomput
ersuni
quei
dent
it
iest
heycanuset
o
communi
cat
ewi
theachot
her
.Speci
fi
cal
l
y,I
Paddr
essesandMACaddr
essesar
eused
onmosthomeandbusi
nessnet
wor
ks.
Pr
otocol
s:APr
otocoli
saconv
ent
ionorst
andar
drul
est
hatenabl
esandcont
rol
sthe
connect
ion,
communi
cat
ionanddat
atr
ansf
erbet
weent
wocomput
ingendpoi
nts.
Por
tAni
nter
faceonacomput
ert
owhi
chy
oucanconnectadev
ice.I
tisa"
logi
cal
connect
ionpl
ace"andspeci
fi
cal
l
y,usi
ngt
heI
nter
net
'spr
otocol
,TCP/
IP.
Apor
tisa16-
bitnumber
,usedbyt
hehost
-t
o-hostpr
otocolt
oident
if
ytowhi
ch
hi
gher
-lev
elpr
otocolorappl
i
cat
ion pr
ogr
am (
process)i
tmustdel
i
veri
ncomi
ng
messages.
PORTS RANGE
Well
-knownports 1-1023
Ephemeralpor
ts 1024- 5000
User-
defi
nedports5001-65535
Connect
ion:I
tdef
inest
hecommuni
cat
ionl
i
nkbet
weent
wopr
ocesses.
Associ
ati
on:Associ
ati
oni
susedf
or5t
upl
ethatcompl
etel
yspeci
fi
est
het
wo
pr
ocessest
hatmakeupaconnect
ion.
{Pr
otocol
,l
ocal
-addr
ess,
local
-pr
ocess,
for
eign-
addr
ess,
for
eign-pr
ocess}
Thel
ocal
addr
essandf
orei
gnaddr
essspeci
fyt
henet
wor
kID&Host
-I
Doft
hel
ocal
host
andt
hef
orei
gnhosti
nwhat
everf
ormati
sspeci
fi
edbypr
otocol
sui
te.
Thel
ocal
processandf
orei
gnpr
ocessar
eusedt
oident
if
ythespeci
fi
cpr
ocesseson
eachsy
stem t
hatar
einv
olv
edi
naconnect
ion.
Weal
sodef
ineHal
fassoci
ati
onasei
ther
{pr
otocol
,l
ocal
-addr
ess,
localpr
ocess}or{pr
otocol
,l
ocal
-addr
ess,
local
pr
ocess}whi
chspeci
fyeachhal
fofaconnect
ion.Thi
shal
fassoci
ati
oni
scal
l
eda
Socketort
ranspor
taddr
ess.
OSIModel
A common wayt
o descr
ibe t
he l
ayer
sin a net
wor
kist
o use t
he I
nter
nat
ional
Or
gani
zat
ionf
orSt
andar
dizat
ion(
ISO)opensy
stemsi
nter
connect
ion(
OSI
)modelf
or
comput
ercommuni
cat
ions.Thi
sisasev
en-
lay
ermodel
,whi
chweshowi
nFi
gur
ebel
ow
al
ongwi
tht
heappr
oxi
mat
emappi
ngt
otheI
nter
netpr
otocol
sui
te.
We consi
dert
he bot
tom t
wo l
ayer
s oft
he OSImodelas t
he dev
ice dr
iverand
net
wor
kinghar
dwar
ethatar
esuppl
i
edwi
tht
hesy
stem.Thenet
wor
klay
eri
shandl
edby
t
heI
Pv4andI
Pv6pr
otocol
s.Thet
ranspor
tlay
erst
hatwecanchoosef
rom ar
eTCPand
UDP
Lay
ersi
nOSImodelandI
nter
netpr
otocolsui
te.
Theuppert
hreel
ayer
soft
heOSImodelar
ecombi
nedi
ntoasi
ngl
elay
ercal
l
edt
he
appl
icat
ion.Thi
sist
heWebcl
i
ent(
browser
)orwhat
everappl
i
cat
ionwear
eusi
ng.Wi
th
t
heI
nter
netpr
otocol
s,t
her
eisr
arel
yanydi
sti
nct
ionbet
weent
heuppert
hreel
ayer
sof
t
heOSImodel
.
Thesocket
spr
ogr
ammi
ngi
nter
facesar
eint
erf
acesf
rom t
heuppert
hreel
ayer
s(t
he
"
appl
i
cat
ion"
)int
othet
ranspor
tlay
er.Thesocket
spr
ovi
det
hei
nter
facef
rom t
heupper
t
hreel
ayer
soft
heOSImodeli
ntot
het
ranspor
tlay
er.Ther
ear
etwor
easonsf
ort
his
desi
gn:
Theuppert
hreel
ayer
shandl
eal
lthedet
ail
soft
heappl
i
cat
ionandknowl
i
ttl
e
aboutt
hecommuni
cat
iondet
ail
s.Thel
owerf
ourl
ayer
sknowl
i
ttl
eaboutt
he
appl
i
cat
ion,
buthandl
eal
lthecommuni
cat
iondet
ail
s:sendi
ngdat
a,wai
ti
ngf
or
acknowl
edgment
s,andsoon.
Thesecondr
easoni
sthatt
heuppert
hreel
ayer
sof
tenf
orm whati
scal
l
edauser
pr
ocesswhi
l
ethel
owerf
ourl
ayer
sar
enor
mal
l
ypr
ovi
dedaspar
toft
heoper
ati
ng
sy
stem (
OS)ker
nel
.
CLI
ENT-
SERVERMODEL
Net
wor
kappl
i
cat
ionscanbedi
vi
dedi
ntot
wopr
ocess:
aCl
i
entandaSer
ver
,
wi
thacommuni
cat
ionl
i
nkj
oini
ngt
het
wopr
ocesses.
Nor
mal
l
y,f
rom Cl
i
ent
-si
dei
tisone-
oneconnect
ion.Fr
om t
heSer
verSi
de,
iti
smany
-one
connect
ion.
Thest
andar
dmodel
fornet
wor
kappl
i
cat
ionsi
sheCl
i
ent
-Sev
ermodel
.ASer
veri
sa
pr
ocesst
hati
swai
ti
ngt
obecont
act
edbyaCl
i
entpr
ocesssot
hatser
vercando
somet
hingf
ort
hecl
i
ent
.
Ty
pical
BSDSocket
sappl
i
cat
ionsconsi
stoft
wosepar
ateappl
i
cat
ionl
evel
processes;
onepr
ocess(
thecl
ient
)request
saconnect
ionandt
heot
herpr
ocess(
theser
ver)
accept
sit
.
Socketf
unct
ionsf
orel
ement
aryTCPcl
ient
/ser
veri
nConnect
ion-
ori
ent
edScenar
io
Theser
verpr
ocesscr
eat
esasocket
,bi
ndsanaddr
esst
oit
,andset
supamechani
sm
(
cal
l
edal
i
stenqueue)f
orr
ecei
vi
ngconnect
ionr
equest
s.Thecl
i
entpr
ocesscr
eat
esa
socketandr
equest
saconnect
iont
otheser
verpr
ocess.Oncet
heser
verpr
ocess
accept
sacl
i
entpr
ocess'
srequestandest
abl
i
shesaconnect
ion,f
ull
-dupl
ex(
two-
way
)
communi
cat
ioncanoccurbet
weent
het
wosocket
s.
Socketf
unct
ionsf
orel
ement
aryTCPcl
ient
/ser
veri
nConnect
ion-
lessScenar
io
By
te-
Order
ingFunct
ions:Consi
dera16-
biti
ntegert
hati
smadeupof2by
tes.Ther
e
ar
etwoway
stost
oret
het
woby
tesi
nmemor
y:wi
tht
hel
ow-
orderby
teatt
hest
art
ing
addr
ess,knownasl
i
ttl
e-endi
anby
teor
der
,orwi
tht
hehi
gh-
orderby
teatt
hest
art
ing
addr
ess,
knownasbi
g-endi
anby
teor
der
.
Li
tt
le-
endi
anby
teor
derandbi
g-endi
anby
teor
derf
ora16-
biti
nteger
.
I
nthi
sfi
gur
e,weshowi
ncr
easi
ngmemor
yaddr
essesgoi
ngf
rom r
ightt
olef
tint
het
op,
andf
rom l
eftt
ori
ghti
nthebot
tom.Weal
soshowt
hemostsi
gni
fi
cantbi
t(MSB)ast
he
l
eft
mostbi
toft
he16-
bitv
alueandt
hel
eastsi
gni
fi
cantbi
t(LSB)ast
her
ight
mostbi
t.
Thet
erms"
li
tt
le-
endi
an"and"
big-
endi
an"i
ndi
cat
ewhi
chendoft
hemul
ti
byt
eval
ue,t
he
l
i
ttl
eendort
hebi
gend,
isst
oredatt
hest
art
ingaddr
essoft
hev
alue.
Wer
efert
otheby
teor
der
ingusedbyagi
vensy
st hehostby
em ast teor
der
.Wemust
dealwi
tht
heseby
teor
der
ingdi
ff
erencesasnet
wor
kpr
ogr
ammer
sbecausenet
wor
king
pr
otocol
smustspeci
fya net
wor
kby
teor
der
.Ourconcer
nist
her
efor
econv
ert
ing
bet
weenhostby
teor
derandnet
wor
kby
teor
der
.Weuset
hef
oll
owi
ngf
ourf
unct
ionst
o
conv
ertbet
weent
heset
woby
teor
der
s.
#i
ncl
ude<neti
net/
in.
h>
#i
ncl
ude<sys/t
ypes.
h>
unsi
gnedl
onght
onl
(unsi onghost
gnedl long);
unsi
gnedshor
t
ht
ons(
unsi thost
gnedshor shor
t);
unsi
gnedl
ong
nt
ohl
(unsi ongnet
gnedl long);
unsi
gnedshor
tnt
ohs(
unsi tnet
gnedshor shor
t);
ht
on host t
o net
wor
k
s shor
t host t
o
ht
on net
wor
k l
ong
l net
wor
k t
o host
nt
oh shor
t
s net
wor
ktohostl
ong
nt
ohl
Socket
sOv
erv
iew
Theoper
ati
ngsy
stem i
ncl
udest
heBer
kel
eySof
twar
eDi
str
ibut
ion(
BSD)i
nter
process
communi
cat
ion(
IPC)f
aci
l
ityknownassocket
s.Socket
sar
ecommuni
cat
ionchannel
s
t
hatenabl
eunr
elat
edpr
ocessest
oexchangedat
alocal
l
yandacr
ossnet
wor
ks.Asi
ngl
e
socketi
soneendpoi
ntofat
wo-
waycommuni
cat
ionchannel
.
Socket
sOv
erv
iew:I
ntheoper
ati
ngsy
stem,
socket
shav
ethef
oll
owi
ngchar
act
eri
sti
cs:
Asocketexi
stsonl
yasl
ongasapr
ocesshol
dsadescr
ipt
orr
efer
ri
ngt
oit
.
Socket
sar
eref
erencedbyf
il
edescr
ipt
orsandhav
equal
i
tiessi
mil
art
othoseofa
char
act
erspeci
aldev
ice.Read,
wri
te,
andsel
ectoper
ati
onscanbeper
for
med
onsocket
sbyusi
ngt
heappr
opr
iat
esubr
out
ines.
Socket
scanbecr
eat
edi
npai
rs,
giv
ennames,
orusedt
orendezv
ouswi
thot
her
socket
sinacommuni
cat
iondomai
n,accept
ingconnect
ionsf
rom t
hese
socket
sorexchangi
ngmessageswi
tht
hem.
Socket
sBackgr
ound:
Socket
swer
edev
elopedi
nresponset
otheneedf
orsophi
sti
cat
ed
i
nter
processf
aci
l
iti
est
omeett
hef
oll
owi
nggoal
s:
Pr
ovi
deaccesst
ocommuni
cat
ionsnet
wor
kssuchast
heI
nter
net
.
Enabl
ecommuni
cat
ionbet
weenunr
elat
edpr
ocessesr
esi
dingl
ocal
l
yonasi
ngl
e
hostcomput
erandr
esi
dingr
emot
elyonmul
ti
plehostmachi
nes.
SocketFaci
li
ti
es:Socketsubr
out
ines and net
wor
kli
brar
ysubr
out
ines pr
ovi
de t
he
bui
l
ding bl
ocksf
orI
PC.Anappl
i
cat
ionpr
ogr
am mustper
for
m t
hef
oll
owi
ng basi
c
f
unct
ionst
oconductI
PCt
hrought
hesocketl
ayer
:
Cr
eat
eandnamesocket
s.
Acceptandmakesocketconnect
ions.
Sendandr
ecei
vedat
a.
Shutdownsocketoper
ati
ons.
SocketI
nter
face:TheSocketi
nter
facepr
ovi
desast
andar
d,wel
l
-document
ed
appr
oacht
oaccessker
nel
net
wor
kresour
ces.
SocketHeaderFi
lest
o be I
ncl
uded:Socketheaderf
il
escont
ain dat
a def
ini
ti
ons,
st
ruct
ures,const
ant
s,macr
os,andopt
ionsusedbysocketsubr
out
ines.Anappl
i
cat
ion
pr
ogr
am musti
ncl
udet
heappr
opr
iat
eheaderf
il
etomakeuseofst
ruct
uresorot
her
i
nfor
mat
ionapar
ti
cul
arsocketsubr
out
iner
equi
res.Commonl
yusedsocketheaderf
il
es
ar
e:
/
usr
/incl
ude/
neti
net /i
n.h Defi
nesI nt
ernetconst antsandst ructures.
/
usr
/incl
ude/
netdb.h Containsdatadefinit
ionsf orsocketsubr outi
nes.
/
usr
/incl
ude/
sys/socket.h Containsdatadefinit
ionsandsocketst ructur
es.
/
usr
/incl
ude/
sys/types.h Containsdatatypedef ini
ti
ons.
/
usr
/incl
ude/
arpa.h Containsdefi
niti
onsf orinternetoper ati
ons.
/
usr
/incl
ude/
sys/errno.h Defi
nest heerrnov al
uest hatar ereturnedby
dr
iversandot herkernel-l
evel code.
I
nter
netaddr
esst
ransl
ati
onsubr
out
inesr
equi
ret
hei
ncl
usi
onoft
hei
net
.hf
il
e.The
i
net
.hf
il
eisl
ocat
edi
nthe/
usr
/incl
ude/
arpadi
rect
ory
.
SocketAddr
esses:Socket
scanbenamedwi
thanaddr
esssot
hatpr
ocessescan
connectt
othem.Mostsocketf
unct
ionsr
equi
reapoi
ntert
oasocketaddr
essst
ruct
ure
as an ar
gument
.Each suppor
ted pr
otocolsui
te def
ines i
ts own socketaddr
ess
st
ruct
ure.Thenamesoft
hesest
ruct
uresbegi
nwi
thsockaddr
_andendwi
thauni
que
suf
fi
xforeachpr
otocol
sui
te.
Gener
icsocketaddr
essst
ruct
ure:Manyoft
heNet
wor
kingsy
stem cal
l
srequi
rea
poi
ntert
oasocketaddr
essst
ruct
ureasanar
gument
.Def
ini
ti
onoft
hisst
ruct
urei
sin
#i
ncl
ude<sy
s/socket
.h>
st
ructsockaddr{
unsi
gnedshortsa_f
amily
;/*addr
essf ami
ly:AF_ xxxVal
ue*
/
char sa_ data[
14];/
*upto14by t
esofpr otocol-
speci
ficaddress* /
}
;
I
nter
netSocketaddressstr
ucture:Thepr
otocol
speci
fi
cst
ruct
uresockaddr
_ini
s
i
dent
ical
insizetogeneri
cstruct
urewhichis16byt
es.
#i
ncl
ude<net
inet
/in.
h>
st
ructsockaddr_
in{
short sin_family;
/*AF_ I
NET
unsi
gnedshor tsin_port
;/*16-bitportnumber* /
/*Net work-
byteor
der
ed
*/st
ructin_
addrsi n_addr; / *32-bi
tnetid/
hosti
d*/
/*Net work-
byteor
der
ed*/
char si
n_ zero[8]
;/*unused*/
};
st
ructi
n_addr{
unsignedl
ongs_
addr
;/*32-
bitnet
id/
hosti
d*/
/*networkbyt
eorder
ed*
/
};
si
n_zer
oisunusedmember
,butweal
way
sseti
tto0whenf
il
li
ngi
noneoft
hese
st
ruct
ures.Socketaddr
essst
ruct
uresar
eusedonl
yonagi
venhost
:thest
ruct
urei
tsel
f
i
snowcommuni
cat
edbet
weendi
ff
erenthost
s,al
thoughcer
tai
nfi
elds(
eg:
IPAddr
ess&
por
ts)ar
eusedf
orcommuni
cat
ion.*
Thepr
otocol
-speci
fi
cst
ruct
uresockaddr
_ini
s
i
dent
ical
insi
zet
ogener
icst
ruct
uresockaddrwhi
chi
s16by
tes.
ELEMENTARYSOCKETSYSTEM CALLS
Socket
()Sy
stem Cal
l:Cr
eat
esanendpoi
ntf
orcommuni
cat
ionandr
etur
nsadescr
ipt
or.
Sy
ntax
#incl
ude
<sys/socket.
h>
#incl
ude
<sys/types.
h>
i ntAd
ntsocket(i dr
essFami
l
y ntTy
,i pe, ntPr
i otocol
);
Descr
ipt
ion:Thesocketsubr
out
inecr
eat
esasocketi
nthespeci
fedAddr
i essFami
l
yand
oft
hespeci
fi
edt
ype.Apr
otocolcanbespeci
fi
edorassi
gnedbyt
hesy
stem.I
fthe
pr
otocoli
slef
tunspeci
fi
ed(
aval
ueof0)
,thesy
stem sel
ect
sanappr
opr
iat
epr
otocol
f
rom t
hosepr
otocol
sint
headdr
essf
ami
l
ythatcanbeusedt
osuppor
tther
equest
ed
sockett
ype.
Thesocketsubr
out
iner
etur
nsadescr
ipt
or(
ani
nteger
)thatcanbeused i
nlat
er
subr
out
inest
hatoper
ateonsocket
s.
Par
amet
ers
Addr
essFami
l
y Specif
iesanaddr essf
ami l
ywithwhi chaddressesspeci
fi
edi
nlater
socketoperati
onsshouldbeinterpreted.Commonlyusedfamil
i
es
are:
AF_UNI X
DenotestheUnixint
ernalprotocols
AF_INET
DenotestheInt
ernetprotocols.
AF_NS
DenotestheXEROXNet workSy st
emspr ot
ocol.
Ty
pe Speci
fi
esthesemant i
csofcommuni cat
ion.Theoper ati
ngsyst
em
suppor
tsthefol
lowingtypes:
SOCK_STREAM
Provi
dessequenced, two-wayby testreamswi tha
tr
ansmissionmechani sm forout-of-
banddat a.
SOCK_DGRAM
Provi
desdatagrams, whichar econnect ionl
essmessagesofa
fi
xedmaxi mum length(usuallyshort)
.
SOCK_RAW
Provi
desaccesst ointer
nal networkpr otocol
sandi nt
erf
aces.
Thisty
peofsocketi savailableonlytot herootuser.
SOCK_SEQPACKET
Sequencedpacketsocket
Pr
otocol Speci
fiesapar t
icul
arprotocoltobeusedwiththesocket
.Speci
fying
theProtocolparameterof0causest hesocketsubr
outi
netoselect
syst
em’ sdefaul
tforthecombi nat
ionoffamil
yandtype.
IPROTO_ TCP TCP Tr ansportprotocol
IPROTO_ UDP UDP Tr ansportprotocol
IPROTO_ SCTP SCTP Transport
prot
ocol
Ret
urnVal
uesUponsuccessf
ulcompl
eti
on,
thesocketsubr
out
iner
etur
nsani
nteger
(
thesocketdescr
ipt
or)
.Itr
etur
ns-
1oner
ror
.
Bi
nd()Sy
stem cal
l:Bi
ndsanamet
oasocket
.
Descri
pti
on:Thebindsubrouti gnsaNamepar
neassi amet
ert
oanunnamedsocket
.It
assi
gnsalocalpr
otocoladdresst
oasocket
.
Sy
ntax
#i
ncl
ude<sy
s/socket
.h>
i
ntbi
nd(
intsockf
d,st
ructsockaddr*
myaddr
,intaddr
len)
;
sockfdisasocketdescr
ipt
orret
urnedbythesocketfunct
ion.Thesecondar
gumenti
s
apointert
oapr ot
ocolspeci
fi
caddressandthir
dar gumenti ssi
zeofthi
saddr
ess
str
ucture.
Ther
ear
e3usesofbi
nd:
a)Serverregi
sterst hei
rwel l
-knownaddr esswithasy stem.Bot hconnect
ion-
ori
entedandconnect i
on-l
essserversneedt odot hi
sbef oreaccepti
ngclient
requests.
b)ACl i
entcanr egisteraspeci f
icaddressforit
self
.
c)AConnect i
onl essclientneedstoassur ethatthesy stem assi
gnsitsome
uniqueaddress, sot hatt
heot herend( t
heserver)hasav al
idret
urnaddressto
senditsresponsest o.
Retur
nValues:Uponsuccessf
ulcompl
eti
on,t
hebi
ndsubrout
iner
etur
nsav
alueof0.
Other
wise,i
tret
urnsavalueof-1t
othecal
li
ngpr
ogram.
connect
()Sy
stem cal
l
:
Theconnectf
unct
ioni
susedbyaTCPcl
i
entt
oest
abl
i
shaconnect
ionwi
thaTCPser
ver
.
#i
ncl
ude<sy
s/socket
.h>
i
ntconnect
(nts
i ockf
d,st
r ser
uctsockaddr* vaddr
,intaddr
len)
;
sockfdisasocketdescript
orr
etur
nedbythesocketfunct
ion.Thesecondandthir
d
argumentsareapointertoasocketaddr
essstr
uct ureanditssi
ze.Thesocketaddr
ess
struct
uremustcontai
nt heIPaddr
essandportnumberoft heser
ver.
Ret
urnValues:Uponsuccessf
ulcomplet
ion,
theconnectsubr
outiner
etur
nsa
val
ueof0.Otherwise,
itr
etur
nsav al
ueof-1tothecal
li
ngprogram.
l
ist
en(
)Sy
stem cal
l
Thi
ssyst
em cal
lisusedbyaconnect
ion-
ori
ent
edser
vert
oindi
cat
ethati
tiswi
l
li
ng
tor
ecei
veconnecti
ons.
#i
ncl
ude<sy
s/socket
.h>
i
ntl
ist
en(
intsockf
d,ntba
i ckl
og)
;
I
tisusual
l
yexecut
edaf
terbot
hthesocketandbi
ndsy
stem cal
l
s,andi
mmedi
atel
y
bef
oreacceptsy
st .Thebackl
em cal
l og ar
gumentspeci
fi
eshow manyconnect
ions
r
equest
scanbequeuedbyt
hesy
stem whi
l
eitwai
tsf
ort
heser
vert
oexecut
ethe
acceptsy
stem cal
l
.
Ret
urnv
alues:
Ret
urns0i
fOK,
-1oner
ror
accept
()Sy
stem cal
l:Theact
ual
connect
ionf
rom somecl
i
entpr
ocessi
swai
tedf
or
byhav
ingt
heser
verexecut
etheacceptsy
stem cal
l
.
#i
ncl
ude<sy
s/socket
.h>
i
ntaccept(
intsockf
d,st
r cl
uctsockaddr* i
addr
,i addr
nt* len)
;
acceptt
akest
hef
ir
stconnect
ionr
equestont
hequeueandcr
eat
esanot
hersocket
wi
tht
hesamepr
oper
tesassockf
i d.I
fther
ear
enoconnect
ionr
equest
spendi
ng,t
his
cal
lbl
ockst
hecal
l
erunt
ilonear
ri
ves.Thecl
i
addrandaddr
lenar
gument
sar
eusedt
o
r
etur
nthepr
otocoladdr
essoft
heconnect
edpeerpr
ocess(
thecl
i
ent
).addr
leni
scal
l
ed
av
alue-
resul
tar
gument
.
RETURN VALUES:Thi
ssy
stem cal
lret
urnsupt
othr
eev
alues:ani
ntegerr
etur
ncode
t
hati
sei
theranewsocketdescr
ipt
ororaner
rori
ndi
cat
ion,
thepr
otocoladdr
essoft
he
cl
i
entpr
ocess(
thr
ought
hecl
i
addrpoi
nter
),andt
hesi
zeoft
hisaddr
ess(
thr
ought
he
addr
lenpoi
nter
).
Send()
,sendt
o()
,r
ecv
()andr
ecv
from()sy
stem cal
ls:
Thesesyst
em cal
lsar
esimil
artothest
andar
dreadandwr
ief
t unct
ions,
butone
addi
ti
onalargumenti
srequi
red.
#i
ncl
ude<sy
s/socket
.h>
i
ntsend(
intsockf
d,char*buf
f,ntnby
i tes,
intf
lags)
;
i
ntsendto(
intsockf
d,charvoi
d*buf
f,ntn
i byt
es,
intf
lags,st
r t
uctsockaddr*o,
int
addrlen);
i
ntr
ecv(
intsockf
d, buf
char* f,ntn
i byt
es,
intf
lags)
;
i
ntr
ecvf
rom(nts
i ockf
d, buf
char* f,ntnby
i tes,
intf
lags,
str f
uctsockaddr*rom,
int
addr
* len);
Thef
ir
stt
hreear s,sockf
gument d,buf
fand nby
tesar
ethesameast
hef
ir
stt
hree
ar
gument
stor
eadandwr
ie.Th
t ef
lagsar
gumenti
sei
ther0ori
sfor
medbyl
ogi
cal
l
y
OR'
i
ngoneormor
eoft
heconst
ant
s.
MSG_
OOB:Sendorr
ecei
veout
-of
-banddat
a.Thi
sfl
agspeci
fi
est
hatout
-of
-banddat
ais
bei
ngsent
.
MSG_
PEEK:Peekati
ncomi
ngmessage(
recvorr
ecv
from)
.Thi
sfl
agl
etst
hecal
l
erl
ook
att
hedat
athat
’sav
ail
abl
etober
ead,wi
thouthav
ingt
hesy
stem di
scar
dthedat
aaf
ter
r
ecvorr
ecv
from r
etur
ns.
DONTROUTE:Th
MSG_ i
sfl
agt
ell
stheker
nelt
hatt
hedest
inat
ioni
sonal
ocal
l
yat
tached
net
wor
kandnott
oper
for
m al
ookupoft
her
out
ingt
abl
e.
Thet
oar
gumentf
orsendt
oisasocketaddr
essst
ruct
urecont
aini
ngt
hepr
otocol
addr
essofwher
ethedat
aist
obesent
.Thesi
zeoft
hissocketaddr
essst
ruct
urei
s
speci
fi
edbyaddr
len.Ther
ecv
from f
unct
ionf
il
lsi
nthesocketaddr
essst
ruct
urepoi
nted
t
obyf
rom wi
tht
hepr
otocol
addr
essofwhosentt
hedat
agr
am.
RETURNVALUES:
All
foursy
stem cal
l
sret
urnt
hel
engt
hoft
hedat
athatwaswr
it
tenor
r
eadast
hev
alueoft
hef
unct
ion.Ot
her
wisei
tret
urns,
-1oner
ror
.
Thenet
wor
ksy
stem cal
lst
akest
woar
gument
s:t
headdr
essoft
hegener
ic
sockaddrst
ruct
ureandt
hesi
zeoft
hepr
otocol
speci
fi
cst
ruct
ure.
Thecal
l
ermustdoi
spr
ovi
det
headdr
essofpr
otocl
-speci
fi
cst
ruct
reasanar
gument
,
cast
ingt
hispoi
ntert
oagener
icsocketaddr
essst
ruct
ure.
Fr
om t
heker
nel
'
sper
spect
ive,anot
herr
eason f
orusi
ng poi
nter
sto gener
ic
socketaddr
essst
ruct
uresasar
gument
sist
hatt
heker
nelmustt
aket
hecal
l
er'
spoi
nter
,
casti
ttoast
r ,
uctsockaddr*andt
henl
ookatt
hev
alueofsa_f yt
ami
l odet
ermi
net
het
ypeof
f
ami
l
y.
Cl
ose()sy
stem cal
l:
Thenor
mal
Uni
xcl
osef
unct
ioni
sal
sousedt
ocl
oseasocketandt
ermi
nat
eaTCPconnect
ion.
#i
ncl
ude<uni
std.
h>
i
ntcl
ose(
intsockf
d);
VALUERESULT-
ARGUMENTS:
Whenasocketaddr
essst
ruct
urei
spassedt
oanysocketf
unct
ion,i
tisal
way
spassed
byr
efer
ence.Thati
s,apoi
ntert
othest
ruct
urei
spassed.Thel
engt
hoft
hest
ruct
urei
s
al
sopassedasanar
gument
.Butt
hewayi
nwhi
cht
hel
engt
hispasseddependson
whi
chdi
rect
iont
hest
ruct
urei
sbei
ngpassed:f
rom t
hepr
ocesst
otheker
nel
,orv
ice
v
ersa.
1.Thr
eef
unct
ions,bi
nd,connect
,andsendt
o,passasocketaddr
essst
ruct
uref
rom
t
hepr
ocesst
otheker
nel
.Onear
gumentt
otheset
hreef
unct
ionsi
sthepoi
ntert
o
t
hesocketaddr
essst
ruct
ureandanot
herar
gumenti
sthei
ntegersi
zeoft
he
st
ruct
ure,
asi
n
st
ructsockaddr
_inser
v;
/
*fi
l
linser
v{}*
/
connect(
sockf
d,(
SA*
)&ser
v,si
zeof
(ser
v))
;
Si
ncet
heker
nel
ispassedbot
hthepoi
nterandt
hesi
zeofwhatt
hepoi
nter
poi
ntst
o,i
tknowsexact
lyhowmuchdat
atocopyf
rom t
hepr
ocessi
ntot
he
ker
nel
.Fi
gur
eshowst
hisscenar
io.
Fi
gur
e:Socketaddr
essst
ruct
urepassedf
rom pr
ocesst
oker
nel
2.Fourf
unct
ions,accept
,recv
from, sockname,
get andget name,
peer passasocketaddr
ess
st
ruct
uref
rom t
heker
nelt
othepr
ocess,t
her
ever
sedi
rect
ionf
rom t
hepr
evi
ous
scenar
io.Twooft
hear
gument
stot
hesef
ourf
unct
ionsar
ethepoi
ntert
othesocket
addr
essst
ruct
ureal
ongwi
thapoi
ntert
oani
ntegercont
aini
ngt
hesi
zeoft
he
st
ruct
ure,
asi
n:
st
ructsockaddr
_uncl
i
;/*Uni
xdomai
n*/
sockl
en_
tlen;
l
en=si
zeof
(cl
i
); /
*leni
sav
alue
*
/accept
(uni
xfd,
(SA*
)&cl
i
,&l
en)
;
/
*lenmayhav
echanged*
/
Ther
easont
hatt
hesi
zechangesf
rom ani
ntegert
obeapoi
ntert
oani
ntegeri
s
becauset
hesi
zei
sbot
hav
aluewhent
hef
unct
ioni
scal
l
ed(
itt
ell
stheker
nelt
he
si
zeoft
hest
ruct
uresot
hatt
heker
neldoesnotwr
it
epastt
heendoft
he
st
ruct
urewhenf
il
li
ngi
tin)andar
esul
twhent
hef
unct
ionr
etur
ns(
itt
ell
sthe
pr
ocesshowmuchi
nfor
mat
iont
heker
nelact
ual
l
yst
oredi
nthest
ruct
ure)
.Thi
s
t
ypeofar
gumenti
scal
l
edav
alue-
resul
tar
gument
.Fi
gur
eshowst
hisscenar
io.
.
Fi
gur
e.Socketaddr
essst
ruct
urepassedf
rom ker
nel
to
pr
ocess
Pr
ogr
am 1:
Under
standi
ngandusi
ngofcommandsl
ikei
fconf
ig,
net
stat
,
pi
ng,
arp,
tel
net
,ft
p,f
inger
,tr
acer
out
e,whoi
s
Pr
ogr
am Descr
ipt
ion:
UNI
Xut
il
it
iesar
ecommandst
hat
,gener
all
y,per
for
m asi
ngl
etask.I
tmaybeassi
mpl
e
aspr
int
ingt
hedat
eandt
ime,oracompl
exasf
indi
ngf
il
est
hatmat
chmanycr
it
eri
a
t
hroughoutadi
rect
oryhi
erar
chy
I
FCONFI
G
TheUni
xcommandif
conf
ig(shor
tforint
erf
aceconf
igurat
or)servestoconf
igur
eand
cont
rol
TCP/I
Pnet
wor ki
nterf
acesfrom acommandlineint
erface(CLI
).
Commonusesforif
conf
igi
ncl
udesett
ingani
nter
face'
sIPaddr
essandnet
mask,
and
di
sabl
i
ngorenabl
ingagi v
enint
erf
ace.
NETSTAT
netst
at(networkst
ati
sti
cs)isacommand-
li
netoolt
hatdi
splay
snet
work
connecti
ons(bothi
ncomingandoutgoi
ng)
,rout
ingt
abl
es,andanumberof
networkint
erf
acestat
ist
ics.
I
tisusedf
orf
indi
ngprobl
emsi
nthenetwor
kandtodeter
minet
heamountof
t
raff
icont
henetwor
kasaperf
ormancemeasur
ement.
Par
amet
ers
Par
amet
ersusedwi
tht
hiscommandmustbepr
efi
xedwi
thahy
phen(
-)r
athert
hanasl
ash(
/).
-a:Di
splay
sallact
iveTCPconnect
ionsandt
heTCPandUDPpor
tsonwhi
cht
he
computeri
sli
steni
ng.
-
e:Displ
aysether
netst
ati
sti
cs,suchasthenumberofby
tesandpacket
ssentand
r
ecei
ved.Thi
sparametercanbecombinedwith-
s.
-
f:Di
spl
aysf
ull
yqual
i
fieddomai
nnames<FQDN>f
orf
orei
gnaddr
esses.
-
i:Di
spl
aysnet
wor
kint
erf
acesandt
hei
rst
ati
sti
cs(
notav
ail
abl
eunderWi
ndows)
-n:Display
sact
iveTCPconnect
ions,
however
,addr
essesandpor
tnumber
sar
e
expressednumeri
cal
lyandnoat
temptismadetodeter
minenames.
-
o:Di
spl
aysact
iveTCPconnect
ionsandi
ncl
udest
hepr
ocessI
D(PI
D)f
oreachconnect
ion.
-
pLi
nux:
Process:
Showwhi
chpr
ocessesar
eusi
ngwhi
chsocket
s
PI
NG
Thewordpingisalsofr
equent
lyusedasaver
bornoun,wherei
tisusual
l
yincor
rect
ly
usedt
orefertotheround-
tr
ipti
me,ormeasur
ingt
heround-
tr
ipti
me.
ARP
Incomputernet
working,t
heAddr essResol ut
ionPr
otocol(ARP)ist
hemethodf
or
fi
ndingahost'
sli
nklayer(har
dwar e)addresswhenonlyit
sInter
netLay
er(
IP)or
someotherNetworkLayeraddressi sknown.
TELNET
Tel
net(Tel
ecommuni
cati
onnetwork)i
sanet
wor
kpr
otocol
usedont
heI
nter
netor
l
ocalar
eanetwor
k(LAN)connect
ions.
Ty
pical
l
y,t
elnetpr
ovi
desaccesst
oacommand-
li
nei
nter
faceonar
emot
emachi
ne.
Thet mt
er el
netal
sorefer
stosoft
warewhichimpl
ementst
hecl
i
entpar
toft
he
prot
ocol
.Tel
netcl
ient
sareavai
labl
eforv
irtual
l
yal
lpl
atf
orms
Pr
otocoldet
ail
s:
Telneti
sacli
ent-
serverpr
otocol,
basedonarel
iabl
econnect
ion-
ori
ent
edt
ranspor
t.
Typical
l
ythi
sprotocoli
susedt oest
abli
shaconnect
iont
oTCPpor t23
FTP
Fi
leTr
ansf
erPr
otocol(
FTP)
:
FTPi
sanet
wor
kpr
otocolusedt
otr
ansf
erdat
afr
om onecomput
ert
oanot
hert
hrough
anet
wor
ksuchast
heI
nter
net
.FTP i
saf
il
etr
ansf
erpr
otocolf
orexchangi
ngand
mani
pul
ati
ngf
il
esov
eraTCPcomput
ernet
wor
k.AnFTPcl
i
entmayconnectt
oanFTP
ser
vert
omani
pul
atef
il
esont
hatser
ver
.FTPr
unsov
erTCP.I
tdef
aul
tst
oli
stenonpor
t
21f
ori
ncomi
ngconnect
ionsf
rom FTPcl
i
ent
s.Aconnect
iont
othi
spor
tfr
om t
heFTP
Cl
i
entf
ormst
hecont
rolst
ream onwhi
chcommandsar
epassedf
rom t
heFTPcl
i
entt
o
t
heFTPser
verandonoccasi
onf
rom t
heFTPser
vert
otheFTPcl
i
ent
.FTPusesout
-of
-
bandcont
rol
,whi
chmeansi
tusesasepar
ateconnect
ionf
orcont
rolanddat
a.Thus,
for
t
heact
ualf
il
etr
ansf
ert
otakepl
ace,adi
ff
erentconnect
ioni
srequi
redwhi
chi
scal
l
ed
t
hedat
ast
ream.
FI
NGER:
I
ncomput
ernet
wor
king,
theName/
Fingerpr
otocolandt
heFi
ngeruseri
nfor
mat
ionpr
otocol
ar
esi
mpl
enet
wor
kpr
otocol
sfort
heexchangeofhuman-
ori
ent
edst
atusanduseri
nfor
mat
ion.
TRACEROUTE:
t
racer
out
eisacomput
ernet
wor
ktoolusedt
odet
ermi
net
her
out
etakenbypacket
s
acr
ossanI
Pnet
wor
k.AnI
Pv6v
ari
ant
,tr
acer
out
e6,i
sal
sowi
del
yav
ail
abl
e.Tr
acer
out
e
i
sof
tenusedf
ornet
wor
ktr
oubl
eshoot
ing.Byshowi
ngal
i
stofr
out
erst
rav
ersed,i
t
al
l
owst
heusert
oident
if
ythepat
htakent
oreachapar
ti
cul
ardest
inat
ionont
he
net
wor
k.Thi
scanhel
pident
if
yrout
ingpr
obl
emsorf
ir
ewal
l
sthatmaybebl
ocki
ng
accesst
oasi
te.Tr
acer
out
eisal
sousedbypenet
rat
iont
est
erst
ogat
heri
nfor
mat
ion
aboutnet
wor
kinf
rast
ruct
ureandI
Prangesar
oundagi
venhost
.Itcanal
sobeused
whendownl
oadi
ngdat
a,andi
fther
ear
emul
ti
plemi
rr
orsav
ail
abl
efort
hesamepi
eceof
dat
a,onecant
raceeachmi
rr
ort
ogetagoodi
deaofwhi
chmi
rr
orwoul
dbet
hef
ast
est
t
ouse.
WHOI
S:
WHOI
S(pr
onounced"
whoi
s";notanacr
ony
m)i
saquer
y/r
esponsepr
otocolwhi
chi
s
wi
del
yusedf
orquer
yinganof
fi
cialdat
abasei
nor
dert
odet
ermi
net
heownerofa
domai
nname,
anI
Paddr
ess,
oranaut
onomoussy
stem numberont
heI
nter
net
.WHOI
S
l
ookupswer
etr
adi
ti
onal
l
ymadeusi
ngacommandl
i
nei
nter
face,butanumberof
si
mpl
i
fiedweb-
basedt
ool
snow exi
stf
orl
ooki
ngupdomai
nowner
shi
pdet
ail
sfr
om
di
ff
erentdat
abases.WHOI
Snor
mal
l
yrunsonTCPpor
t43.
TheWHOI
Ssy
stem or
igi
nat
edasamet
hodt
hatsy
stem admi
nist
rat
orscoul
duset
o
l
ookupi
nfor
mat
iont
ocont
actot
herI
Paddr
essordomai
nnameadmi
nist
rat
ors(
almost
l
i
kea"
whi
tepages"
).
Pr
ogr
am 2:SocketPr
ogr
ammi
ng:I
mpl
ement
ati
onofConnect
ion-
Ori
ent
edSer
vice
usi
ngst
andar
dpor
ts.
i
. EchoSer
vice(
7)
i
i
. Dat
eandTi
meSer
vice(
13)
i
i
i. Ti
meofDaySer
vice(
37)
i
v. Char
act
ergener
ati
on(
19)
Pr
ogr
am Descr
ipt
ion The St
andar
d por
tnumber
s ar
ethe por
tnumber
sthatar
e
r
eser
vedf
orassi
gnmentf
orusebyt
heappl
i
cat
ionendpoi
ntst
hatcommuni
cat
eusi
ng
t
heI
nter
net
'sTr
ansmi
ssi
onCont
rolPr
otocol(
TCP)ort
heUserDat
agr
am Pr
otocol
(
UDP)
.Eachki
ndofappl
i
cat
ionhasadesi
gnat
ed(
andt
hus"
wel
l
-known"
)por
tnumber
.
I
nor
dert
oimpl
ementt
hest
andar
dpor
tsweneedt
ocr
eat
eanappl
i
cat
ionf
ori
nst
ance
saycl
i
ent
,whi
chi
sgoi
ngt
oinv
okeser
vicewhi
chi
sest
abl
i
shedont
hest
andar
dpor
ts.
TheCl
i
entwi
l
lbecr
eat
ingi
tssocketendpoi
ntandest
abl
i
shaconnect
ionwi
tht
he
st
andar
dser
verbyspeci
fi
yi
ngt
hepor
tnumberwhi
chhast
hedef
inedser
vice,f
or
i
nst
ance7f
orechoser
vice.
STEPS:
Connect
ionOr
ient
edI
mpl
ement
ati
on
Cl
ient
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
toft
heSer
ver
,wher
eiti
spr
ovi
dingser
vice
Est
abl
i
shconnect
iont
otheSer
v ngconnect
erusi ()sy
stem cal
l
.
Forechoser
ver
,sendamessaget
otheser
vert ngsend(
obeechoedusi )sy
stem cal
l
.
Recei
vet
her
esul
toft
her
equestmadet
otheser
v ngr
erusi ecv
()sy
stem cal
l
.
Wr
it
ether
esul
tthusobt
ainedont
hest
andar
dout
put
.
Val
idat
ion:
Sampl
eInput
:Forpor
t37:
Cli
entsendsanempt
ymessage
Sampl
eOut
put
:Forpor
t37:
Timeel
apsedsi y1st1900i
nceJanuar nsecondswi
l
lbe
di
spl
ayed.”
Pr
ogr
am 3:I
mpl
ement
ati
onofConnect
ion-
LessSer
viceusi
ngst
andar
dpor
ts
v
. EchoSer
vice(
7)
v
i. Dat
eandTi
meSer
vice(
13)
v
ii
. Char
act
ergener
ati
on(
19)
Pr
ogr
am Descr
ipt
ion:
TheSt
andar
dpor
tnumber
sar
ethepor
tnumber
sthatar
ereser
vedf
orassi
gnmentf
or
usebyt
heappl
i
cat
ionendpoi
ntst
hatcommuni
cat
eusi
ngt
heI
nter
net
'sTr
ansmi
ssi
on
Cont
rolPr
otocol(
TCP)ort
heUserDat
agr
am Pr
otocol(
UDP)
.Eachki
ndofappl
i
cat
ion
hasadesi
gnat
ed(
andt
hus"
wel
l
-known"
)por
tnumber
.
I
nor
dert
oimpl
ementt
hest
andar
dpor
tsweneedt
ocr
eat
eanappl
i
cat
ionf
ori
nst
ance
saycl
i
ent
,whi
chi
sgoi
ngt
oinv
okeser
vicewhi
chi
sest
abl
i
shedont
hest
andar
dpor
ts.
TheCl
i
entwi
l
lbecr
eat
ingi
tssocketendpoi
ntandest
abl
i
shaconnect
ionwi
tht
he
st
andar
dser
verbyspeci
fi
yi
ngt
hepor
tnumberwhi
chhast
hedef
inedser
vice,f
or
i
nst
ance7f
orechoser
vice.
Connect
ionl
essI
mpl
ement
ati
on
Cl
ient
:
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaUDPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
toft
heSer
ver
,wher
eiti
spr
ovi
dingser
vice
Forechoser
ver
,sendamessaget
otheser
vert ngsendt
obeechoedusi o()sy
stem cal
l
.
Recei
vet
her
esul
toft
her
equestmadet
otheser
v ngr
erusi ecv
from(
)sy
stem cal
l
.
Wr
it
ether
esul
tthusobt
ainedont
hest
andar
dout
put
.
Val
idat
ion:
I
nput
:Cl
i
entsendsamessaget
hatwi
l
lbeechoedbyt
heSer
ver
,say“
Hel
l
o”.
Out
put
:Ser
verechoest
hemessagebackt
othecl
i
enti
.e“
Hel
l
o”
Pr
ogr
am 4:I
mpl
ement
ati
onofConnect
ion-
Ori
ent
edI
ter
ati
veEcho-
Ser
ver
,dat
eand
t
ime,
char
act
ergener
ati
onusi
nguser
-def
inedpor
ts
Pr
obl
em Def
ini
ti
on:Ani
ter
ati
veser
verknowsaheadoft
imeabouthowl
ongi
ttakest
o
handl
eeachr
equest&ser
verpr
ocesshandl
eseachr
equesti
tsel
f.
Pr
obl
em Descr
ipt
ion:I
nor
dert
oimpl
ementt
heI
ter
ati
veSer
viceweneedt
ocr
eat
ean
appl
i
cat
ionf
ori
nst
ancesaycl
i
ent
,whi
chwi
l
lbei
nvoki
ngser
vicewhi
chi
sest
abl
i
shedon
t
heI
ter
ati
veser
verwor
kingonauser
-def
inedpor
t.TheCl
i
entwi
l
lbecr
eat
ingi
tssocket
endpoi
ntandest
abl
i
shaconnect
ionwi
tht
heI
ter
ati
veser
verbyspeci
fi
yi
ngt
hepor
t
numbersi
mil
art
othatoft
heSer
ver
STEPS:
a)Connect
ionOr
ient
ed
I
mpl
ement
ati
onSer
ver
:
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
twher
etheser
vicewi
l
lbedef
inedt
obeusedbycl
i
ent
.
Bi
ndt
headdr
essandpor ngbi
tusi nd(
)sy
stem cal
l
.
Ser
v esl
erexecut i
sten(
)sy
stem cal
ltoi
ndi
cat
eit
swi
l
li
ngnesst
orecei
veconnect
ions.
Acceptt
henextcompl
etedconnect
ionf
rom t
hecl
i
entpr nganaccept
ocessbyusi ()
sy
stem cal
l
.
Recei
veamessagef
rom t
heCl
i ngr
entusi ecv
()sy
stem cal
l
.
Sendt
her
esul
toft
her
equestmadebyt
hecl
i ngsend(
entusi )sy
stem cal
l
.
Cl
ient
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
toft
heSer
ver
,wher
eiti
spr
ovi
dingser
vice
Est
abl
i
shconnect
iont
otheSer
v ngconnect
erusi ()sy
stem cal
l
.
Forechoser
ver
,sendamessaget
otheser
vert ngsend(
obeechoedusi )sy
stem cal
l
.
Recei
vet
her
esul
toft
her
equestmadet
otheser
v ngr
erusi ecv
()sy
stem cal
l
.
Wr
it
ether
esul
tthusobt
ainedont
hest
andar
dout
put
.
FLOW-
CHART
Execut
ionPr
ocedur
e:Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
& $cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:Cl
i
entsendsamessaget
hatwi
l
lbeechoedbyt
heSer
ver
,say“
Hel
l
o”.
Sampl
eOut
put
:Ser
verechoest
hemessagebackt
othecl
i
enti
.e“
Hel
l
o”
Pr
ogr
am 5:I
mpl
ement
ati
onofConnect
ionl
essI
ter
ati
veEcho-
ser
ver
,dat
eandt
ime,
char
act
ergener
ati
onusi
nguser
-def
inedpor
ts.
Pr
obl
em Def
ini
ti
on:
Ani
ter
ati
veser
verknowsaheadoft
imeabouthowl
ongi
ttakest
ohandl
eeachr
equest
&ser
verpr
ocesshandl
eseachr
equesti
tsel
f.
Pr
obl
em Descr
ipt
ion:
I
nor
dert
oimpl
ementt
heI
ter
ati
veSer
viceweneedt
ocr
eat
eanappl
i
cat
ionf
ori
nst
ance
saycl
i
ent
,whi
chwi
l
lbei
nvoki
ngser
vicewhi
chi
sest
abl
i
shedont
heI
ter
ati
veser
ver
wor
kingonauser
-def
inedpor
t.TheCl
i
entwi
l
lbecr
eat
ingi
tssocketendpoi
ntand
est
abl
i
shaconnect
ionwi
tht
heI
ter
ati
veser
verbyspeci
fyi
ngt
hepor
tnumbersi
mil
art
o
t
hatoft
heSer
ver
Connect
ion l
ess
I
mpl
ement
ati
onSer
ver
:
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaUDPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
twher
etheser
vicewi
l
lbedef
inedt
obeusedbycl
i
ent
.
Bi
ndt
headdr
essandpor ngbi
tusi nd(
)sy
stem cal
l
.
Recei
veamessagef
rom t
heCl
i ngr
entusi ecv
from(
)sy
stem cal
l
.
Sendt
her
esul
toft
her
equestmadebyt
hecl
i ngsendt
entusi o()sy
stem cal
l
.
Cl
ient
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaUDPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
toft
heSer
ver
,wher
eiti
spr
ovi
dingser
vice
Forechoser
ver
,sendamessaget
otheser
vert ngsendt
obeechoedusi o()sy
stem cal
l
.
Recei
vet
her
esul
toft
her
equestmadet
otheser
v ngr
erusi ecv
from(
)sy
stem cal
l
.
Wr
it
ether
esul
tthusobt
ainedont
hest
andar
dout
put
.
FLOW CHART
Execut
ionPr
ocedur
e:
Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj
$.
/obj
&
$cccl
i
ent
.c
$.
/a.
out
Val
idat
ion:
Sampl
eInput
:Cl
i
entsendsamessaget
hatwi
l
lbeechoedbyt
heSer
ver
,say“
Hel
l
o”.
Sampl
eOut
put
:Ser
verechoest
hemessagebackt
othecl
i
enti
.e“
Hel
l
o”
Pr
ogr
am 6:I
mpl
ement
ati
onofConnect
ion-
Ori
ent
edConcur
rentEcho-
ser
ver
,dat
eand
t
ime,
char
act
ergener
ati
onusi
nguser
-def
inedpor
ts
Pr
obl
em def
ini
ti
on:Theamountofwor
krequi
redt
ohandl
ear
equesti
sunknown,
sot
he
ser
verst
art
sanot
herpr
ocesst
ohandl
eeachr
equest
.
Pr
obl
em Descr
ipt
ion:
Inor
dert
oimpl
ementt
heI
ter
ati
veSer
viceweneedt
ocr
eat
ean
appl
i
cat
ionf
ori
nst
ancesaycl
i
ent
,whi
chwi
l
lbei
nvoki
ngser
vicewhi
chi
sest
abl
i
shedon
t
heI
ter
ati
veser
verwor
kingonauser
-def
inedpor
t.TheCl
i
entwi
l
lbecr
eat
ingi
tssocket
endpoi
ntandest
abl
i
shaconnect
ionwi
tht
heI
ter
ati
veser
verbyspeci
fi
yi
ngt
hepor
t
numbersi
mil
art
othatoft
heSer
ver
STEPS:
a)Connect
ion-
Ori
ent
edI
mpl
ement
ati
on:
Ser
ver
:
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
twher
etheser
vicewi
l
lbedef
inedt
obeusedbycl
i
ent
.
Bi
ndt
headdr
essandpor ngbi
tusi nd(
)sy
stem cal
l
.
Ser
v esl
erexecut i
sten(
)sy
stem cal
ltoi
ndi
cat
eit
swi
l
li
ngnesst
orecei
veconnect
ions.
Acceptt
henextcompl
etedconnect
ionf
rom t
hecl
i
entpr nganaccept
ocessbyusi ()
sy
stem cal
l
.
Cr
eat
eanewpr
ocess(
chi
l
dpr ngf
ocess)usi ork(
),t
ohandl
ethecl
i
entr
equest
.
Thepar
entpr
ocesswi
l
lbewai
ti
ngf
ornewi
ncomi
ngconnect
ions.
Recei
veamessagef
rom t
heCl
i ngr
entusi ecv
()sy
stem cal
l
.
Sendt
her
esul
toft
her
equestmadebyt
hecl
i ngsend(
entusi )sy
stem cal
l
.
Cl
ient
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
toft
heSer
ver
,wher
eiti
spr
ovi
dingser
vice
Est
abl
i
shconnect
iont
otheSer
v ngconnect
erusi ()sy
stem cal
l
.
Forechoser
ver
,sendamessaget
otheser
vert ngsend(
obeechoedusi )sy
stem cal
l
.
Recei
vet
her
esul
toft
her
equestmadet
otheser
v ngr
erusi ecv
()sy
stem cal
l
.
Wr
it
ether
esul
tthusobt
ainedont
hest
andar
dout
put
.
a)Connect
ion-
lessI
mpl
ement
ati
on:
Ser
ver
:
Incl
udeappr opr i
ateheaderf i
les.
CreateaUDPSocket .
Fil
linthesocketaddr essst ructure(withser verinformat i
on)
Specifythepor twher et heserv i
cewi l
lbedef inedt obeusedbycl i
ent .
Bindtheaddr essandpor tusingbind()sy stem call.
Createanewpr ocess( childprocess)usi ngfor k()
, t
ohandl etheclientrequest.
Thepar entprocesswi l
l bewai ti
ngf ornewi ncomi ngconnect ions.
Receiveamessagef r
om t heCl i
entusingr ecv f
rom( )system call.
Sendt heresultoft herequestmadebyt hecl i
entusi ngsendto()sy stem call
.
Cl
ient
Incl
udeappr opr i
ateheaderf i
les.
CreateaUDPSocket .
Fil
linthesocketaddr essst ructure(withser verinformat i
on)
Specifythepor toft heSer ver,whereitispr ovidingservice
Forechoser ver, sendamessaget otheser vert obeechoedusi ngsendt o()syst
em cal
l
.
Receivetheresul tofther equestmadet otheser verusingrecvfrom( )syst
em call
.
Wr i
tetheresultt husobt ainedont hest andardout put.
Execut
ionProcedure:
Suppose,t
heserverprogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
&$ cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:Cl
i
entsendsamessaget
hatwi
l
lbeechoedbyt
heSer
ver
,say“
Hel
l
o”
.
Sampl
eOut
put
:Ser
verechoest
hemessagebackt
othecl
i
enti
.e“
Hel
l
o”
Pr
ogr
am 7:Pr
ogr
am f
orconnect
ion-
ori
ent
edI
ter
ati
veSer
vicei
nwhi
chser
verr
ever
ses
t
hest
ri
ngsentbyt
hecl
i
entandsendsi
tback
Pr
obl
em Descr
ipt
ion:
Thepr
obl
em canbei
mpl
ement
edusi
ngsocket
s.Gener
al
i
mpl
ement
ati
onst
epsar
easf
oll
ows:
St
epsi
nvol
vedi
nwr
it
ingt
heSer
verPr
ocess:
1.Cr
eat ngsocket
easocketusi ()sy
stem cal
l
..
2.Bi
ndser
ver
’saddr
essandpor ngbi
tusi nd()sy
stem cal
l
.
3.Conv
ertt
hesocketi
ntoal
i
steni ngl
ngsocketusi i
sten()sy
tem cal
l
.
4.Wai
tforcl
i
entconnect
iont
ocompl
et ngaccept
eusi ()sy
stem cal
l
.
5.Recei
vet
heCl
i
entr
equestusi
ngr
ecv
()sy
stem cal
lwhi
chconsi
stoft
henameof
t
hecommandt
hati
stobeexecut
edal
ongwi
thdat
apar
amet
ers(
ifany
)
6.Thecommandi
sint
erpr
etedandexecut
ed.
7.Onsuccessf
ulexecut
iont
her
esul
tispassedbackt
othecl
i
entbyt
heser
ver
St
epsi
nvol
vedi
nwr
it
ingt
heCl
ientPr
ocess:
Cr
1. eat
easocket
.
Fi
2. l
li
nthei
nter
netsocketaddr
essst
ruct
ure(
wit
hser
ver
i
nfor
mat
ion)
.3.
Connectt
oser
v ngconnectsy
erusi stem cal
l
.
4.
Thecl
i
entpassest
hecommandanddat
apar
amet
ers(
ifany
)to
t
heser
ver
.5.
Readt
her
esul
tsentbyt
heser
ver
,wr
it
eitt
ost
andar
d
out
put
.
6.
Closet
hesocketconnect
ion.
Execut
ionPr
ocedur
e:
Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
& $cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:
TheCl
i
entsendst
hest
ri
ng“
NPLAB”
Sampl
eOut
put
Thest
ri
ngwi
l
lgetbackasr
ever
se“
BALNP”
Pr
ogr
am 8:Pr
ogr
am f
orconnect
ion-
ori
ent
edI
ter
ati
veser
vicei
nwhi
chser
verchanges
t
hecaseoft
hest
ri
ngssentbyt
hecl
i
entandsendsback(
CaseSer
ver
).
Pr
obl
em Descr
ipt
ion:
Thepr
obl
em canbei
mpl
ement
edusi
ngsocket
s.Gener
al
i
mpl
ement
ati
onst
epsar
easf
oll
ows:
St
epsi
nvol
vedi
nwr
it
ingt
heSer
verPr
ocess:
1.Cr
eat ngsocket
easocketusi ()sy
stem cal
l
..
2.Bi
ndser
ver
’saddr
essandpor ngbi
tusi nd()sy
stem cal
l
.
3.Conv
ertt
hesocketi
ntoal
i
steni ngl
ngsocketusi i
sten()sy
tem cal
l
.
4.Wai
tforcl
i
entconnect
iont
ocompl
et ngaccept
eusi ()sy
stem cal
l
.
5.Recei
vet
heCl
i
entr
equestusi
ngr
ecv
()sy
stem cal
lwhi
chconsi
stoft
henameof
t
hecommandt
hati
stobeexecut
edal
ongwi
thdat
apar
amet
ers(
ifany
)
6.Thecommandi
sint
erpr
etedandexecut
ed.
7.Onsuccessf
ulexecut
iont
her
esul
tispassedbackt
othecl
i
entbyt
heser
ver
St
epsi
nvol
vedi
nwr
it
ingt
heCl
ientPr
ocess:
1.Cr
eat
easocket
.
2.Fi
l
lint
hei
nter
netsocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
.
3.Connectt
oser
v ngconnectsy
erusi stem cal
l
.
4.Thecl
i
entpassest
hecommandanddat
apar
amet
ers(
ifany
)tot
heser
ver
.
5.Readt
her
esul
tsentbyt
heser
ver
,wr
it
eitt
ost
andar
dout
put
.
6.Cl
oset
hesocketconnect
ion.
Execut
ionPr
ocedur
e:
Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
& $cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:
TheCl
i
entsendst
hest
ri
ng“
HEl
l
o”
Sampl
eOut
put
:Thest
ri
ngwi
l
lgetbackas“
heLLO”
Pr
ogr
am 9:Pr
ogr
am f
orConnect
ion-
Ori
ent
edI
ter
ati
veser
vicei
nwhi
chser
ver
cal
cul
atest
heNet
-sal
aryofanEmpl
oyeebasedont
hef
oll
owi
ngdet
ail
ssentbyt
he
cl
i
ent
i
)basi
c-sal
ii
)hr
aii
i
)dai
v)ptv
)epf(net
-sal
a=basi
c+hr
a+da-
pt-
epf
).
Pr
obl
em Descr
ipt
ion:
Thepr
obl
em canbei
mpl
ement
edusi
ngsocket
s.Gener
al
i
mpl
ement
ati
onst
epsar
easf
oll
ows:
St
epsi
nvol
vedi
nwr
it
ingt
heSer
verPr
ocess:
1.Cr
eat ngsocket
easocketusi ()sy
stem cal
l
..
2.Bi
ndser
ver
’saddr
essandpor ngbi
tusi nd()sy
stem cal
l
.
3.Conv
ertt
hesocketi
ntoal
i
steni ngl
ngsocketusi i
sten()sy
tem cal
l
.
4.Wai
tforcl
i
entconnect
iont
ocompl
et ngaccept
eusi ()sy
stem cal
l
.
5.Recei
vet
heCl
i
entr
equestusi
ngr
ecv
()sy
stem cal
lwhi
chconsi
stoft
henameof
t
hecommandt
hati
stobeexecut
edal
ongwi
thdat
apar
amet
ers(
ifany
)
6.Thecommandi
sint
erpr
etedandexecut
ed.
7.Onsuccessf
ulexecut
iont
her
esul
tispassedbackt
othecl
i
entbyt
heser
ver
St
epsi
nvol
vedi
nwr
it
ingt
heCl
ientPr
ocess:
1.Cr
eat
easocket
.
2.Fi
l
lint
hei
nter
netsocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
.
3.Connectt
oser
v ngconnectsy
erusi stem cal
l
.
4.Thecl
i
entpassest
hecommandanddat
apar
amet
ers(
ifany
)tot
heser
ver
.
5.Readt
her
esul
tsentbyt
heser
ver
,wr
it
eitt
ost
andar
dout
put
.
6.Cl
oset
hesocketconnect
ion.
Execut
ionPr
ocedur
e:Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
& $cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:TheCl
i
entsendst
hesal
arydet
ail
s100020003000500500100
Sampl
eOut
put
Theni
twi
l
lret
urnt
hecompl
etesal
arayaf
tercal
cul
ati
on4900
Pr
ogr
am 10:Pr
ogr
am f
orf
il
eaccessusi
ngsocket
s.
Pr
obl
em Descr
ipt
ion:
Thepr
obl
em canbei
mpl
ement
edusi
ngsocket
s.Gener
al
i
mpl
ement
ati
onst
epsar
easf
oll
ows:
St
epsi
nvol
vedi
nwr
it
ingt
heSer
verPr
ocess:
1.Cr
eat ngsocket
easocketusi ()sy
stem cal
l
..
2.Bi
ndser
ver
’saddr
essandpor ngbi
tusi nd()sy
stem cal
l
.
3.Conv
ertt
hesocketi
ntoal
i
steni ngl
ngsocketusi i
sten()sy
tem cal
l
.
4.Wai
tforcl
i
entconnect
iont
ocompl
et ngaccept
eusi ()sy
stem cal
l
.
5.Recei
vet
heCl
i
entr
equestusi
ngr
ecv
()sy
stem cal
lwhi
chconsi
stoft
henameof
t
hecommandt
hati
stobeexecut
edal
ongwi
thdat
apar
amet
ers(
ifany
)
6.Thecommandi
sint
erpr
etedandexecut
ed.
7.Onsuccessf
ulexecut
iont
her
esul
tispassedbackt
othecl
i
entbyt
heser
ver
St
epsi
nvol
vedi
nwr
it
ingt
heCl
ientPr
ocess:
1.Cr
eat
easocket
.
2.Fi
l
lint
hei
nter
netsocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
.
3.Connectt
oser
v ngconnectsy
erusi stem cal
l
.
4.Thecl
i
entpassest
hecommandanddat
apar
amet
ers(
ifany
)tot
heser
ver
.
5.Readt
her
esul
tsentbyt
heser
ver
,wr
it
eitt
ost
andar
dout
put
.
6.Cl
oset
hesocketconnect
ion.
Execut
ionPr
ocedur
e:
Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
& $cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:TheCl
i
entsendsaf
il
ewhi
chi
spr
esenti
ncur
rentdi
rect
ory“
sampl
e.c”
Sampl
eOut
put
Theni
twi
l
lret
urnt
hecont
entoft
hef
il
e“wel
comet
oNpl
ab”
Pr
ogr
am 11:Pr
ogr
am f
orRemot
eCommandExecut
ionusi
ngsocket
s
Pr
obl
em Def
ini
ti
on-Remot
ecommandexecut
ioni
swhenapr
ocessonahostcausesa
pr
ogr
am t
obeexecut
edonanot
herhost
.Usual
l
ythei
nvoki
ngpr
ocesswant
stopass
dat
atot
her
emot
epr
ogr
am capt
urei
tsout
putal
so.
Pr
obl
em Descr
ipt
ion:The pr
obl
em can be i
mpl
ement
ed usi
ng socket
s.Gener
al
i
mpl
ement
ati
onst
epsar
easf
oll
ows:
St
epsi
nvol
vedi
nwr
it
ingt
heSer
verPr
ocess:
8.Cr
eat ngsocket
easocketusi ()sy
stem cal
l
..
9.Bi
ndser
ver
’saddr
essandpor ngbi
tusi nd()sy
stem cal
l
.
10.
Conv
ertt
hesocketi
ntoal
i
steni ngl
ngsocketusi i
sten()sy
tem cal
l
.
11.
Wai
tforcl
i
entconnect
iont
ocompl
et ngaccept
eusi ()sy
stem cal
l
.
12.
Recei
vet
heCl
i
entr
equestusi
ngr
ecv
()sy
stem cal
lwhi
chconsi
stoft
henameof
t
hecommandt
hati
stobeexecut
edal
ongwi
thdat
apar
amet
ers(
ifany
)
13.
Thecommandi
sint
erpr
etedandexecut
ed.
14.
Onsuccessf
ulexecut
iont
her
esul
tispassedbackt
othecl
i
entbyt
heser
ver
St
epsi
nvol
vedi
nwr
it
ingt
heCl
ientPr
ocess:
1.Cr
eat
easocket
.
2.Fi
l
lint
hei
nter
netsocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
.
3.Connectt
oser
v ngconnectsy
erusi stem cal
l
.
4.Thecl
i
entpassest
hecommandanddat
apar
amet
ers(
ifany
)tot
heser
ver
.
5.Readt
her
esul
tsentbyt
heser
ver
,wr
it
eitt
ost
andar
dout
put
.
6.Cl
oset
hesocketconnect
ion.
Execut
ionPr
ocedur
e:
Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj $.
/obj
& $cccl
i
ent
.c $.
/a.
out
Val
idat
ion:
Sampl
eInput
:TheCl
i
entsendst
henameoft
hecommandt
obeexecut
ed,
fori
nst
ancepwd
Sampl
eOut
put
:/home/
guest
/i
t07001/
net
wor
ks
Pr
ogr
am 12:I
mpl
ement
ati
onofDNS
Pr
obl
em Def
ini
ti
on:TheDomai
nNameSy
stem (
DNS)i
sahi
erar
chi
calnami
ngsy
stem
f
orcomput
ers,
ser
vices,
oranyr
esour
cepar
ti
cipat
ingi
ntheI
nter
net
.TheDomai
nName
Sy
stem di
str
ibut
est
her
esponsi
bil
i
tyofassi
gni
ngdomai
nnamesandmappi
ngt
hose
namest
oIPaddr
esses
Pr
obl
em Descr
ipt
ion:
TheCl
i
entpr
ogr
am sendsar
equestcont
aini
ngdomai
n-namet
otheser
ver
STEPS:
Ser
ver
:
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
twher
etheser
vicewi
l
lbedef
inedt
obeusedbycl
i
ent
.
Bi
ndt
headdr
essandpor ngbi
tusi nd(
)sy
stem cal
l
.
Ser
v esl
erexecut i
sten(
)sy
stem cal
ltoi
ndi
cat
eit
swi
l
li
ngnesst
orecei
veconnect
ions.
Acceptt
henextcompl
etedconnect
ionf
rom t
hecl
i
entpr nganaccept
ocessbyusi ()
sy
stem cal
l
.
Recei
vet
her
equestf
rom t
heCl
i ngr
entusi ecv
()sy
stem cal
l
.
Fort
hedomai
n-namet
husr
ecei
vedf
rom t
heCl
i
ent
,obt
aint
hecor
respondi
ngI
P
addr
essusi
ngappr
opr
iat
elogi
c.
Sendther
esul
t(i
nthebuf
fer
)oft
her
equestmadebyt
hecl
i ngsend(
entusi )sy
stem
cal
l.
Cl
ient
I
ncl
udeappr
opr
iat
eheaderf
il
es.
Cr
eat
eaTCPSocket
.
Fi
l
lint
hesocketaddr
essst
ruct
ure(
wit
hser
veri
nfor
mat
ion)
Speci
fyt
hepor
toft
heSer
ver
,wher
eiti
spr
ovi
dingser
vice
Toobt
aint
heI
Paddr
essf
ort
hedomai
nname.Sendr
equestt
otheser
ver
consi
sti
ngoft
hedomai
n- ngsend(
nameusi )sy
stem cal
l
.
Recei
vet
her
esul
toft
her
equestmadet
otheser
v ngr
erusi ecv
()sy
stem cal
l
.
Wr
it
ether
esul
tthusobt
ainedont
hest
andar
dout
put
.
Execut
ionPr
ocedur
e:
Suppose,
theser
verpr
ogr
am i
sser
ver
.candcl
i
entpr
ogr
am i
s
cl
i
ent
.cFi
rstcompi
l
etheSer
verpr
ogr
am as,
$ccser
ver
.c–oobj
$.
/obj
&
$cccl
i
ent
.c
$.
/a.
out
Val
idat
ion:
Sampl
eInput
:
Cl
i
entsendsDomai
n-nameaski
ngf
ort
heI
P
addr
essForExampl
e:ndc.
it
.edu
Sampl
eOut
put
:
Ser
verr
epl
i
esbackwi
tht
heI
PAddr
esst
hatcor
respondst
othe
domai
nname.Thecor
respondi
ngI
PAddr
esswi
l
lbegener
ated,
forex:
192.
100.
100.
6