Download - Cn Lab Programs
-
7/23/2019 Cn Lab Programs
1/36
B.Tech. (CSE)Fifth Semester
COMPUTER NETWORKS LAB
Code EURCS !"" C#te$or% CE
Credits & 'ors *er +ee, -e*#rtmet CSE
". Write a program to
a. Print the IP address of a www.yahoo.com
b. Print the url of 205.163.22.10
c. Print all the addresses of www.apple.com
d. Print the IP address of the local machinee. Print the hostname of the local machine
&.Write a program to Identify the well !nown ports on a "emote #ystem$y trying to listen to the %arious well !nown ports by opening client connections. If the
e&ception does not occur then the remote port is acti%e else the remote port is inacti%e.
. 'i%en a (")* write a program to print the parts of (").
/. Write a program to display the soc!et+s port and IP address.
!. Write a program to send , "ecei%e data from -atagramPac!et
0. Write a program for ulticast #niffer
1. Write a program for ulticast sender2.Write a program for a /hat pplication
nene $y opening soc!et connection and displaying what is written by one party to
the other.
anyany 4$road cast ach client opens a soc!et connection to the chat ser%er and
writes to the soc!et. Whate%er is written by one party can be seen by all other parties.
3. Write a program for the -ata "etrie%al from a "emote -atabaset the remote database a ser%er listens for client connections. 7his ser%er accepts #8) 9ueries
from the client* e&ecutes it on the database and sends the response to the client.
"4. Write a program for the ail /lient
PP /lient 'i%es the ser%er name * user name and password retrie%e the mails and
allow manipulation of mail bo& using PP commands.
#7P /lient 'i%es the ser%er name* send email to the recipient using #7P
commands 4/ore :a%a 2 pg163.
"". Write a program for the #imulation of 7elnet
Pro%ide a user interface to contact well!nown ports* so that clientser%er interaction can be seenby the user.
"&.Write a program for the #imple file transfer between two systems
$y opening soc!et connection to our ser%er on one system and sending a file from one system toanother.
".Write a program for the 7;7P/lient7o de%elop a 7;7P client for file transfer. 4(ni&
-
7/23/2019 Cn Lab Programs
2/36
Lab Programs
". Write a program to
#. Prit the 6P #ddress of # +++.%#hoo.com 7 +++.$it#m.ed
Pro$r#m
+++.%#hoo.com
import @a%a.net.AB
public class IPddressC
public static %oid main 4#tring argsDE
C
try
C
Inetddress addresses F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
3/36
H
Ot*t
www.gitam.edu?12.16J.23.
(OR)
Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
public class IP;inder
C
public static %oid main4#tringDE args throws I&ception
C
#tring hostB
$uffered"eader input Fnew $uffered"eader4new Input#tream"eader4#ystem.inB
#ystem.out.print4GKnKnnterhost name GB
host F input.read)ine4B
try
C
Inetddress address F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
4/36
5. Prit the r8 of &4!."0.&&."4/
Pro$r#m
import @a%a.net.AB
class (")
C
public static %oid main 4#tring argsDE
C
try
C
Inetddress address F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
5/36
Prit #88 the #ddresses of +++.$it#m.ed
Pro$r#m
import @a%a.net.AB
class llddress C
public static %oid main 4#tring argsDE C
try
C
InetddressDE address F Inetddress.getll$y
-
7/23/2019 Cn Lab Programs
6/36
IP ddress is padma@aP/?1O2.1O.2J.J
e. Prit the host#me of the 8oc#8 m#chie #d host#me
Pro$r#m
??Print the IP address of the local machine , host name
import @a%a.net.ABclass myddress1 C
public static %oid main 4#tring argsDE C
try C
?? 7o get local machine+s IP address
Inetddress address F Inetddress.get)ocal=ost4B
#ystem.out.println4GKn IP ddress is GLaddressB
?? 7o get the local machine+s name
#tring s1Faddress.get=ost
-
7/23/2019 Cn Lab Programs
7/36
#ystem.out.println4G7here is ser%er on port G LiLG of GLhostB
s.close4B
H
catch4I&ception ieC
H
H
H
H
Ot*t
7here is ser%er on port 135 of localhost
(OR)
import @a%a.io.ABimport @a%a.net.AB
public class w!portsC
public static %oid main4#tring argsDE throws &ception
CInetddress addressFInetddress.get)ocal=ost4B
#tring hostFaddress.get=ost
-
7/23/2019 Cn Lab Programs
8/36
Port 5 cti%e
(OR)
Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
public class Port#canner
C
public static %oid main4#tringDE argsC
#tring host F GlocalhostGB
if 4args.length 0
Chost F argsD0EB
H
tryC
Inetddress theddress F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
9/36
Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
public class )owPort#cannerC
public static %oid main4#tringDE args
C
#tring host F GlocalhostGBif 4args.length 0
C
host F argsD0EBH
for 4int i F 1B i M 102B iLL
Ctry
C
#oc!et s F new #oc!et4host* iB#ystem.out.println4G7here is a ser%er on port G L i L G of G L hostB
H ?? end try
catch 4(n!nown=ost&ception e&
C#ystem.err.println4e&B
brea!B
H ?? end catchcatch 4I&ception e&
C
?? must not be a ser%er on this portH ? end catch
H ?? end for
H ?? end main
H ?? end Port#canner
Ot*t
. ;i9e # URL< +rite # *ro$r#m to *rit the *#rts of URL.
Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
-
7/23/2019 Cn Lab Programs
10/36
public class get(")Parts C
public static %oid main4#tring argsDE C
for 4int i F 0B i M args.lengthB iLL C
try C
(") u F new (")4argsD0EB
#ystem.out.println4G7he (") is G L uB
#ystem.out.println4G7he protocol part is G L u.getProtocol4B
#ystem.out.println4G7he host part is G L u.get=ost4B
#ystem.out.println4G7he port part is G L u.getPort4B
#ystem.out.println4G7he file part is G L u.get;ile4B
#ystem.out.println4G7he ref part is G L u.get"ef4B
H ?? end try
catch 4alformed(")&ception e C
#ystem.err.println4argsD0E L Gis not a (") I understand.GB
H
H ?? end for
H ?? end main
H ?? end get(")Parts
Ot*t
4"
Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
import @a%a.lang.AB
import @a%a.net.(")B
public class Parse(") C
-
7/23/2019 Cn Lab Programs
11/36
public static %oid main4#tringDE args throws &ception C
(") a(") F new (")4Ghttp??e&le.comJ0?docs?boo!s?tutorialG
L G?inde&.htmlRnameFnetwor!ingS-W
-
7/23/2019 Cn Lab Programs
12/36
if 4host QF null C
int at#ign F host.inde&f4NTNB
if 4at#ign QF 1 host F host.substring4at#ignL1B
#ystem.out.println4G7he host is G L hostB
H
else C
#ystem.out.println4G7he host is null.GB
H
#ystem.out.println4G7he port is G L u.getPort4B
#ystem.out.println4G7he path is G L u.getPath4B
#ystem.out.println4G7he ref is G L u.get"ef4B
#ystem.out.println4G7he 9uery string is G L u.get8uery4B
H ?? end try
catch 4alformed(")&ception e& C
#ystem.err.println4argsDiE L G is not a (") I understand.GB
H
#ystem.out.println4B
H ?? end for
H ?? end main
H ?? end (")#plitter
Ot*t
/. Write # *ro$r#m to dis*8#% the soc,et=s *ort #d 6P #ddressPro$r#m
import @a%a.net.AB
import @a%a.io.AB
public class #oc!etInfo
C
public static %oid main4#tringDE args
C
-
7/23/2019 Cn Lab Programs
13/36
for 4int i F 0B i M args.lengthB iLL
C
try
C
#oc!et the#oc!et F new #oc!et4argsDiE* J0B
#ystem.out.println4G/onnected to G L the#oc!et.getInetddress4
L G on port G L the#oc!et.getPort4 L G from port G
L the#oc!et.get)ocalPort4 L G of G L the#oc!et.get)ocalddress4B
H ?? end try
catch 4(n!nown=ost&ception e
C
#ystem.err.println4GI canNt find G L argsDiEB
H
catch 4#oc!et&ception e
C
#ystem.err.println4G/ould not connect to G L argsDiEB
H
catch 4I&ception e
C
#ystem.err.println4eB
H
H ?? end for
H ?? end main
H ?? end #oc!etInfoOt*t
(OR)77dis*8#% soc,et 6etAddress< *ort< 8oc#8*ort #d 8oc#8 #ddress
import @a%a.net.#oc!etB
public class ain/lass C
public static %oid main4#tringDE args throws &ception C
#oc!et the#oc!et F new #oc!et4G12.16J.23.G* J0B
#ystem.out.println4G/onnected to G L the#oc!et.getInetddress4 L G on port G
L the#oc!et.getPort4 L G from port G L the#oc!et.get)ocalPort4 L G of G
-
7/23/2019 Cn Lab Programs
14/36
L the#oc!et.get)ocalddress4B
H
H
Ot*t
!. Write # *ro$r#m to sed > Recei9e d#t# from -#t#$r#mP#c,et
Pro$r#m
import @a%a.net.AB
public class -atagram&le C
public static %oid main4#tringDE args C
#tring s F G7his is a test.GBbyteDE data F s.get$ytes4 B
try C
Inetddress ia F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
15/36
import @a%a.io.AB
public class ulticast#niffer
C
public static %oid main4#tringDE args
C
Inetddress group F nullB
int port F 0B
?? read the address from the command line
try
C
group F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
16/36
H
catch 4I&ception e& CH
H
H
H
H
1. Write # *ro$r#m for M8tic#st seder
Pro$r#m
import @a%a.io.AB
public class ulticast#ender
C
public static %oid main4#tringDE args
C
Inetddress ia F nullB
int port F 0B
byte ttl F 4byte 1B
try
C
ia F Inetddress.get$y
-
7/23/2019 Cn Lab Programs
17/36
H ?? end catch
catch 4I&ception e&
C
#ystem.err.println4e&B
H ??end /atch
H ?? nd main
H ?? nd ulticast#ender
'o+ to e:ecte the *ro$r#m
&ecute 6 , O programs
;irst open two command prompts
/ompile , run ulticast#niffer program in one command prompt
?#9#c M8tic#stSiffer.?#9#
?#9# M8tic#stSiffer &3.&!!.&!!.&!!.444
/ompile , run ulticast#ender program in another command prompt separately
?#9#c M8tic#stSeder.?#9#
?#9# M8tic#stSeder &3.&!!.&!!.&!!.444
7hen output shows in ulticast#niffer command prompt as 'ere=s some m8tic#st d#t# is
*rited "4 times
2. Write # *ro$r#m for # Ch#t A**8ic#tio
Oe@Oe B% o*ei$ soc,et coectio #d dis*8#%i$ +h#t is +ritte 5% oe *#rt%
to the other.
-
7/23/2019 Cn Lab Programs
18/36
M#%@M#% (Bro#d c#st) E#ch c8iet o*es # soc,et coectio to the ch#t ser9er
#d +rites to the soc,et. Wh#te9er is +ritte 5% oe *#rt% c# 5e see 5% #88 other
*#rties.
Oe@Oe
/lient side Program
import @a%a.io.AB
import @a%a.net.ABimport @a%a.lang.AB
public class chatclient1C
public static %oid main4#tring argsDE
throws I&ception
C#oc!et csoc F nullB
#tring hostB
if4args.length0
hostFargsD0EBelse hostFGlocalhostGB
PrintWriter poutFnullB$uffered"eader binFnullB
try
C
csoc F new #oc!et4host*OBpout F new PrintWriter4csoc.getutput#tream4*trueB
bin F new $uffered"eader4new Input#tream"eader4csoc.getInput#tream4B
Hcatch4(n!nown=ost&ception e
C#ystem.err.println4Gun!nown =ostGB#ystem.e&it41B
H
catch4I&ception eCH$uffered"eader inFnew $uffered"eader4new Input#tream"eader4#ystem.inB
#tring inputB
while4true
Cinput F in.read)ine4B
pout.println4inputB
#tring msgFbin.read)ine4B#ystem.out.println4G#er%er GLmsgB
if4msg.e9uals4G$yeG brea!B
Hin.close4B
pout.close4B
bin.close4B
csoc.close4BH
H
#er%er #ide Program
-
7/23/2019 Cn Lab Programs
19/36
import @a%a.io.AB
import @a%a.net.ABimport @a%a.lang.AB
public class chatser%er
Cpublic static %oid main4#tring argsDE
throws I&ception
C#er%er#oc!et ssoc F nullB
tryC
ssoc F new #er%er#oc!et4OB
H
catch4I&ception eC
#ystem.err.println4G
-
7/23/2019 Cn Lab Programs
20/36
ssoc.close4B
csoc.close4B
HH
=ow to e&ecute the program
;irst open two command prompts
/ompile /lient side program in one command prompt
/ompile #er%er side program in another command prompt separately;irst run the ser%e side program then run the client side program
#end data from client to ser%er and from ser%er to client.
M#%@M#% (Bro#d c#st)
C8iet side Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
-
7/23/2019 Cn Lab Programs
21/36
public class m/lient1
C
static #oc!et sFnullB
static #tring nameB
static -ataInput#tream ddB
public static %oid main4#tring argsDEC
tryC
#ystem.out.println4Genter the nameGBddFnew -ataInput#tream4#ystem.inB
nameFdd.read)ine4B
H
catch4&ception e
C
#ystem.err.println4eBH
try
CsFnew #oc!et4GlocalhostG*O21B ?? ip address of the ser%er
cli#end2 csFnew cli#end24sBcs.start4B
/l"ec2 crFnew /l"ec24sB
cr.start4B
H
catch4&ception e
C#ystem.err.println4eB
H
H
H
class cli#end2 e&tends 7hread
C
#oc!et sBstatic PrintWriter pwB
cli#end24#oc!et s throws &ceptionC
this.sFsB
pwFnew PrintWriter4s.getutput#tream4*trueBH
public %oid run4
C
while4trueC
try
C
-ataInput#tream dinFnew -ataInput#tream4#ystem.inB
-
7/23/2019 Cn Lab Programs
22/36
#tring str1Fdin.read)ine4B
pw.println4G GLm/lient1.nameLGGLstr1B
Hcatch4&ception eC
#ystem.err.println4eB
H
HH
H
class /l"ec2 e&tends 7hreadC
#oc!et sB
static $uffered"eader brB/l"ec24#oc!et s throws &ception
C
this.sFsB
brFnew $uffered"eader4new Input#tream"eader4s.getInput#tream4BH
public %oid run4
C
tryC
#tring lineFbr.read)ine4Bwhile4lineQFnull
C
#ystem.out.println4lineBlineFbr.read)ine4B
H
Hcatch4&ception e
C#ystem.err.println4eB
HHH
Ser9er side Pro$r#m
import @a%a.io.AB
import @a%a.net.AB
public class m#er%er1
Cstatic #oc!et sDEFnew #oc!etD10EB
static int countB
public static %oid main4#tring argsDE throws &ceptionC
#er%er#oc!et ssB
try
CssFnew #er%er#oc!et4O21B
#ystem.out.println4G#er%er "unningGB
#r#end1 srFnew #r#end14B
sr.start4B
-
7/23/2019 Cn Lab Programs
23/36
while4true
C
sDcountEFss.accept4B/li#end1 csFnew /li#end14countB
cs.start4B
countLLB
HH
catch4&ception e
C#ystem.err.println4eB
H
HH
class /li#end1 e&tends 7hread
C
$uffered"eader brBPrintWriter pwB
#oc!et sB
#tring strB
int cB
/li#end14int cC
?? this.sFsB
this.cFcBtry
C
brFnew $uffered"eader4new
Input#tream"eader44m#er%er1.sDcE.getInput#tream4BHcatch4&ception e
C#ystem.err.println4eBH
H
public %oid run4C
while4true
C
tryC
strFbr.read)ine4B
#ystem.out.println4strBfor4int iF0BiMm#er%er1.countBiLL
C
if4iFFccontinueB
pwFnew PrintWriter4m#er%er1.sDiE.getutput#tream4*trueB
pw.println4strB
HHcatch4&ception e
C
#ystem.err.println4eB
H
-
7/23/2019 Cn Lab Programs
24/36
H
H
Hclass #r#end1 e&tends 7hread
C
$uffered"eader brB
#tring strBPrintWriter pwB
#r#end14 throws &ceptionC
brFnew $uffered"eader4new Input#tream"eader4#ystem.inB
H
public %oid run4
C
while4trueC
try
C
strFbr.read)ine4Bfor4int iF0BiMm#er%er1.countBiLL
CpwFnew PrintWriter4m#er%er1.sDiE.getutput#tream4*trueB
pw.println4G#er%erGLstrB
HHcatch4&ception e
C
#ystem.err.println4eB
HH
HH
=ow to e&ecute the program
;irst open three command prompts
/ompile /lient side program in two command prompt
/ompile #er%er side program in one command prompt separately
;irst run the ser%e side program
then run the client side program gi%e a name of the client as &
ne&t run the another client side program gi%e a name of the client as y
#end data from one client V that data %isible in ser%er side and other client side
#end data from ser%er V that data %isible in both client sides
#end data from another client V that data %isible in ser%er side and other client side
-
7/23/2019 Cn Lab Programs
25/36"&. Write # *ro$r#m for the Sim*8e fi8e tr#sfer 5et+ee t+o s%stems
-
7/23/2019 Cn Lab Programs
26/36
B% o*ei$ soc,et coectio to or ser9er o oe s%stem #d sedi$ # fi8e from oe
s%stem to #other.
C8iet side Pro$r#m
import @a%a.net.AB
import @a%a.io.AB
public class ftpclientC
public static %oid main4#tring argsDEC#oc!et sB
$uffered"eader in*brB
PrintWriter pwB#tring spath*dpathB
;ileutput#tream fosB
int cB
tryC sFnew #oc!et4GlocalhostG*1111B
inFnew $uffered"eader4new Input#tream"eader4#ystem.inB
brFnew $uffered"eader4new Input#tream"eader 4s.getInput#tream4B
pwFnew PrintWriter4s.getutput#tream4*trueB#ystem.out.println4GKn nter source file to be copiedGB
spathFin.read)ine4B#ystem.out.println4GKn nter -estination Path to 7ransferGB
dpathFin.read)ine4B
fosFnew ;ileutput#tream4dpathBpw.println4spathB
while44cFbr.read4QF1C
fos.write44charcB
fos.flush4BH
#ystem.out.println4G;ile 7ransfer /ompletedGBHcatch4&ception eC
#ystem.out.println4eB
HH
H
Ser9er side Pro$r#m
import @a%a.net.AB
import @a%a.io.ABpublic class ftpser%erC
public static %oid main4#tring argsDEC
#oc!et sB#er%er#oc!et ser%erB
$uffered"eader brB
PrintWriter pwB
#tring filenameB;ileInput#tream fisB
int cB
tryC
ser%erFnew #er%er#oc!et41111B
-
7/23/2019 Cn Lab Programs
27/36
#ystem.out.println4G#er%er Waiting for /onnectionKnGB
sFser%er.accept4B
#ystem.out.println4G/onnection stablishedKnGBbrFnew $uffered"eader4new Input#tream"eader 4s.getInput#tream4B
pwFnew PrintWriter4s.getutput#tream4B
filenameFbr.read)ine4B
fisFnew ;ileInput#tream4filenameBwhile44cFfis.read4QF1C
pw.print44charcB
pw.flush4BH
#ystem.out.println4filename L Gcopied to destinationGB
s.close4BH
catch4&ception eC
#ystem.out.println4eB
HH
H
'o+ to e:ecte the *ro$r#m
;irst open two command prompts/ompile and run #er%er side program in one command prompt
/ompile and run /lient side program in another window
nter source file to be copied
nter new name to sa%e that data
7hat new file automatically created and copied and located in same location of source program
". Write # *ro$r#m for the TFTP@C8iet. To de9e8o* # TFTP c8iet for fi8e tr#sfer.
-
7/23/2019 Cn Lab Programs
28/36
C8iet side Pro$r#m
import @a%a.io.ABimport @a%a.net.AB
import @a%a.lang.AB
public class tftpc
C public static %oid main4#tring argsDE throws &ception
C
-atagram#oc!et dsFnew -atagram#oc!et41501B $uffered"eader inputFnew $uffered"eader4new Input#tream"eader4#ystem.inB
#ystem.out.println4Genter file to sa%eGB
#tring fileFinput.read)ine4B ;ileutput#tream fosFnew ;ileutput#tream4fileB
#ystem.out.println4Genter file to transGB
fileFinput.read)ine4B
byte sDEFnew byteDfile.length4EB sFfile.get$ytes4B
#tring dataFnullB
ds.send4new -atagramPac!et4s*s.length*Inetddress.get)ocal=ost4*1500B
while4true
C sFnew byteD102EB
-atagramPac!et dpFnew -atagramPac!et4s*102B
ds.recei%e4dpB dataFnew #tring4dp.get-ata4*0*dp.get)ength4B
if4data.e9uals4GAAAG brea!B
fos.write4data.get$ytes4B
H fos.close4B
ds.close4B HH
Ser9er side Pro$r#m
import @a%a.io.AB
import @a%a.net.AB
import @a%a.lang.ABpublic class tftps
C
public static %oid main4#tring argsDE throws &ception C
-atagram#oc!et dsFnew -atagram#oc!et41500B
byte sDEFnew byteD102EB -atagramPac!et dpFnew -atagramPac!et4s*102B
ds.recei%e4dpB
#ystem.out.println4G/lient /onnectedGB
#tring dataFnew #tring4dp.get-ata4*0*dp.get)ength4B int countF0B
#ystem.out.println4G7ransferredGB
;ileInput#tream fsFnew ;ileInput#tream4dataB
while4fs.a%ailable4QF0
-
7/23/2019 Cn Lab Programs
29/36
C
if4fs.a%ailable4M102 countFfs.a%ailable4B
else countF102B sFnew byteDcountEB
fs.read4sB
dpFnew -atagramPac!et4s*s.length*Inetddress.get)ocal=ost4*1501B
ds.send4dpB H
fs.close4B
sFnew byteD3EB sFGAAAG.get$ytes4B
ds.send4new -atagramPac!et4s*s.length*Inetddress.get)ocal=ost4*1501B
ds.close4B H
H
'o+ to e:ecte the *ro$r#m
;irst open two command prompts
/ompile and run #er%er side program in one command prompt
/ompile and run /lient side program in another window
nter file to be sa%e 4new name
nter file to transfer 4e&isting file
7hat new file automatically created and copied and located in same location of source program
"/. Write # *ro$r#m for the 'TTP@Ser9er
-e9e8o* # 'TTP ser9er to im*8emet the fo88o+i$ comm#ds ;ET< POST< 'EA-