You are on page 1of 41

1.

Ai
m:-Desi
gnandi
mpl
ementacl
i
ent
-ser
vercommuni
cat
ionbysendi
ngmessagest
hrough
socket
susi
ngTCPsocketpr
ogr
ammi
ng.

Descr
ipt
ion:
-

TCPi
ssai
dtobeconnect
ion-
ori
ent
edbecausebef
oreoneappl
i
cat
ionpr
ocesscanbegi
nto
senddat
atoanot
her
,thet
wopr
ocessesmustf
ir
st“
handshake”wi
theachot
her
—thati
s,t
hey
mustsendsomepr
eli
minar
ysegment
stoeachot
hert
oest
abl
i
sht
hepar
amet
ersoft
heensui
ng
dat
atr
ansf
er.

SocketPr
ogr
ammi
ngwi
thTCP

The pr
ocesses r
unni
ng on di
ff
erentmachi
nes communi
cat
e wi
th each ot
herbysendi
ng
messagesi
ntosocket
s.Thesocketi
sthedoorbet
weent
heappl
i
cat
ionpr
ocessandTCP.The
appl
i
cat
ion dev
eloperhas cont
rolofev
ery
thi
ng on t
he appl
i
cat
ion-
lay
ersi
de oft
he socket
;
howev
er,
ithasl
i
ttl
econt
rol
oft
het
ranspor
t-
lay
ersi
de.

Asi
mpl
ecl
i
ent
-ser
verappl
i
cat
iont
odemonst
rat
esocketpr
ogr
ammi
ngf
orbot
hTCPandUDP:

 Acl
i
entr
eadsal
i
nef
rom i
tsst
andar
dinput(
key
boar
d)andsendst
hel
i
neouti
tssockett
o
t
heser
ver
.
 Theser
verr
eadsal
i
nef
rom i
tsconnect
ionsocket
.
 Theser
verconv
ert
sthel
i
net
oupper
case.
 Theser
versendst
hemodi
fi
edl
i
neouti
tsconnect
ionsockett
othecl
i
ent
.
 Thecl
i
entr
eadst
hemodi
fi
edl
i
nef
rom i
tssocketandpr
int
sthel
i
neoni
tsst
andar
dout
put
(
moni
tor
).
Fi
g:-Thecl
i
ent
-ser
verappl
i
cat
ion,
usi
ngconnect
ion-
ori
ent
edt
ranspor
tser
vices
Fi
g:-TCPcl
i
enthast
hreest
reamst
hroughwhi
chchar
act
ersf
low

Pr
ogr
am:
-

TCPCl
i
ent
.j
ava
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;
cl
assTCPCl
i
ent
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gv[
])t
hrowsExcept
ion
{
St
ri
ngsent
ence;
St
ri
ngmodi
fi
edSent
ence;
Buf
fer
edReaderi
nFr
omUser=newBuf
fer
edReader
(newI
nput
Str
eamReader
(Sy
stem.
in)
);
Socketcl
i
ent
Socket=newSocket
("
local
host
",6789)
;
Dat
aOut
put
Str
eam out
ToSer
ver=new
Dat
aOut
put
Str
eam(
cli
ent
Socket
.get
Out
put
Str
eam(
));
Buf
fer
edReaderi
nFr
omSer
ver=newBuf
fer
edReader
(new
I
nput
Str
eamReader
(cl
i
ent
Socket
.get
Input
Str
eam(
)))
;
sent
ence=i
nFr
omUser
.r
eadLi
ne(
);
out
ToSer
ver
.wr
it
eBy
tes(
sent
ence+'
\n'
);
modi
fi
edSent
ence=i
nFr
omSer
ver
.r
eadLi
ne(
);
Sy
stem.
out
.pr
int
ln(
"FROM SERVER:
"+modi
fi
edSent
ence)
;
cl
i
ent
Socket
.cl
ose(
);
}
}
TCPSer
ver
.j
ava
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;
cl
assTCPSer
ver
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gv[
])t
hrowsExcept
ion
{
St
ri
ngcl
i
ent
Sent
ence;
St
ri
ngcapi
tal
i
zedSent
ence;
Ser
ver
Socketwel
comeSocket=newSer
ver
Socket
(6789)
;

whi
l
e(t
rue)
{
Socketconnect
ionSocket=wel
comeSocket
.accept
();
Buf
fer
edReaderi
nFr
omCl
i
ent=
newBuf
fer
edReader
(new
I
nput
Str
eamReader
(connect
ionSocket
.get
Input
Str
eam(
)))
;
Dat
aOut
put
Str
eam out
ToCl
i
ent=new
Dat
aOut
put
Str
eam(
connect
ionSocket
.get
Out
put
Str
eam(
));
cl
i
ent
Sent
ence=i
nFr
omCl
i
ent
.r
eadLi
ne(
);
Sy
stem.
out
.pr
int
ln(
"Recei
ved:
"+cl
i
ent
Sent
ence)
;
capi
tal
i
zedSent
ence=cl
i
ent
Sent
ence.
toUpper
Case(
)+'
\n'
;
out
ToCl
i
ent
.wr
it
eBy
tes(
capi
tal
i
zedSent
ence)
;
Sy
stem.
out
.pr
int
ln(
"Modi
fi
edMessagedpassedt
ocl
i
ent
");
}
}
}

2.Ai
m:-Desi
gnandi
mpl
ementacl
i
ent
-ser
vercommuni
cat
ionbysendi
ngmessagest
hrough
socket
susi
ngUDPsocketpr
ogr
ammi
ng.

Descr
ipt
ion:
-

UDPi
ssai
dtobeconnect
ionl
esspr
otocol
.DNSi
sanexampl
eofanappl
i
cat
ion-
lay
er
pr
otocol
thatt
ypi
cal
l
yusesUDP.
Manyappl
i
cat
ionsar
ebet
tersui
tedf
orUDPf
ort
hef
oll
owi
ngr
easons:
 Fi
nerappl
i
cat
ion-
lev
elcont
rolov
erwhatdat
aissent
,andwhen.
 Noconnect
ionest
abl
i
shment
.
 Noconnect
ionst
ate.
 Smal
lpacketheaderov
erhead.
SocketPr
ogr
ammi
ngwi
thUDP

Asi
mpl
ecl
i
ent
-ser
verappl
i
cat
iont
odemonst
rat
esocketpr
ogr
ammi
ngf
orbot
hTCPandUDP:

 Acl
i
entr
eadsal
i
nef
rom i
tsst
andar
dinput(
key
boar
d)andsendst
hel
i
neouti
tssockett
o
t
heser
ver
.
 Theser
verr
eadsal
i
nef
rom i
tsconnect
ionsocket
.
 Theser
verconv
ert
sthel
i
net
oupper
case.
 Theser
versendst
hemodi
fi
edl
i
neouti
tsconnect
ionsockett
othecl
i
ent
.
 Thecl
i
entr
eadst
hemodi
fi
edl
i
nef
rom i
tssocketandpr
int
sthel
i
neoni
tsst
andar
dout
put
(
moni
tor
).
Fi
g.:
-Thecl
i
ent
-ser
verappl
i
cat
ion,
usi
ngconnect
ionl
esst
ranspor
tser
vices
Fi
g.:
-UDPCl
i
enthasonest
ream;
thesocketaccept
spacket
sfr
om t
hepr
ocessanddel
i
ver
s
packet
stot
hepr
ocess.

Fi
g.:
-UDPSer
verhasnost
reams;
thesocketaccept
spacket
sfr
om t
hepr
ocessanddel
i
ver
s
packet
stot
hepr
ocess.

Pr
ogr
am/
Sour
ceCode:
-
UDPCl
i
ent
.j
ava:
-
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;
cl
assUDPCl
i
ent
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])t
hrowsExcept
ion
{
Buf
fer
edReaderi
nFr
omUser=
newBuf
fer
edReader
(newI
nput
Str
eamReader
(Sy
stem.
in)
);
Dat
agr
amSocketcl
i
ent
Socket=newDat
agr
amSocket
();
I
net
Addr
essI
PAddr
ess=I
net
Addr
ess.
get
ByName(
"l
ocal
host
");
by
te[
]sendDat
a=newby
te[
1024]
;
by
te[
]recei
veDat
a=newby
te[
1024]
;
St
ri
ngsent
ence=i
nFr
omUser
.r
eadLi
ne(
);
sendDat
a=sent
ence.
get
Byt
es(
);
Dat
agr
amPacketsendPacket=newDat
agr
amPacket
(sendDat
a,sendDat
a.l
engt
h,I
PAddr
ess,
9876)
;
cl
i
ent
Socket
.send(
sendPacket
);
Dat
agr
amPacketr
ecei
vePacket=newDat
agr
amPacket
(recei
veDat
a,r
ecei
veDat
a.l
engt
h);
cl
i
ent
Socket
.r
ecei
ve(
recei
vePacket
);
St
ri
ngmodi
fi
edSent
ence=newSt
ri
ng(
recei
vePacket
.get
Dat
a()
);
Sy
stem.
out
.pr
int
ln(
"FROM SERVER:
"+modi
fi
edSent
ence)
;
cl
i
ent
Socket
.cl
ose(
);
}
}
UDPSer
ver
.j
ava:
-
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;
cl
assUDPSer
ver
{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])t
hrowsExcept
ion
{
Dat
agr
amSocketser
ver
Socket=newDat
agr
amSocket
(9876)
;
by
te[
]recei
veDat
a=newby
te[
1024]
;
by
te[
]sendDat
a=newby
te[
1024]
;
whi
l
e(t
rue)
{
Dat
agr
amPacketr
ecei
vePacket=newDat
agr
amPacket
(recei
veDat
a,
r
ecei
veDat
a.l
engt
h);
ser
ver
Socket
.r
ecei
ve(
recei
vePacket
);
St
ri
ngsent
ence=newSt
ri
ng(r
ecei
vePacket
.get
Dat
a()
);
Sy
stem.
out
.pr
int
ln(
"RECEI
VED:
"+sent
ence)
;
I
net
Addr
essI
PAddr
ess=r
ecei
vePacket
.get
Addr
ess(
);
i
ntpor
t=r
ecei
vePacket
.get
Por
t()
;
St
ri
ngcapi
tal
i
zedSent
ence=sent
ence.
toUpper
Case(
);
sendDat
a=capi
tal
i
zedSent
ence.
get
Byt
es(
);
Dat
agr
amPacketsendPacket=
newDat
agr
amPacket
(sendDat
a,sendDat
a.l
engt
h,I
PAddr
ess,
por
t);
ser
ver
Socket
.send(
sendPacket
);
Sy
stem.
out
.pr
int
ln(
"Modi
fi
edmessagepassedt
oUDPcl
i
ent
");
}
}
}
3.Ai
m:I
mpl
ementt
hedat
ali
nkl
ayerf
rami
ngmet
hodssuchasBi
tSt
uff
ing.
Descr
ipt
ion:
-

Bi
tSt
uff
ingal
l
owsdat
afr
amest
ocont
ainanar
bit
rar
ynumberofbi
tsandal
l
owschar
act
er
codeswi
thanar
bit
rar
ynumberofbi
tsperchar
act
er.Att
hest
artandendofeachf
ramei
saf
lag
by
te consi
sti
ng oft
he speci
albi
tpat
ter
n 01111110.Whenev
ert
he sender
'sdat
ali
nkl
ayer
encount
ersf
iveconsecut
ive1si
nthedat
a,i
taut
omat
ical
l
yst
uff
sazer
obi
tint
otheout
goi
ngbi
t
st
ream.Thi
stechni
quei
scal
l
edbi
tst
uff
ing.Whent
her
ecei
verseesf
iveconsecut
ive1si
nthe
i
ncomi
ngdat
ast
ream,f
oll
owedbyazer
obi
t,i
taut
omat
ical
l
yde-
stuf
fst
he0bi
t.Theboundar
y
bet
weent
wof
ramescanbedet
ermi
nedbyl
ocat
ingt
hef
lagpat
ter
n.
Pr
ogr
am/
Sour
ceCode:
-
#i
ncl
ude<i
ost
ream>
v
oidmai
n()
{
i
nta[
15]
;
i
nti
,
j,
k,
n,
c=0,
pos=0;
cout
<<endl
<<"
Ent
ert
henumberofbi
ts"
;
ci
n>>n;
cout
<<endl
<<"
Ent
ert
hebi
ts"
;
f
or(
i=0;
i
<n;
i
++)
ci
n>>a[
i]
;
f
or(
i=0;
i
<n;
i
++)
{
i
f(
a[i
]==1)
{
c++;
i
f(
c==5)
{
pos=i
+1;
c=0;
f
or(
j=n;
j
>=pos;
j
--)
{
k=j
+1;
a[
k]=a[
j]
;
}
a[
pos]
=0;
n=n+1;
}
}
el
se
c=0;
}
cout
<<"
DATAAFTERSTUFFI
NG"
<<endl
;
cout
<<"
01111110"
;
f
or(
i=0;
i
<n;
i
++)
cout
<<a[
i]
;
cout
<<"01111110"
;
}
4.Ai
m:-I
mpl
ementt
hedat
ali
nkl
ayerf
rami
ngmet
hodssuchasChar
act
erSt
uff
ingandal
soDe-
st
uffi
t
Descr
ipt
ion:
-
I
nthi
smet
hod,eachf
ramest
art
swi
tht
heASCI
Ichar
act
ersequenceDLESTXandends
wi
tht
hesequenceDLEETX.
(wher
eDLEi
sDat
aLi
nkEscape,
STXi
sSt
artofTeXtandETXi
sEndof
TeXt
.)Thi
smet
hodov
ercomest
hedr
awbacksoft
hechar
act
ercountmet
hod.I
fthedest
inat
ion
ev
erl
osessy
nchr
oni
zat
ion,i
tonl
yhast
olookf
orDLESTXandDLEETXchar
act
ers.I
fhowev
er,
bi
nar
ydat
aisbei
ngt
ransmi
tt
edt
hent
her
eexi
stsapossi
bil
i
tyoft
hechar
act
ersDLESTXandDLE
ETXoccur
ri
ngi
nthedat
a.Si
ncet
hiscani
nter
fer
ewi
tht
hef
rami
ng,at
echni
quecal
l
edchar
act
er
st
uff
ingi
sused.Thesender
'sdat
ali
nkl
ayeri
nser
tsanASCI
IDLEchar
act
erj
ustbef
oret
heDLE
char
act
eri
nthedat
a.Ther
ecei
ver
'sdat
ali
nkl
ayerr
emov
est
hisDLEbef
oret
hisdat
aisgi
vent
o
t
henet
wor
klay
er.Howev
erchar
act
erst
uff
ingi
scl
osel
yassoci
atedwi
th8-
bitchar
act
ersandt
his
i
samaj
orhur
dlei
ntr
ansmi
tt
ingar
bit
rar
ysi
zedchar
act
ers.
Pr
ogr
am/
Sour
ceCode:
-
/
/PROGRAM FORCHARACTERSTUFFI
NG
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<st
ri
ng.
h>
#i
ncl
ude<pr
ocess.
h>
#i
ncl
ude<coni
o.h>
v
oidmai
n()
{
i
nti
=0,
j
=0,
n,
pos;
chara[
20]
,
b[50]
,
ch;
cout
<<"
ent
erst
ri
ng\
n";
ci
n>>a;
n=st
rl
en(
a);
cout
<<"
ent
erposi
ti
on\
n";
ci
n>>pos;
i
f(
pos>n)
{
cout
<<"
inv
ali
dposi
ti
on,
Ent
eragai
n:"
;
ci
n>>pos;
}
cout
<<"
ent
ert
hechar
act
er\
n";
ch=get
che(
);
b[
0]='
d'
;
b[
1]='
l
';
b[
2]='
e'
;
b[
3]='
s'
;
b[
4]='
t
';
b[
5]='
x'
;
j
=6;
whi
l
e(i
<n)
{
i
f(
i==pos-
1)
{
b[
j]
='
d'
;
b[
j+1]
='
l'
;
b[
j+2]
='
e'
;
b[
j+3]
=ch;
b[
j+4]
='
d'
;
b[
j+5]
='
l'
;
b[
j+6]
='
e'
;
j
=j+7;
}
i
f(
a[i
]=='
d'&&a[
i+1]
=='
l
'&&a[
i+2]
=='
e'
)
{
b[
j]
='
d'
;
b[
j+1]
='
l'
;
b[
j+2]
='
e'
;
j
=j+3;
}
b[
j]
=a[
i]
;
i
++;
j++;
}
b[
j]
='
d'
;
b[
j+1]
='
l'
;
b[
j+2]
='
e'
;
b[
j+3]
='
e'
;
b[
j+4]
='
t'
;
b[
j+5]
='
x'
;
b[
j+6]
='
\0'
;
cout
<<"
\nf
rameaf
terst
uff
ing:
\n"
;
cout
<<b;
}
5.Ai
m:-I
mpl
ementt
heer
rorcor
rect
ingcodeCy
cli
cRedundancyCheck(
CRC)ofdat
ali
nkl
ayer
.
Descr
ipt
ion:
-
CRC(
Cycl
i
cRedundancyCheck)
I
tist
hemostpower
fulandeasyt
oimpl
ementt
echni
que.Unl
i
kechecksum scheme,whi
ch
i
sbasedonaddi
ti
on,
CRCi
sbasedonbi
nar
ydi
vi
sion.I
nCRC,
asequenceofr
edundantbi
ts,
cal
l
ed
cy
cli
credundancycheckbi
ts,ar
eappendedt
otheendofdat
auni
tsot
hatt
her
esul
ti
ngdat
auni
t
becomesexact
lydi
vi
si
blebyasecond,pr
edet
ermi
nedbi
nar
ynumber
.Att
hedest
inat
ion,t
he
i
ncomi
ngdat
auni
tisdi
vi
dedbyt
hesamenumber
.Ifatt
hisst
ept
her
eisnor
emai
nder
,thedat
a
uni
tisassumedt
obecor
rectandi
sther
efor
eaccept
ed.Ar
emai
nderi
ndi
cat
est
hatt
hedat
auni
t
hasbeendamagedi
ntr
ansi
tandt
her
efor
emustber
eject
ed.
1.Bi
tst
ri
ngsar
ecr
eat
edasr
epr
esent
ati
onofpol
ynomi
alswi
thcoef
fi
cient
s‘0’
and‘
1’
onl
y.
xk-1 t 0
2.Ak-
bitf
ramei
sregar
dedascoef
fi
cient
sli
stf
orapol
ynomi
alwi
th‘
k’t
erms( ox )
x5+x4+x0 =110001
Eg:
Whent
hismet
hodi
sused,t
hesenderandt
her
ecei
vershoul
dagr
eeuponagener
ator
pol
ynomi
al,
G(x)i
nadv
ance.
Bot
hthehi
ghandl
owor
derbi
tsofG(
x)mustbe‘
1’
Tocomput
echecksum f
orsomef
ramewi
th‘
m’bi
ts(
pol
ynomi
al=M(
x),
append‘
r’zer
obi
ts
t
othel
owerendoft
hef
rame(
r=degr
eeoft
hegener
atorpol
ynomi
al)sot
hatt
hischecksummed
f
ramei
sdi
vi
si
blebyG(
x).
Di
vi
deM(
x)byG(
x)usi
ngmodul
o-2di
vi
si
onandsubt
ractt
her
emai
nderf
rom M(
x)usi
ng
modul
o-2subt
ract
ion.Lett
her
esul
tantbecal
l
edasT(
x).
T(
x)i
spassedt
other
ecei
verandt
her
ecei
verdi
vi
desi
tbyG(
x).I
fther
eisar
emai
nder
,
t
her
ehasbeenat
ransmi
ssi
oner
ror
.
Al
gor
it
hm f
orcomput
ingchecksum:
1.Let‘
r’
bet
hedegr
eeofG(
x).Append‘
r’
tot
hel
owerendoft
hef
ramesot
hati
tcont
ains(
m+
r
)bi
ts.
2.Di
vi
deM(
x)byG(
x)usi
ngMOD-
2di
vi
sion.
3.Subt
ractt
her
emai
nderf
rom M(
x)usi
ngMOD-
2subt
ract
ion.
4.Ther
esul
tist
hechecksummedf
ramet
obet
ransmi
tt
ed.
Eg: f
rame=1101011011
x)=x4+x+1=10011
G(
èdegr
ee=4
Ther
efor
e,f
rame=1101011011+0000
èM(
x)=11010110110000
Commonl
yuseddi
vi
sorpol
ynomi
alsar
e:
x12+x11+x3+x2+x+1
CRC12:
x16+x15+x2+1
CRC16:
CRCCCI x16+x12+x5+1
TT:
Per
for
mance:
CRCi
sav
eryef
fect
iveer
rordet
ect
iont
echni
que.I
fthedi
vi
sori
schosenaccor
dingt
othe
pr
evi
ousl
yment
ionedr
ules,
itsper
for
mancecanbesummar
izedasf
oll
ows:
 CRCcandet
ectal
lsi
ngl
e-bi
ter
ror
s
 CRCcandet
ectal
ldoubl
e-bi
ter
ror
s(t
hree1’
s)
 CRCcandet
ectanyoddnumberofer
ror
s(X+1)
 CRCcandet
ectal
lbur
ster
ror
sofl
esst
hant
hedegr
eeoft
hepol
ynomi
al.
 CRCdet
ect
smostoft
hel
argerbur
ster
ror
swi
thahi
ghpr
obabi
l
ity
.
 Forexampl
eCRC-
12det
ect
s99.
97%ofer
ror
swi
thal
engt
h12ormor
e.
Pr
ogr
am/
Sour
ceCode:
-
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<st
ri
ng.
h>
#i
ncl
ude<st
dli
b.h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<st
dio.
h>
i
ntmai
n()
{
charr
em[
50]
,
a[50]
,
s[50]
,
c,msg[
50]
;
chargen[
]="
10001000000000101"
;
i
nti
,
genl
en,
t,
j
,f
lag=0,
k,
n;
cout
<<"
\nGener
atorpol
ynomi
ali
sCRC-
CCI
TT:
"<<gen;
genl
en=st
rl
en(
gen)
;
k=genl
en-
1;
cout
<<"
\nEnt
ert
hemessage:
";
n=0;
whi
l
e((
c=get
char
())
!='
\n'
)
{
msg[
n]=c;
n++;
}
msg[
n]='
\0'
;
f
or(
i=0;
i
<n;
i
++)
a[
i]
=msg[
i]
;
f
or(
i=0;
i
<k;
i
++)
a[
n+i
]='
0'
;
a[
n+k]
='
\0'
;
cout
<<"
\nMessagepol
ynomi
alappendedwi
thzer
o's:
";
put
s(a)
;
f
or(
i=0;
i
<n;
i
++)
{
i
f(
a[i
]=='
1'
)
{
t
=i;
f
or(
j=0;
j
<=k;
j
++)
{
i
f(
a[t
]==gen[
j]
)
a[
t]
='
0'
;
el
se
a[
t]
='
1'
;
t
++;
}
}
}
f
or(
i=0;
i
<k;
i
++)
r
em[
i]
=a[
n+i
];
r
em[
k]='
\0'
;
cout
<<"
\nThechecksum appended:
";
put
s(r
em)
;
cout
<<"
\nMessagewi
thchecksum appended:
";
f
or(
i=0;
i
<n;
i
++)
a[
i]
=msg[
i]
;
f
or(
i=0;
i
<k;
i
++)
a[
n+i
]=r
em[
i]
;
a[
n+k]
='
\0'
;
put
s(a)
;
n=0;
cout
<<"
\nEnt
ert
her
ecei
vedpol
ynomi
al:
";
whi
l
e((
c=get
char
())
!='
\n'
)
{
s[
n]=c;
n++;
}
s[
n]='
\0'
;
f
or(
i=0;
i
<n;
i
++)
{
i
f(
s[i
]=='
1'
)
{
t
=i;
f
or(
j=0;
j
<=k;
j
++,
t++)
{
i
f(
s[t
]==gen[
j]
)
s[
t]
='
0'
;
el
se
s[
t]
='
1'
;
}
}
}
f
or(
i=0;
i
<k;
i
++)
r
em[
i]
=s[
n+i
];
r
em[
k]='
\0'
;
f
or(
i=0;
i
<k;
i
++)
{
i
f(
rem[
i]
=='
1'
)
f
lag=1;
}
i
f(
fl
ag==0)
cout
<<"
\nTher
ecei
vedpol
ynomi
ali
ser
rorf
ree\
n";
el
se
cout
<<"
\nTher
ecei
vedpol
ynomi
alhaser
ror
\n"
;
get
ch(
);
r
etur
n0;
}
/
*Out
Put
:-
Gener
atorpol
ynomi
ali
sCRC-
CCI
TT:
10001000000000101
Ent
ert
hemessage:
101
Messagepol
ynomi
alappendedwi
thzer
o's:
1010000000000000000
Thechecksum appended:
0101000000010001
Messagewi
thchecksum appended:
1010101000000010001
Ent
ert
her
ecei
vedpol
ynomi
al:
1010101000000010001
Ther
ecei
vedpol
ynomi
ali
ser
rorf
ree *
/
6.Ai
m:-Desi
gnanddev
elopapr
ogr
am t
oimpl
ementGoBackNpr
otocol
.

Descr
ipt
ion:
-/*SeeNot
es/
Text
book/
Onl
i
ne*
/

Sour
ceCode:
-
/
*Ser
verPr
ogr
am*
/

i
mpor
tjav
a.net
.*;
i
mpor tjava.i
o.*;
i
mpor tjava.ut
il.
*;
publi
ccl assGBNSer v er
{
publicstaticv oidmai n( Stri
ngar gs[])t hrowsExcept ion
{
Ser verSocketser ver =newSer verSocket (6262) ;
Sy stem. out .print l
n( "
Ser verest abl ished.");
Socketcl i
ent =ser ver.accept ();
Obj ectOut putSt ream oos=newObj ectOutputStr
eam( cl
ient.
getOutputSt
ream( )
);
Obj ectInput Stream oi s=newObj ect I
nputStream(cli
ent.get
InputStr
eam());
Sy stem. out .print l
n( "
Cl ienti snowconnect ed.");
intx=( I
nt eger )oi s.readObj ect();
intk=( I
nt eger )oi s.readObj ect();
intj=0;
inti=(Integer )
oi s.readObj ect( )
;
bool eanf lag=t rue;
Random r =newRandom( 6) ;
intmod=r .next Int(6);
whi le(
mod==1| |mod==0)
mod=r .next Int(6) ;
whi le(
true)
{
intc=k;
for (inth=0; h<=x; h++)
{
Sy stem. out .print("|
"+c+" |
")
;
c=( c+1) %x;
}
Sy stem. out .pr i
nt ln();
Sy stem. out .pr i
nt ln();
if(k==j )
{
Sy stem. out .printl
n( "
Fr ame" +k+"recieved"+"
\n"+"Data:
"+j
);
j
++;
Sy stem. out .printl
n( );
}
el se
Sy stem. out .printl
n( "
Fr ames r eci
eved not i n cor rect order"
+"\
n"+"
Expectedf r
ame: "+j +"\ n"+"Reci ev edf rameno: "
+k);
Sy stem. out .pr i
nt ln();
if(j%mod==0&&f lag)
{
Sy stem. out .printl
n( "
Er rorfound.Acknowl edgementnotsent .")
;
fl
ag=! fl
ag;
j
--;
}
el sei f(k==j -1)
{
oos. wr iteObj ect(k);
Sy stem. out .printl
n( "
Acknowl edgementsent ");
}
System. out.
pri
ntl
n();
i
f(j%mod==0)
fl
ag=!f l
ag;
k=(Integer)
ois.
readObject
();
i
f(k==- 1)
break;
i
=(Integer)oi
s.r
eadObject(
);
}
Syst
em.out
.print
ln(
"Cl
i
entf
ini
shedsendi
ngdat
a.Exi
ti
ng"
);
oos.
wri
teObject(
-1)
;
}
}
/
*Cl
i
entPr
ogr
am*
/

i
mportjava.uti
l.
*;
i
mportjava.net.
* ;
i
mportjava.i
o.*;
publ
i
ccl assGBNCl ient
{
publicstat i
cv oi dmai n(St ri
ngar gs[])throwsExcept ion
{
Buf feredReaderbr =newBuf feredReader (newI nputStreamReader (Sy
stem.in)
);
Sy stem. out .
pr i
nt( "Entert hev alueofm : ");
intm=I nt eger .
par seInt(br.readLine( ))
;
intx=( int )((
Mat h.pow( 2,m) )-
1);
Sy stem. out .
pr i
nt( "Enterno.off ramest obesent :
")
;
intcount =Integer .par seInt(br.r
eadLi ne())
;
intdat a[ ]=newi nt [count];
inth=0;
for( i
nti=0; i<count ;i++)
{
Sy stem. out .pri
nt("Enterdat af orframeno"+h+"=>" );
dat a[i]
=I nteger .parseInt(br.readLine())
;
h=( h+1) %x;
}
Socketcl ient=newSocket ("l
ocalhost ",
6262) ;
Obj ectInput Stream oi s=newObj ect I
nputStream( cl
ient.get
InputSt
ream());
Obj ectOut put Stream oos=newObj ectOutputStream( cli
ent.
getOutputSt
ream());
Sy stem. out .
pr i
ntln( "Connect edwi thserver.")
;
bool eanf l
ag=f alse;
GoBackNLi stenerl i
stener =newGoBackNLi st
ener (
ois,x);
li
st ener =newGoBackNLi st ener(ois,x);
li
st ener .
t .
star t();
intst r
t=0;
h=0;
oos. wr i
t eObj ect (x) ;
do
{
i
ntc=h;
for (i
nti =h;i<count ;i++)
{
Sy st em. out.pri
nt("|
"+c+" |
");
c=( c+1) %x;
}
Sy stem. out.pr i
ntln(
);
Sy stem. out.pr i
ntln(
);
h=st rt;
for( i
nti =strt;i
<x;i++)
{
Sy stem. out .
print
ln("
Sendingfr
ame:"
+h);
h=( h+1) %x;
Sy stem. out .
print
ln()
;
oos. wr it
eObj ect(
i);
oos. wr it
eObj ect(
data[i
]);
Thr ead. sl
eep( 100);
}
li
st ener .t.
join(3500) ;
if(l
istener .reply!=x-1)
{
Sy stem. out .
print
ln("
Nor eplyfr
om serverin3.5seconds.Resendi
ng
dat
afrom frameno"+( l
i
st ener .reply+1));
Sy stem. out .
print
ln()
;
st r
t=listener.repl
y+1;
flag=f alse;
}
else
{
Sy stem. out .
print
ln("
Allel
ementssentsuccessf
ull
y.Exi
ti
ng"
);
flag=t rue;
}
}while(
!fl
ag) ;
oos.writeObj ect (-
1) ;
}
}
i
mportjava.util
.*;
i
mportjava.net .
*;
i
mportjava.io.*;
cl
assGoBackNLi steneri mpl ement sRunnabl e
{
Threadt ;
Object I
nput Str
eam oi s;
intreply,x;
GoBackNLi stener (
Obj ectInput Str
eam o, i
nti
)
{
t=newThr ead( this);
ois=o;
reply=-
2;
x=i;
}
@Ov erri
de
publicv oidrun(){
try
{
i
ntt emp=0;
whi
l
e(r
eply!
=-1)
{
reply=(
Int
eger)ois.
readObject
();
if
(reply
!=-
1&&r epl
y!=temp+1)
reply=t
emp;
if
(reply
!=-
1)
{
temp=reply;
System.out.
pr i
ntl
n("
Acknowl
edgementoff
rameno"+(
repl
y%x)+"
r
eci
eved.
")
;
Sy
stem.
out
.pr
int
ln(
);
}
}
r
epl
y=t
emp;
}
cat
ch(
Except
ione)
{
Sy
stem.out
.pr
int
ln(
"Except
ion=>"+e)
;
}
}
}
Out
put
:-

7.Ai
m:-Desi
gnanddev
elopapr
ogr
am t
oSel
ect
iveRepeatpr
otocol
.

Descr
ipt
ion:
-/*SeeNot
es/
Text
book/
Onl
i
ne*
/

SourceCode: -
i
mpor tjava.
lang.System;
i
mpor tjava.
net .
*;
i
mpor tjava.
io.*;
i
mpor tjava.
mat h.*;
cl
assSWPCl ient
{
publi
cst ati
cvoi dmai n(St r
inga[ ])
{
try{
InetAddr essaddr =InetAddr ess.getByName("Local
host")
;
Sy stem.out .print
ln(addr) ;
Socketconnect i
on=newSocket (addr,
500);
Dat aOut putStream out=new
DataOutputStream(connect ion. getOut putStream( )
);
Buf fer
edI nput Str
eam i
n=new
Buffer
edInputStream( connect ion.getInputStream( ));
Buf fer
edI nput Str
eam inn=new
Buffer
edInputStream( connect ion.getInputStream( ));
Buf fer
edReaderki =newBuf feredReader(newInput
StreamReader
(System. i
n))
;
intflag=0;
Sy stem.out .print
ln("Connect ")
;
Sy stem.out .print
ln("Entertheno.off ramest oberequestedt
oserver:
" );
intc=Integer .parseInt(ki.r
eadLine());
out .
wr i
te(c) ;
out.
flush();
i
nti,
jj=0;
whil
e( j
j<c)
{
i=in.read();
Sy stem. out.
pri
ntl
n("
Receivedf
rameno."
+i)
;
Sy stem. out.
pri
ntl
n("
Sendingacknowl
edgementf
orf
rameno.
"+i
);
out .
wr it
e(i);
out .
flush();
jj++;
}
out.
flush();
i
n.cl
ose( )
;
i
nn.close( )
;
out.
close( );
System. out.pri
ntln("
Quit
ti
ng..
..
")
;
}
cat
ch(
Except
ione)
{Sy
stem.
out
.pr
int
ln(
e);
}
}
}

i
mportjava.
lang.System;
i
mportjava.
net.*;
i
mportjava.
io.*
;
cl
assSWPSer ver
{
publi
cstaticvoidmai n(Stri
ng[ ]args)
{
tr
y{
BufferedInput Stream i n;
ServerSocketSer versocket
=newSer verSocket(500);
System. out .
println("
Wai t
ingforconnect ion"
);
Socketcl ient=Ser versocket.
accept ();
System. out .
println("
Receivedr equestforsendi ngframes");
i
n=newBuf feredI nputStr
eam( cli
ent.getI
nputStream( )
);
DataOut put Str
eam out =newDat aOutputStr
eam( cl
ient.
getOutput
Stream(
));
i
ntp=i n.read();
System. out .
println("
Sending..
....
");
for
(inti=1;i<=p;++i )
{
System. out .
pr i
ntl
n("
Sendi ngFrameno. "
+i);
out.writ
e( i);
out.fl
ush( );
System. out .
pr i
ntl
n("
Wai ti
ngf orAcknowl edge");
Thread.sl eep(5000);
i
nta=i n.read();
System. out .
pr i
ntl
n("
Recei vedacknowl edgeforf r
ameno. :
"+i
+"as"
+a);
}
out.
flush( );
i
n.close( )
;
out.
close( );
cl
ient.
close(
);
Server
socket.close(
);
System.out.
printl
n("
Quit
ti
ng"
);
}
cat
ch(
IOExcepti
one){Syst
em.out
.pr
int
ln(
e);
}
cat
ch(
Int
errupt
edExcept
ione)
{}
}
}

Out
put:
-
C:
\User
s\M Damodhar
\Deskt
op>j
avacSWPCl
i
ent
.j
ava

C:\
User s\M Damodhar\Desktop>jav
aSWPCli
ent
Localhost/127.
0.0.1
Connect
Entertheno.offramest oberequestedt
oser
ver:
4
Receivedframeno. 1
Sendingacknowl edgementforframeno.
1
Receivedframeno. 2
Sendingacknowl edgementforframeno.
2
Receivedframeno. 3
Sendingacknowl edgementforframeno.
3
Receivedframeno. 4
Sendingacknowl edgementforframeno.
4
Quitt
ing..
..

C:
\User
s\M Damodhar
\Deskt
op>j
avacSWPSer
ver
.j
ava

C:\
Users\M Damodhar\Desktop>j
avaSWPServer
Waiti
ngforconnecti
on
Recei
vedrequestforsendingfr
ames
Sendi
ng..
..
..
Sendi
ngFrameno. 1
Waiti
ngforAcknowledge
Recei
vedacknowledgef orfr
ameno.:1as1
Sendi
ngFrameno. 2
Waiti
ngforAcknowledge
Recei
vedacknowledgef orfr
ameno.:2as2
Sendi
ngFrameno. 3
Waiti
ngforAcknowledge
Recei
vedacknowledgef orfr
ameno.:3as3
Sendi
ngFrameno. 4
Waiti
ngforAcknowledge
Recei
vedacknowledgef orfr
ameno.:4as4
Quit
ti
ng
8.Aim:-Designandimpl ementaprogram f
orDist
anceVect
orRout
ingal
gor
it
hm t
ofi
ndsui
tabl
e
pat
hfort
ransmi
ssi
on.

Descr
ipt
ion:
-/*SeeNot
es/
Text
book/
Onl
i
ne*
/
Sour
ceCode:
-
#include<stdl i
b. h>
#include<iost ream. h>
#def i
neNUL1000
#def i
neNODES10
structnode
{
i
ntt[NODES] [3];
};
structnoden[ NODES] ;
ty
pedefst r
uctnodeNOD;
i
ntmai n()
{
voi
di nit(int,int) ;
voi
di np( int,int);
voi
dcal ler(int ,
int )
;
voi
dop1( int ,
int,int);
voi
df ind( int ,
int )
;
i
nti,
j,x,y,no;
do{
cout <<" \nEnt erthenoofnodesr equired:"
;
cin>>no;
}whil
e( no>10| |
no<0) ;
for
(i
=0; i<no; i
++)
{
i
ni t(no, i);
i
np( no, i
) ;
}
cout<<" \nTheconf igurati
onofthenodesaf t
erini
tal
i
zat i
oni sasf ol
lows:"
;
for
(i
=0; i<no; i
++)
op1(no, i,
0) ;
for
(j
=0; j<no; j
++)
{
for(i=0; i<no; i
++)
caller (no, i)
;
}
cout<<" \nTheconf igoft henodesafterthecompoft hepat hsi
sasf ol
l
ows:";
for
(i
=0; i<no; i
++)
op1( no, i,
1);
whil
e( 1)
{
cout <<" \nEnt er0t oexi
toranyot herkeytofindt heshortestpath:
";
cin>>j ;
i
f (!
j)
break;
do{
cout <<"\nEntert
henodesbt nwhichpat histobef ound:";
cin>>x>>y ;
}whi le( (x<0||x>no)&&( y
<0||
y>no));
cout <<" \nThemostsui t
ableroutefrom node" <<x<<"to"
<<y <<"i
sasf ol
lows\
n";
fi
nd(
x,y
);
cout
<<y;
cout
<<"\
nThel
engt
hoft
heshor
testpat
hbt
nnode"
<<x<<"&"
<<y
<<"i
s"<<n[
x].
t[
y-1]
[2]
;
}
}
v
oidi
nit(intno,intx)
{
i
nti ;
for(i
=0;i<no;i++)
{
n[x].
t[i
][
1]=i;
n[x].
t[i
][
2]=999;
n[x].
t[i
][
3]=NUL;
}
n[x].
t[x]
[2]=0;
n[x].
t[x]
[3]=x;
}
v
oidi
np( intno,intx)
{
i
nti ;
cout <<"\nEnterthedi stsf rom thenodes" <<x+1<<"toot
hernode.
..
";
cout <<"\nPlsent er999i ft hereisnodi rect\n";
for(i
=0;i<no;i++)
{
i
f(i!
=x)
{
do{
cout <<"\nEnt erdisttonode" <<i
+1<<"="
;
cin>>n[ x].
t[
i]
[2];
}while(n[x].
t[i][
2]<0||
n[x].
t[i]
[2]
>999);
i
f(n[x].
t[i]
[2]!=999)
n[x].
t[i][
3]=i;
}
}
}
v
oidcaller(
intno,intx)
{
voidcompar (i
nt,
int,i
nt )
;
i
nti ;
for(i
=0;i<no;i++)
{
i
f(n[x].t
[i
][
2]!=999&&n[ x]
.t[
i]
[2]
!=0)
{
compar (x,i
,
no) ;
}
}
}
v
oidcompar (i
ntx,inty ,
i
ntno)
{
i
nti ,
z;
for(i
=0;i<no;i++)
{
z=n[
x].
t[
y][2]+n[y
].t
[i
][
2];
i
f(n[
x].
t[
i]
[2]>z)
{
n[x].t
[i]
[2]
=z;
n[x].t
[i]
[3]
=y;
}
}
}
voi
dop1( intno,intx,i
ntz)
{
i
nti,j
;
cout<<"\nTher out i
ngt ablefornodeno" <<x+1<<"isasfoll
ows" ;
cout<<"\n\n\t\t
\ t
DESTI NATION\ tDISTANCE\ tNEXT_ HOP";
for(
i=0;
i<no;i++)
{
if
((!z&&n[ x].
t[i
][2]
>=999)| |
(n[x]
.t
[i]
[2]
>=(999*no)))
cout <<"\n\t\t
\t"<<n[x]
.t[
i]
[1]
+1<<"\tNOLI NK\ tNOHOP" ;
else i f
(n[x].t
[i]
[3]==NUL)
cout <<"\n\t\t
\t"<<n[x]
.t[
i]
[1]
+1<<"\t"<<n[x]
.t
[i
][2]
<<"\tNOOP" ;
else
cout <<"\n\t\t
\t"<<n[x]
.t[
i]
[1]
+1<<"\t"<<n[x]
.t
[i
][2]
<<"\t"
<<n[x]
.t
[i
][
3]+1;
}
}
voi
df i
nd(intx,
inty )
{
i
nti,j
;
i
=x-1;
j
=y -
1;
cout<<x<<"-->"
;
i
f(n[i
].
t[
j][
3]!=j)
{
fi
nd( n[i
].
t[j
][3]+1,y);
return;
}
}
Output:
-

9.Ai
m:-Desi
gnanddev
elopapr
ogr
am f
orLi
nkSt
ateRout
ingusi
ngDi
j
kst
ra’
sAl
gor
it
hm.

Descr
ipt
ion:
-/*SeeNot
es/
Text
book/
Onl
i
ne*
/

Sour
ceCode:
-
#include<iostream>
#include<conio.h>
#include<stdio.h>
usingnamespacest d;
i
ntshor test(i
nt,int)
;
i
ntcost [10][
10] ,
dist
[20]
,
i,
j,
n,
k,
m,S[
20]
,
v,t
otcost
,pat
h[20]
,
p;
mai n()
{
i
ntc;
cout<<"Ent ernoofv ert
ices";
cin>>n;
cout<<"Ent ernoofedges" ;
cin>>m;
cout<<"\nEnt ertheedgeswi thcost:
<sour
ce><dest
inat
ion><cost
>\n"
;
for(
k=1;k<=m; k++)
{
cin>>i >>j>>c;
cost[i]
[j
]=c;
}
for(
i=1;i
<=n;i++)
for(j
=1;j<=n;
j++)
if
(cost[
i]
[j
]==0)
cost[
i]
[j
]=31999;
cout<<"enteri ni
ti
alvertex";
cin>>v;
cout<<v <<"\n";
shortest(v,
n);
}
i
ntshortest(
intv ,i
ntn)
{
i
ntmi n;
for(i
=1;i<=n; i
++)
{
S[i]
=0;
dist[i]
=cost [v
][i
];
}
path[++p] =v ;
S[v]=1;
dist[v
]=0;
for(i
=2;i<=n- 1;
i++)
{
k=-1;
mi n=31999;
for(j=1;j
<=n; j
++)
{
if(
di st
[j
]<mi n&&S[ j
]!
=1)
{
mi n=di st[j
];
k=j;
}
}
i
f(cost [v]
[k]<=dist[k]
)
p=1;
pat h[++p]=k;
for(j=1;j
<=p; j
++)
cout <<pat h[j
]<<"-->"
;
cout <<"Mi n.Costi s: "<<min;
cout<<" \n" ;
//cout<<k;
S[k]=1;
f
or(
j=1;
j
<=n;
j++)
i
f(
cost[
k][
j]
!=31999&&di st
[j
]>=dist
[k]
+cost
[k]
[j
]&&S[
j]
!=1)
dist
[j]
=dist
[k]
+cost[
k][j
];
}
}
Out
put:
-
Ent
ernoofver
tices6
Ent
ernoofedges11

Entert
heedgeswi thcost:<sour
ce><dest
inat
ion><cost
>
1250
1345
1410
2310
2415
3530
4110
4515
5220
5335
653
enteri
nit
ialvertex1
1
1--
>4--
>Min.Costi s:10
1--
>4--
>5-->Min.Costis:25
1--
>4--
>5-->2-
->Min.Costis:45
1--
>3--
>Min.Costi s:45

10.Ai
m:-Desi
gnandi
mpl
ementapr
ogr
am f
orSpanni
ngTr
eeAl
gor
it
hm t
ofi
ndLoopl
esspat
hwi
th
6t
o10Nodesf
orBr
oadcastRout
ing.

Descr
ipt
ion:
-/*SeeNot
es/
Text
book/
Onl
i
ne*
/

Sour
ceCode:
-
#incl
ude<iostream.h>
#incl
ude<coni o.
h>
#incl
ude<stdlib.
h>
#defi
neMAX20
#defi
neI NFINITY999
enum boolean{ FALSE,TRUE};
voidpri
m( i
ntc[ ]
[MAX],
intt
[MAX],
i
ntn)
;
i
ntmi ncost=0;
//i
nit
ial
izemi nimum costto0
i
ntmai n()
{
i
ntn,c[MAX][MAX] ,
t[
2*(
MAX-1)
];
i
nti,
j;
cout <<" \nTof indmi npat hspanni ngt r
ee";
cout <<" \nEnt ernoofnodes: "
;
cin>>n;
cout <<" \nEnt ert hecostadj acencymat ri
x";
for(i
=0; i<n;i
++)
for(j
=0; j<n;j++)
cin>>c[ i]
[j];
/
/ distancebet weent henodes
prim( c,t,
n) ;
/
/ algorithm t of indt hemi nimum spanni ngtree
for(i
=0; i<2*( n-1) ;
i+=2)
cout <<" \n"<<t [
i]+1<<"" <<t[
i+1]+1;
/
/ nodesoft hespanni ngt ree
cout <<" \nmi ncost ="<<mi ncost ;
return0;
}
/
/ usingpr im' sal gor ithm f orf indingshor testpath
voidpr i
m( intc[ ][MAX] ,
intt [
MAX] ,
intn)
{
i
nti ,
j;
enum bool eanv [MAX] ;
i
ntk, s,mi n,v1, v2;
for(i
=0; i<n;i
++)
v[i
]=FALSE;
v[0]=TRUE;
k=0;
t[
k]=1;
s=0;
k++;
whi l
e( k<n)
{
mi n=INFI NITY;
for(i
=0; i<n;i++)
for(j=1;j<n;j++) //whilethereisnopat
hb/wany2nodesanddi
sti
slesst
han
mi nimum
i
f(v [
i]==TRUE&&v [
j]
==FALSE&&c[
i]
[j
]<mi
n)
{
mi n=c[i]
[j
];
v1=i;
v2=j;
}
mi ncost =mi ncost +mi n;
//addt hemi ndistt ot hemi ncost
i
f(mi n==I NFI NITY)
{
cout <<"gr aphdi sconnect ed"
;
exit(0) ;
}
v[v2]=TRUE;
k++;
t[
s++] =v 1;
t[
s++] =v 2;
}
}

Out
put
:-

11.Ai
m:-Designandi
mpl ementanappl
i
cati
onforSi
mpl
emai
ltr
ansf
erpr
otocol
.
Descr
ipt
ion:
-/*SeeNotes/Text
book/
Onli
ne*/

Sour
ceCode:
-
smt
pSer
ver
.j
ava
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;

cl
asssmt pServer
{
publi
cst at
icv oi
dmain(Str
ingargs[
])t
hrowsExcept
ion
{
ServerSocketss=newSer ver
Socket(
8080)
;
Sockets=ss. accept
();
Servi
ceClient(
s);
}

publ
icst ati
cv oidSer vi
ceCli
ent(Sockets)t hrowsException
{
DataInputStream dis=nul l;
Pri
ntStream ps=nul l;
di
s=newDat aInputStr
eam (s.
getInputStream());
ps=newPr i
ntStream (s.
getOutputStream( )
);
Fi
leWr i
terf=newFi l
eWr i
ter(
"TestMail.eml");
St
ringt el
=dis.readLine()
;
i
f(t
el.
equal s("Ready "
))
System.out .pri
ntl
n( "
Readysignal Receivedfrom cl
i
ent");

ps.
print
ln(
"Ent
ertheFrom addr
ess:
")
;
Str
ingfrom =di
s.r
eadLine(
);
f.
writ
e("
From:"+f
rom+" \n"
);

ps.
print
ln(
"Ent
ertheToaddr
ess:
")
;
Str
ingto=dis.
readLi
ne()
;
f.
writ
e("
To:"+to+"\n"
);

ps.
pr i
ntl
n("
EntertheSubj ect:
")
;
Str
ingsub=di s.readLine()
;
f.
wr i
te(
"Subj
ect:"+sub+" \n"
);
ps.
pr i
ntl
n("
EntertheMessage: ");
Str
ingmsg=di s.readLine(
);
f.
wr i
te(
"\nMessage: "+msg+" \n");
f.
close()
;
}
}
smt
pCl
i
ent
.j
ava
i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;

cl
asssmt pCli
ent
{
publi
cstati
cvoi
dmai n(
Stri
ngargs[
])throwsExcept
ion
{
Sockets=newSocket
("
local
host"
,8080);

DataI
nputSt
ream di
s=newDat aInput
Stream(s.getI
nputSt
ream(
));
DataI
nputSt
ream i
n=newDat aInputStr
eam( System.i
n);
Pri
ntSt
ream ps =newPr i
ntStream( s.
getOutputStr
eam());

ps.
pri
ntl
n("
Ready
");

System.out.pri
ntl
n( di
s.r
eadLine(
));
Str
ingstrFrom=in.readLi
ne();
ps.pri
ntl
n(strFr
om) ;

System.out.
print
ln(di
s.r
eadLi
ne(
));
Str
ingstrTo=in.r
eadLi
ne();
ps.pri
ntl
n(st
rTo);

System.out.
pri
ntl
n(di
s.r
eadLi
ne(
));
Str
ingstrSub=i
n.r
eadLi
ne()
;
ps.pri
ntl
n(st
rSub)
;

Sy
stem.
out
.pr
int
ln(
dis.
readLi
ne(
));

whil
e(tr
ue)
{
Str
ingmsg=in.
readLi
ne(
);
ps.
print
ln(
msg);

i
f(
msg. equals("Quit
")
)
{
Syst
em. out.pri
ntl
n
(
"Messagei sdeliveredtoserverandcl
i
entqui
ts"
);
br
eak;
}
}
}
}

Output
:-
C:
/>jav
asmt
pSer
ver
Readysignal
Receiv
edf
rom cl
i
ent
C:/
>jav
asmt pCl
ient
EntertheFr
om address:
crathi
sh@gmail
.com
EntertheToaddress:
crathi
sh@y ahoo.co.
in
EntertheSubject:
TestMai l
EntertheMessage:
Hello,
thismailisgenerat
edaspartoft
heSMTPpr
ogr
am t
est
ing!
Quit
Messagei sdeli
veredtoserv
erandcli
entqui
ts

//Gener
atedMai
l
Test
Mail.
eml

From:dhamu@gmai l
.com
To:dhamu@y ahoo.
co.i
n
Subject
:TestMail

Message:
Hel
l
oFr
iendhowru.
..

12.Ai
m:-Designandi
mpl ementanappl
i
cati
ontot
ransf
erf
il
ebet
weencl
i
entandf
il
eser
ver
.
Descr
ipt
ion:
-/*SeeNotes/Text
book/
Onli
ne*/

Sour
ceCode:
-
Fi
l
eSer
ver
.j
ava
i
mportjava.
io.*
;
i
mportjava.
net.*;
cl
assFil
eServer
{
publi
cstaticv oi
dmai n( Str
ingar g[]
)t hrowsExcept i
on
{
Str
ingf name, f1name, opt i
on;
DataInputStream i n=newDat aInputStream( System.in);
ServerSocketss=newSer ver Socket (
6789) ;
while(t
rue)
{
Sockets=ss. accept ();
DataInput Stream i np=newDat aInputStr
eam( s.getI
nputSt
ream())
;
DataOut putSt ream out =newDat aOutputStream( s.
get
OutputSt
ream(
));
option=inp. readLi ne();
if(
option. compar eTo( "A" )
==0)
{
f1name=i np.readLi ne();
try
{
Fil
ef =newFi le(f
1name) ;
Fil
eOut put Stream fo=newFi l
eOut put
Str
eam( f
);
byteb[ ]=newby te[
400] ;
i
np. r
ead( b);
fo.writ
e( b);
fo.cl
ose( );
Sy stem. out .
pr i
nt l
n("Fi
le"+f1name+"UPLOADED! !!
!!
")
;
}
cat ch(FileNot FoundEx ceptione)
{
St
ringexcep="Sorry
..
.Fil
eNotPr
esent
";
System.
out.
pri
ntl
n(excep)
;
br
eak;
}
}
el
seif
(option.compar eTo("B")==0)
{
fname=i np. readLine( )
;
try
{
Filef =newFi l
e( fname) ;
Input Stream fi=newFi leI
nput
Str
eam( f
);
Sy st em. out.
pr i
nt ln("Accessedfi
le:"+fname)
;
intsi ze=f i
.availabl e();
by teb[ ]=newby te[size];
f i
.read( b);
f i
.close( );
out .wr i
te(b);
Sy st em. out.
pr i
nt ln("Successf
ull
ysend");
}
catch(Fil
eNot FoundEx cept i
one)
{
St ringexcep=" Sor ry.
..Fil
eNotPresent"
;
Sy st em. out.
pr i
nt ln(excep);
br eak;
}
}
el
se
{
System. out .pri
ntln(
"Ent erVal idOpti
on"
);
}

}
}
}
user
.j
ava
i
mportjava.
io.*
;
i
mportjava.
net.*;
cl
assuser
{
publi
cstaticvoi dmai n(
Stri
ngar g[
])throwsExcept ion
{
l
ongend;
Str
ingf name, f
1name;
i
ntsize=0;
Str
ingopt ion;
DataInputStream in=newDat aInputStream(Sy st
em.in)
;
Socketcs=newSocket ("
127.0.0.1"
,6789);
DataInputStream inp=newDat aInputStream( cs.
getI
nputSt
ream())
;
DataOut putStr
eam out =newDat aOut putSt
ream( cs.
getOut
putSt
ream(
));
System. out.
print
ln("
A.UPLOAD" );
System. out .
print ln("B.DOWNLOAD" );
System. out .
print ("ENTERTHEOPTI ON:")
;
option=in. readLi ne( )
;
out.writeBy tes( opt ion+"\ n");
i
f(opt i
on. compar eTo( "A")==0)
{
Sy stem. out .pri
ntln("Enterthefil
ename: ")
;
fname=i n.readLi ne();
Sy stem. out .pri
ntln("Enterthefil
enamet obesavedinSer
ver
:"
);
f1name=i n.readLine( )
;
out .
wr iteBy tes(f
1name+' \
n')
;
Filef =newFi l
e(f
name) ;
FileInput St ream f i=newFi leI
nputStream(fname);
size=f i
.av ail
able();
by teb[]=newby te[size];
fi
.read( b) ;
fi
.close( ) ;
out .
wr ite( b);
Sy stem. out .pri
ntln("UPLOADED! !
!!
!"
);
}
el
sei f(opt i
on. compar eTo( "B")==0)
{
Sy stem. out .pri
ntln("Enterthefil
ename: "
);
fname=i n.readLi ne();
Sy stem. out .pri
ntln("Enterthefil
enamet obesaved:"
);
f1name=i n.readLine( )
;
out .
wr iteBy tes(f
name+' \n')
;
by teb[]=newby te[400];
i
np. read( b) ;
Filef =newFi l
e(f
1name) ;
FileOut put Stream f o=newFi l
eOutputStr
eam(f1name);
fo.wr it
e( b) ;
fo.close( ) ;
Sy stem. out .pri
ntln("DOWNLOADED! !!
!!
")
;
}
el
se
{
Sy stem. out .pri
ntln("EnterValidOption")
;
}
cs.close( );
end=Sy stem. cur rentTimeMi l
lis()
;
}
}

13.Ai
m:-Designandi
mpl ementaprogram t
oencr
yptandde-
cry
ptpl
aint
extusi
ngDESal
gor
it
hm.
Descr
ipt
ion:
-/*SeeNotes/Text
book/Onli
ne*/

Sour
ceCode:
-
#i
ncl
ude<stdi
o.h>
#i
ncl
ude<i
ostream>
#i
ncl
ude<coni
o.h>
#incl
ude<stdli
b.h>
#incl
ude<stri
ng. h>
voidmain()
{
i
nti,
ch,lp;
charcipher[50],
plai
n[50] ;
charkey [
50];
whil
e(1)
{
cout<<"\n--
--
-MENU- -
---\n" ;
cout<<"\n1:DataEncr ypt i
on\ t\n\ n2:Dat aDecr ypti
on\
t\n\
n3:
Exi
t"
;
cout<<"\n\nEnt eryourchoi ce:" ;
cin>>ch;
swi t
ch(ch)
{
case1: cout<<"\ nDat aEncr y
pt ion" ;
cout<<"\nEnt ert hepl ai nt ext:";
ffl
ush(stdi n);
gets(plain) ;
cout<<"\nEnt ert heencr y ptionkey :
";
gets(key) ;
l
p=st rl
en( key );
for(i
=0;plai n[i
]!
='\ 0';
i++)
cipher [i
]=pl ain[i
]^lp;
cipher[
i]='\0';
cout<<"\nTheencr y ptedt exti s:"
;
puts(cipher )
;
break;
case2: cout<<"\ nDat adecr y
pt ion" ;
for(i
=0;cipher [i]
!='\0';i
++)
pl ain[i]
=ci pher [i
]^lp;
cout <<"\nDecr y ptedt extis:
";
put s(plain) ;
br eak;
case3: exit
(0);
}
}
}

Output:-
--
---
MENU- --
--
1:DataEncr ypti
on
2:DataDecr ypti
on
3:Exi
t
Enteryourchoi ce:1
DataEncr yption
Entertheplaint ext:
dhamu
Entertheencr yptionkey:sea
Theencr yptedt extis:
gkbnv
--
---
MENU- --
--
1:DataEncr ypti
on
2:DataDecr ypti
on
3:Exi
t
Enteryourchoi ce:2
Datadecr yption
Decryptedt exti s:
dhamu
--
---
MENU- ---
-
1:DataEncr yption
2:DataDecr yption
3:Exi
t
Enteryourchoi ce:3
Pressanykeyt ocont i
nue...
14.Aim:-Desi gnandi mpl ementaprogram t
oencr
yptanddecr
yptt
hedat
ausi
ngt
hepubl
i
ckey
generati
onal gor i
thm RSA.
Descripti
on:-/ *SeeNot es/Text
book/Onli
ne*/
SourceCode: -
#incl
ude<i ostream>
#incl
ude<st dio. h>
#incl
ude<mat h.h>
#incl
ude<st ring. h>
#incl
ude<st dlib.h>
usingnamespacest d;
l
ongi ntp, q,n, t,flag,e[100],
d[100]
,temp[100]
,j
,m[100],
en[
100]
,i
;
charmsg[ 100] ;
i
ntpr i
me( longi nt )
;
voidce();
l
ongi ntcd( l
ongi nt )
;
voidencry pt();
voiddecry pt();
i
ntpr i
me( longi ntpr )
{
i
nti ;
j=sqr t (pr);
for( i=2; i<=j;i++)
{
if( pr%i ==0)
ret urn0;
}
return1;
}
i
ntmai n()
{
cout<<" \nENTERFI RSTPRIMENUMBER: ";
cin>>p;
fl
ag=pr ime( p);
i
f( fl
ag==0)
{
cout<<" \nWRONGI NPUT\ n"
;
exit(1);
}
cout<<" \nENTERANOTHERPRI MENUMBER: "
;
cin>>q;
fl
ag=pr ime( q);
i
f( fl
ag==0| |p==q)
{
cout<<"\
nWRONGI
NPUT\
n";
exi
t(1)
;
}
cout<<" \
nENTERMESSAGE: "
;
ffl
ush(st
din);
cin>>msg;
for(i=0;msg[i]!
=NULL; i
++)
m[i]=msg[ i
];
n=p*q;
t=( p-1)*(q-1);
ce();
cout<<" \
nPOSSIBLEVALUESOFeANDdARE\
n";
for(i=0;i<j-1;i++)
cout<<e[i
]<<"\t
"<<d[i
]<<"
\n"
;
encrypt(
);
decrypt(
);
retur
n0;
}
v
oidce()
{
i
ntk;
k=0;
for(
i=2; i<t;
i++)
{
if(t%i==0)
cont inue;
flag=pr ime( i
);
if(fl
ag==1&&i !=p&&i !=q)
{
e[k]=i ;
flag=cd( e[k]
);
if( fl
ag>0)
{
d[k]=f l
ag;
k++;
}
if( k==99)
br eak;
}
}
}
l
ongintcd(
longintx)
{
l
ongintk=1;
whil
e(1)
{
k=k+t ;
if(k%x==0)
ret urn(k/x) ;
}
}
v
oidencrypt()
{
l
ongi ntpt ,ct, key=e[ 0],
k,len;
i=0;
l
en=st rl
en( msg) ;
while(i!=len)
{
pt=m[ i
];
pt=pt-96;
k=1;
for( j=0; j<key ;j
++)
{
k=k*pt ;
k=k%n;
}
temp[ i]=k;
ct=k+96;
en[i]=ct ;
i++;
}
en[i
]=- 1;
cout<<" \nTHEENCRYPTEDMESSAGEI S:"
;
for(i=0; en[ i
]! =-1;i
++)
pri
ntf("
%c" ,en[ i]
);
}
v
oiddecrypt()
{
l
ongi ntpt ,ct, key=d[ 0],k;
i=0;
while(en[i]!=- 1)
{
ct=t emp[ i];
k=1;
for( j=0; j<key ;j
++)
{
k=k*ct ;
k=k%n;
}
pt=k+96;
m[ i
]=pt ;
i++;
}
m[i]=-1;
cout<<" \nTHEDECRYPTEDMESSAGEI S:
";
for(i=0; m[ i]!=-1;i++)
printf("%c" ,m[i
]);
}

Out
put:
-
ENTERFIRSTPRI
MENUMBER:47
ENTERANOTHERPRIMENUMBER:53
ENTERMESSAGE:dhamu
POSSI
BLEVALUESOFeANDdARE
3 1595
5 957
7 1367
11 435
17 985
19 1259
29 165
31 463
37 1293
41 2217
43 1947
59 1419
61 549
67 2035
71 1415
73 1409
79 1847
83 2075
89 2177
97 1233
101 1421
103 2183
THEENCRYPTEDMESSAGEI
S:á`a)
\
THEDECRYPTEDMESSAGEI
S:dhamu

15.Ai
m:-Desi
gnandi
mpl
ementanappl
i
cat
ionf
orconcur
rentser
ver
.

Descr
ipt
ion:
-/*SeeNot
es/
Text
book/
Onl
i
ne*
/

Sour
ceCode:
-
Cl
i
ent
con.
jav
a
i
mportjava.
io.*
;
i
mportjava.
net.*;
cl
assClient
con
{
publi
cstaticvoi dmai n(St r
ingar g[]
)t hrowsExcept ion
{
Str
ingsent ence, newsent ence, newsent ence1;
DataInputStream i n=newDat aInput Str
eam( Syst
em.in)
;
Socketcs=newSocket ("127.0. 0.1",
6789);
DataInputStream i np=newDat aI nputStr
eam( cs.
getI
nputSt
ream())
;
DataOut putStr
eam out =newDat aOutputStr
eam( cs.
getOut
putSt
ream(
));
sentence=inp.readLi ne();
System.out.println(sentence) ;
newsent ence=in.r eadLi
ne( )
;
out.
wr i
teBytes( newsent ence+' \ n')
;
newsent ence1=i np.readLi ne();
Syst
em. out.
pri
ntl
n("
From Ser
ver
:"
+newsent
ence1)
;
cs.
close()
;
}
}

Ser
ver
.j
ava

i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.net
.*;

cl
assServer
{
publi
cstaticv oi
dmai n(
Stri
ngarg[])throwsException
{
i
ntcount =0;
Threadt =Thread.
current
Thr ead();
ServerSocketss=newSer verSocket(6789)
;
while(
true)
{
tr
y
{
Sockets=ss.accept
( )
;
count++;
conserverc=newconser ver(
s,count);
}
catch(
Except i
one){}
}
}
}

cl
assconserverimpl ementsRunnabl
e
{
Threadt ;
Sockets;
i
ntc;
conserver(Sockets1,
intcount
)
{
t=newThr ead(t
his,"
Demo"
);
t.
start(
);
s=s1;
c=count ;

}
publ
i
cvoidr
un(
)
{
t
ry
{
DataI
nputStream inp=newDat aI
nputSt
ream(s.
getI
nput
Stream())
;
DataOutputStr
eam out =newDataOutputSt
ream(s.
get
OutputSt
ream(
));
Str
ingsentence="EntertheStri
ng:"
;
Str
ingnewsent ence;
out.
writ
eBytes(sentence+'\
n')
;
newsent ence=i
np. r
eadLine(
);
//Thread.sl
eep(10000);
System.out.pri
ntln(
"Fr
om Client"
+c+"
:"
+newsent
ence)
;
out.wri
teBytes(newsentence+'\
n')
;

}
cat
ch(
Except
ione){
}
}
}

Out
put
:-

You might also like