client&server lab
TRANSCRIPT
-
8/9/2019 Client&Server Lab
1/24
Client / Server Lab Program details
1. Develop a program to display the time of the server from a client using TCP/IP
2. Develop a program to display the time of the server from a client using UDP3. Develop a client/server application for chat using TCP/IP
. Develop a client/server application for chat using UDP!. "rite a program to simulate Domain #ame $erver%. "rite a client program to do&nload a file from 'TTP server
(. Implement a )PC
*. Implement a +TP
,. Develop a client/server application for -mails1. Develop a client/server 0aning pplication &ith dataase connectivity
11. Develop a client/server reservation system application &ith dataase connectivity
12. Develop a client/server application for implementing a scheduling for printing a different
4os ased on priority
1. Develop a program to display the time of the server from a client using TCP/IP
L!"#IT$%&'
Step1& $tart the program
Step(& Create the TCP socets for communications et&een Client and $erver.
Step)& 5et the time from the server using the server socet.
Step*&$end the time and date format to the client side.
Step+&Print the Date and the Time.
Step,&$top the program.
tcptimeserver.-ava
import 4ava.io.67
import 4ava.net.67
import 4ava.util.Calendar7
import 4ava.te8t.$impleDate+ormat7
pulic class tcptimeserver
9
pulic static void main:$tring a;+?)@T>#?"AByyyy@@dd ''mmssB7
$ystem.out.println:BTCP $-)-)B=7
-
8/9/2019 Client&Server Lab
2/24
$ystem.out.println:B$erver is ready to send time......B=7
$erver$ocet serversoc A ne& $erver$ocet:,=7
$ocet clientsoc Aserversoc.accept:=7
Print"riter outAne& Print"riter:clientsoc.get?utput$tream:=Etrue=7
$tring inputline7
Calendar calA Calendar.getInstance:=7
$impleDate+ormat sdfAne& $impleDate+ormat:DT->+?)@T>#?"=7
inputlineAsdf.format:cal.getTime:==7
out.println:inputline=7
$ystem.out.println:BProcess finishedB=7
F
F
tcptimeclient.-ava
import 4ava.io.67
import 4ava.net.67
pulic class tcptimeclient
9
pulic static void main:$tring;< args= thro&s I?-8ception
9
$ystem.out.println:BTCP CGI-#TB=7
$ystem.out.println:B-nter the host name to connectB=7
DataInput$tream inpAne& DataInput$tream:$ystem.in=7
$tring strAinp.readGine:=7
$ocet clientsocAne& $ocet:strE,=7
Print"riter outAne& Print"riter:clientsoc.get?utput$tream:=Etrue=7
0uffered)eader inAne& 0uffered)eader:ne& Input$tream)eader:clientsoc.getInput$tream:===7
-
8/9/2019 Client&Server Lab
3/24
$ystem.out.println:B$erver $ays BHin.readGine:==7
F
F
(. Develop a program to display the time of the server from a client using DP
L!"#IT$%&'
Step1& $tart the program
Step(& Create the UDP $ocets for communication et&een the Client and the $erver.
Step)& 5et the time from the server using the UDP $erver socet.
Step*&$end the time and date format to the client side.
Step+&Print the Date and the Time.
Step,&$top the program.
DPServer.-ava
import 4ava.net.67
import 4ava.io.67
import 4ava.util.67
class UDP$erver
9
pulic static void main:$tring args;
-
8/9/2019 Client&Server Lab
4/24
DPClient.-ava
import 4ava.net.67
import 4ava.io.67
class UDPClient
9
pulic static void main:$tring args;
-
8/9/2019 Client&Server Lab
5/24
pulic class tcpserver
9
pulic static void main:$tring a;
-
8/9/2019 Client&Server Lab
6/24
tcpclient.-ava
import 4ava.io.67
import 4ava.net.67
pulic class tcpclient
9
pulic static void main:$tring;< args= thro&s I?-8ception
9
$ystem.out.println:BTCP CGI-#TB=7
$ystem.out.println:B-nter the host name to connectB=7
DataInput$tream inpAne& DataInput$tream:$ystem.in=7
$tring strAinp.readGine:=7
$ocet clientsocAne& $ocet:strE,=7
Print"riter outAne& Print"riter:clientsoc.get?utput$tream:=Etrue=7
0uffered)eader inAne& 0uffered)eader:ne& Input$tream)eader:clientsoc.getInput$tream:===7
0uffered)eader stdinAne& 0uffered)eader:ne& Input$tream)eader:$ystem.in==7
$tring userinput7
try
9
&hile:true=
9
$ystem.out.println:B$erver saysBHin.readGine:==7
userinputAstdin.readGine:=7
out.println:userinput=7
F
F
catch:-8ception e=
-
8/9/2019 Client&Server Lab
7/24
9
$ystem.e8it:=7
F
F
F
*. Develop a client/server application for chat using DP
L!"#IT$%&'
Step1& $tart the program
Step(& Create the UDP socets for communications et&een Client and $erver.
Step)& -stalish the connections et&een the client and the server using the follo&ing
functions.
String sentence0ne5 StringreceivePacet.getData223
Step*&$end message from the server to that of the client side.
Step+&ccept the message from the server and send the reply message from the client to the
server
Step,&Close the connections et&een the client and server.
Step4&$top the program.
DPServer.-ava
import 4ava.io.67
import 4ava.net.67
class UDP$erver
9
pulic static void main:$tring args;
-
8/9/2019 Client&Server Lab
8/24
9
DatagramPacet receivePacet A ne& DatagramPacet:receiveDataEreceiveData.length=7
server$ocet.receive:receivePacet=7
$tring sentenceAne& $tring:receivePacet.getData:==7
$ystem.out.println:B)-C-I-D +)?@ CGI-#TBHsentence=7
Inetddress IPddress A receivePacet.getddress:=7
int port A receivePacet.getPort:=7
$tring capitalied$entenceAsentence.toUpperCase:=7
sendDataAcapitalied$entence.get0ytes:=7
DatagramPacet sendPacetAne& DatagramPacet:sendDataEsendData.lengthEIPddressEport=7
server$ocet.send:sendPacet=7
F
F
F
DPClient.-ava
import 4ava.io.67
import 4ava.net.67
class UDPClient
9
pulic static void main:$tring args;
-
8/9/2019 Client&Server Lab
9/24
yte;< sendDataAne& yte;12
-
8/9/2019 Client&Server Lab
10/24
for:int iA7iJaddresses.length7iHH=
9
$tring hostnameAaddresses;i
-
8/9/2019 Client&Server Lab
11/24
U)G urlAne& U)G:Bhttp//&&&.server.comB=7
'ttpU)GConnection conA:'ttpU)GConnection=url.openConnection:=7
0uffered)eader in A ne& 0uffered)eader:ne& Input$tream)eader:con.getInput$tream:===7
$tring inputGine7
+ile ne&fileAne& +ile:Bne&file.t8tB=7
+ile"riter out Ane& +ile"riter:ne&file=7
&hile::inputGineAin.readGine:==KAnull=
9
$ystem.out.println:inputGine=7
out.&rite:inputGine=7
F
in.close:=7
F
F
4. Implementation a ;TPL!"#IT$%&'
Step1& start the program.
Step( Create the TCP socet for communication et&een the client and the server.
Step) -stalish the connections et&een the client and the server.
Step* 5et the Target file to e sent using the +TP Protocol.
Step+ Transfer the Particular file to the client side.
Step, )eceive the file that is transferred from the server side.
Step4 fter finishing all the reLuest from the client side close the connection et&een them.
Step
-
8/9/2019 Client&Server Lab
12/24
9
pulic static void main:$tring args;
-
8/9/2019 Client&Server Lab
13/24
-
8/9/2019 Client&Server Lab
14/24
Input$tream iAs.getInput$tream:=7
DataInput$tream disAne& DataInput$tream:i=7
?utput$tream oAs.get?utput$tream:=7
Data?utput$tream dosAne& Data?utput$tream:o=7
$ystem.out.println:B"aiting for reLuestB=7
$tring fnameAdis.readUT+:=.to$tring:=7
if:fname.eLuals:Be8itB==
9
s.close:=7
$ystem.e8it:1=7
F
else
9
$ystem.out.println:BThe received file name IsBHfname=7
+ile)eader frAne& +ile)eader:fname=7
0uffered)eader rAne& 0uffered)eader:fr=7
$tring sr7
&hile::srAr.readGine:==KAnull=
9
dos.&riteUT+:sr=7
F
$ystem.out.println:B+ile isB=7
fr.close:=7
dis.close:=7
i.close:=7
dos.close:=7
o.close:=7
-
8/9/2019 Client&Server Lab
15/24
-
8/9/2019 Client&Server Lab
16/24
6/
pulic class -mail$ervlet e8tends 'ttp$ervlet
9
/66
6 0are ones method to send email message. #o attempt is
6 made to verify delivery. Uses $@TP protocol to contact
6 an email server at Q1(.,2.12.2Q port 2!
66/
pulic void send-mail: $tring fromE $tring toE $tring message=
9
try
9
// get a socet connection to the mail
// server at $@TP port 2! $ocet socet A ne& $ocet:B1(2.1%.1.12BE 2!=7
// Create an output stream for sending message
Print"riter out A ne& Print"riter:socet.get?utput$tream:=E true=7
// send mail using $@TP protocol
out.println:B@IG +)?@ B H from=7
out.println:B)CPT T? B H to=7
out.println:BDTMnB=7 // $ip line after DT
out.println:message=7 out.println:B.B=7 // -nd message &ith a single period
out.flush:=7
F
catch :-8ception e=
9
$ystem.out.println:B+ailed to send email B H e=7
F
F
pulic void doPost:'ttp$ervlet)eLuest reLuestE
'ttp$ervlet)esponse response=
thro&s $ervlet-8ceptionE I?-8ception
9
$tring from A reLuest.getParameter:BfromB=7
-
8/9/2019 Client&Server Lab
17/24
$tring to A reLuest.getParameter:BtoB=7
$tring message A reLuest.getParameter:BmessageB=7
send-mail:fromE toE message=7
response.setContentType:Bte8t/htmlB=7
Print"riter out A response.get"riter:=7
out.println:B-mail hopefully sent to B H to=7
F
F
Success.-sp
JR
Document success
Created on $ep (E 2,E 3,1( P@
uthor po&er
RS
JRpage contentTypeABte8t/htmlB page-ncodingABUT+*BRS
JhtmlS
JheadS
JtitleS@essageJ/titleS
Jlin relABstylesheetB hrefABstyle.cssB typeABte8t/cssBS
J/headS
JodyS
JcenterS
Jdiv classABmsgBS
Jh2S@essageJ/h2S
JpS
-mail sent
J/pS
J/divS
J/centerS
J/odyS
J/htmlS
-
8/9/2019 Client&Server Lab
18/24
Inde9.-sp
JR
Document inde8
Created on $ep (E 2,E 213% P@
uthor po&er
RS
JRpage contentTypeABte8t/htmlB page-ncodingABUT+*BRS
JhtmlS
JheadS
Jmeta httpeLuivABContentTypeB contentABte8t/html7 charsetAUT+*BS
JtitleS$ending emailJ/titleS
Jlin relABstylesheetB hrefABstyle.cssB typeABte8t/cssBSJ/headS
JodyS
JcenterS
Jform actionAB-mail$ervletBS
JtaleS
JtrS
JtdS+romJ/tdS
JtdSJinput typeABte8tB nameABfromBSJ/tdS
J/trS
JtrS
JtrS
JtdSToJ/tdS
JtdSJinput typeABte8tB nameABtoBSJ/tdS
J/trS
JtrS
JtdS$u4ectJ/tdS
JtdSJinput typeABte8tB nameABsu4ectBSJ/tdS
J/trS
JtrS
JtdS@essageJ/tdS
JtdSJte8tarea colsAB2!B ro&sAB*B nameABmessageBSJ/te8tareaSJ/tdS
J/trS
JtrS
JtdSGoginJ/tdS
JtdSJinput typeABte8tB nameABloginBSJ/tdS
-
8/9/2019 Client&Server Lab
19/24
J/trS
JtrS
JtdSPass&ordJ/tdS
JtdSJinput typeABpass&ordB nameABpass&ordBSJ/tdS
J/trS
J/taleS
JrS
Jinput typeABsumitB valueABsumitBS
J/formS
J/centerS
J/odyS
J/htmlS
=rror.-sp
JR
Document error
Created on $ep (E 2,E 3,3, P@
uthor po&er
RS
JRpage contentTypeABte8t/htmlB page-ncodingABUT+*BRS
JhtmlS
JheadS
Jmeta httpeLuivABContentTypeB contentABte8t/html7 charsetAUT+*BS
JtitleS-rrorJ/titleS
Jlin relABstylesheetB hrefABstyle.cssB typeABte8t/cssBS
J/headS
JodyS
JcenterS
Jdiv classABerrorBS
Jh2S-rrorJ/h2S
JpS
@essage JRA reLuest.getttriute:B-rror@essageB= RS
J/pS
J/divS
J/centerS
J/odyS
J/htmlS
-
8/9/2019 Client&Server Lab
20/24
>. Develop a client/server reservation system application 5ith database connectivity
L!"#IT$%&'
Step1&$tart the program.
Step(&Initialie the Nava Data0ase Driver to that of the ?4ect Data0ase Driver.
Step)&Create an ?D0C Driver for connecting the ccess Dataase.
Step*&Create the Tale in the dataase and add the necessary fields in that tale.
Step+&$elect the Data $ource and lin the Data0ase to that of the Nava program.
Step,&)un the program and insert the Data into the tale using the ueries.
Step4&)etrieve the Data from the Data0ase and print the result.
Step@$B=7
status A true7
F
catch:-8ception e=
9
$ystem.out.println:B-rror in d connectionB=7
F
return status7
F
-
8/9/2019 Client&Server Lab
21/24
pulic $tring @aeTicets:$tring ticet>nameEint ticet>numerEint ticet>LuentEdoule
ticet>priceE$tring start>locationE$tring end>location=
9
$tring strABProlem in data insertB7
try
9
oolean st A connect:=7
$ystem.out.println:Bconnection SBHst=7
$tring add>Luery A BI#$-)T I#T?
ticet:ticet>numerEticet>nameEticet>LuentEticet>priceEstart>locationEend>location=
GU-$:BHticet>numerHBEQBHticet>nameHBQEBHticet>LuentHBEBHticet>priceHBEQBHstart>locat
ionHBQEQBHend>locationHBQ=B7
$ystem.out.println:BInsert LuerySBHadd>Luery=7
4ava.sLl.$tatement statement A connection.create$tatement:=7
statement.e8ecute:add>Luery=7 strA BTicet successfully addedB 7
F
catch:-8ception e=
9
$ystem.out.println:B-rror in InsertBHe=7
F
return str7
F
pulic $tring reservTicet:int ticet>numerE$tring ticet>nameE int ticet>Luent=
9
$tring strABProlem in reservB7
try
9
int LuntA7
oolean st A connect:=7
$tring select>Luery A B$-G-CT 6 +)?@ ticet "'-)-
ticet>numerABHticet>numer7
4ava.sLl.$tatement statement A connection.create$tatement:=7
statement.e8ecuteuery:select>Luery=7
4ava.sLl.)esult$et result$et A statement.get)esult$et:=7
if:result$et.ne8t:==
9
Lunt A result$et.getInt:Bticet>LuentB=7
F
-
8/9/2019 Client&Server Lab
22/24
if:LuntJticet>Luent=
9
return LuntHB ticets only avilale for this routB7
F
else
9
Lunt A Lunt ticet>Luent7
$tring update>Luery A BUPDT- ticet $-T ticet>LuentABHLuntHB "'-)-
ticet>numerABHticet>numer7
int status A statement.e8ecuteUpdate:update>Luery=7
if:statusAA1 =
9
return BTicet successfully )eservedB7
F
else 9
return str7
F
F
F
catch:-8ception e=
9
$ystem.out.println:B-rror in reserv BHe=7
F
return B)eservation methodB7
F
pulic void InformationCenter:$tring start>locationE$tring end>location=
9
$tring strABProlem in information centerB7
try
9
int LuntA7
oolean st A connect:=7
$tring select>Luery A B$-G-CT 6 +)?@ ticet "'-)-
start>locationAQBHstart>locationHBQ #D end>locationAQBHend>locationHBQB7
$ystem.out.println:B$elect Luery SBHselect>Luery=7
4ava.sLl.$tatement statement A connection.create$tatement:=7
statement.e8ecuteuery:select>Luery=7
4ava.sLl.)esult$et result$et A statement.get)esult$et:=7
-
8/9/2019 Client&Server Lab
23/24
if:result$et.ne8t:==
9
$ystem.out.println:BTicet DetailsB=7
$ystem.out.println:BTicet #umer SBHresult$et.getInt:Bticet>numerB==7
$ystem.out.println:BTicet #ameSBHresult$et.get$tring:Bticet>nameB==7
$ystem.out.println:B-ach Ticet PriceSBHresult$et.getDoule:Bticet>priceB==7
$ystem.out.println:B#umer of Ticets availale
SBHresult$et.getInt:Bticet>LuentB==7
F
else
9
$ystem.out.println:BThere is no ticets for this routB=7
F
F
catch:-8ception e= 9
$ystem.out.println:B-rror in reserv BHe=7
F
F
pulic static void main:$tring;< args= thro&s I?-8ception
9
DataInput$tream din A ne& DataInput$tream:$ystem.in=7
@ain res A ne& @ain:=7
&hile:true=9 $ystem.out.println:BMtMt?nline TrainTicet )eservation B=7
$ystem.out.println:B1.@ae Ticets :dministrator only=Mn2.Ticet
)eservationMn3.Imfomation CenterMn.-8itB=7
$ystem.out.println:B-nter your choice B=7
int choice A Integer.parseInt:din.readGine:==7
s&itch :choice=
9
case 1
9
$ystem.out.println:B-nter Trian Ticet #umerB=7
int ticet>numer A Integer.parseInt:din.readGine:==7
$ystem.out.println:B-nter Ticet #ameB=7
$tring ticet>name A din.readGine:=7
$ystem.out.println:B-nter Ticet price B=7
doule ticet>priceA Doule.parseDoule:din.readGine:==7
$ystem.out.println:B$tart locationB=7
-
8/9/2019 Client&Server Lab
24/24
$tring start>location A din.readGine:=7
$ystem.out.println:B-nd locationB=7
$tring end>location A din.readGine:=7
$ystem.out.println:B#umer of Ticets B=7
int ticet>Luent AInteger.parseInt:din.readGine:==7
$tring output A res.@aeTicets:ticet>nameE
ticet>numerEticet>LuentEticet>priceE start>locationE end>location=7
$ystem.out.println:output=7
F
rea7
case 2
9
$ystem.out.println:B-nter Trian Ticet #umerB=7
int ticet>numer A Integer.parseInt:din.readGine:==7
$ystem.out.println:B-nter Ticet #ameB=7 $tring ticet>name A din.readGine:=7
$ystem.out.println:B#umer of Ticets B=7
int ticet>Luent AInteger.parseInt:din.readGine:==7
$tring output A res.reservTicet:ticet>numerE ticet>nameE ticet>Luent=7
$ystem.out.println:output=7
F
rea7
case 3
9
$ystem.out.println:B$tart locationB=7 $tring start>location A din.readGine:=7
$ystem.out.println:B-nd locationB=7
$tring end>location A din.readGine:=7
res.InformationCenter:start>locationE end>location=7
F
rea7
case
$ystem.e8it:1=7
rea7
FF
FF