MAY 2011 Master of Computer AppIication (MCA) - Semester 4 MC0078 - Java Programming - 4 Credits (Book ID: B0831 & B0832) Assignment Set - 1 (40 Marks) Answer aII Questions Each Question carries five marks Book ID: B0831
1. rite Java program to convert the Rupees to DoIIars. Ans 1:
import java.util.Scanner; public class rupeesconversion
public static void main(Iloat|| args)
//Have the user to enter rupees System.out.println("Enter rupees"); double rupeesr;
// Convert rupees to dollars. iI (rupeesr~1)
double dollar ruppesr/45.2; System.out.println("the converted value Irom rupees to dollars is:"dollar); } else System.out.println("Try again"); } }
MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
2
2. Discuss the foIIowing with suitabIe exampIe programs for each: A) Data Types in Java B) VariabIes in Java Ans 2:
A) Data Types in 1ava There are two kinds oI data types in Java 1. Primitives/standard data types. 2. Abstract/derived data types.
Primitives Data Types rlmlLlve daLa Lypes (also know as sLandard daLa Lypes) are Lhe daLa Lypes LhaL are bullL lnLo Lhe !ava language 1he !ava compller holds deLalls lnsLrucLlons on each operaLlon Lhe daLa Lypes LhaL are bullL lnLo Lhe !ava language 1he !ava compller holds deLalled lnsLrucLlons on each legal operaLlon Lhe daLa Lype supporLs 1here are elghL prlmlLlve daLa Lypes ln !ava
1he daLa Lypes byLe shorL lnL long floaL and double are numerlc daLa Lypes 1he flrsL four of Lhese can hold only whole numbers whereas Lhe lasL Lwo (floaL and double) can hold declmal values llke 303 All Lhese daLa Lypes can hold negaLlve values Powever Lhe keyword unslgned can be used Lo resLrlcL Lhe range of values Lo poslLlve numbers AmongsL oLhers boolean can hold only Lhe value Lrue or false and char can hold only a slngle characLer MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
3
Abstract/Derived Data Types AbsLracL daLa Lypes are based on prlmlLlves daLa Lypes and have more funcLlonallLy LhaL Lhe prlmlLlve daLa Lypes lor example SLrlng ls an absLracL daLa Lype LhaL can sLore alphabeLs dlglLs and oLher characLers llke / () $# ?ou cannoL perform calculaLlons on a varlable of Lhe sLrlng daLa Lype even lf Lhe daLa sLored ln lL has dlglLs
Variables in 1ava When you learned algebralc equaLlons ln school you used x and y Lo represenL values ln equaLlons unllke pl whlch has a consLanL value of 314 Lhe values of x and y are noL consLanL ln equaLlons !ava provldes consLanLs and varlables Lo sLore daLa ln programs !ava allocaLes memory Lo each varlable and consLanL you use ln your program As ln algebra Lhe values of varlables may change ln a program buL Lhe values of consLanLs as Lhe name suggesLs do noL change ?ou musL asslgn unlque names Lo varlables and consLanLs varlable names are used ln a program ln much Lhe same way as Lhey are ln ordlnary Algebra Lach varlable used ln a program musL be declared 1haL ls Lo say Lhe program musL conLaln a sLaLemenL speclfylng preclsely whaL klnd of lnformaLlon (daLa Lype) Lhe varlable wlll conLaln 1hls applles Lo every varlable used ln Lhe program regardless of Lhe Lype
Naming Variables A program refers Lo a varlable uslng lLs name CerLaln rules and convenLlons govern Lhe namlng of varlables ?ou musL adhere Lo rules ConvenLlons help lmprove Lhe readablllLy of Lhe program buL followlng Lhem ls noL mandaLory Rules for Naming Variables in 1ava A variable name: 1. Must not be a keyword in Java. 2. Must not begin with a digit. 3. Must not contain embedded spaces. 4. Can contain characters Irom various alphabets, like Japanese, Greek, 5. and Cyrillic. Syntax for Defining Variables All the attributes oI a class are deIined as data members. The syntax used to declare a class variable is: datatype~ variablename~ As the braces ' } are used to mark the beginning and end oI a class, a semicolon '; is used to mark the end oI a statement.
3. hat are the difference between an interface and an abstract cIass? MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
4
Ans 3: Abstract classes can have implementations for some of its members (Methods), but the interface class can't have implementation for any of its members.
An interface is a contract: the guy writing the interface say "hey, accept things looking that way", and the guy using the interface say "OK, the class write looks that way". And interface is an empty shell, there are only the signatures (name / params / return type) of the methods. The methods do not contain anything. The interface can't do anything. t's just a pattern
MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
3
4. Describe the foIIowing with respect to Exception HandIing: A) Exception CIasses B) Common Exceptions Ans 4: (A) Exception CIasses 1he class aL Lhe Lop of Lhe excepLlon classes hlerarchy ls called 1hrowab|e 1wo classes are derlved from Lhe 1hrowable class Lrror and LxcepLlon 1he LxcepLlon class ls used fro Lhe excepLlonal condlLlons LhaL have Lo be Lrapped ln a program 1he Lrror class deflnes a condlLlon LhaL does noL occur under normal clrcumsLances ln oLher words Lhe Lrror class ls used for caLasLrophlc fallures such as vlrLualMachlneLrror 1hese classes are avallable ln Lhe [avalang package
(B) Common Exceptions !ava has several predeflned excepLlons 1he mosL common excepLlons LhaL you may encounLer are descrlbed below
Arithmetic Exception 1hls excepLlon ls Lhrown when an excepLlonal arlLhmeLlc condlLlon has occurred lor example a dlvlslon by zero generaLes such an excepLlon
MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
6
uIIPointer Exception 1hls excepLlon ls Lhrown when an appllcaLlon aLLempLs Lo use null where an ob[ecL ls requlred An ob[ecL LhaL has noL been allocaLed memory holds a null value 1he slLuaLlons ln whlch an excepLlon ls Lhrown lnclude
1. Using an object without allocating memory for it. 2. Calling the methods of a null object. 3. Accessing or modifying the attributes of a null object.
ArrayIndexOutOfBounds Exception 1he excepLlon ArraylndexCuLCf8ounds LxcepLlon ls Lhrown when an aLLempL ls made Lo access an array elemenL beyond Lhe lndex of Lhe array lor example lf you Lry Lo access Lhe elevenLh elemenL of an array LhaL's has only Len elemenLs Lhe excepLlon wlll be Lhrown
Book ID: B0832
6. Define RMI. Define the architecture of RMI invocation. 8Ml appllcaLlons ofLen comprlse Lwo separaLe programs a server and a cllenL A Lyplcal server program creaLes some remoLe ob[ecLs makes references Lo Lhese ob[ecLs accesslble and walLs for cllenLs Lo lnvoke meLhods on Lhese ob[ecLs A Lyplcal cllenL program obLalns a remoLe reference Lo one or more remoLe ob[ecLs on a server and Lhen lnvokes meLhods on Lhem 8Ml provldes Lhe mechanlsm by whlch Lhe server and Lhe cllenL communlcaLe and pass lnformaLlon back and forLh Such an appllcaLlon ls someLlmes referred Lo as a lsttlbote object oppllcotloo
Distributed object applications need to do the following: ocate remote objects: AppllcaLlons can use varlous mechanlsms Lo obLaln references Lo remoLe ob[ecLs lor example an appllcaLlon can reglsLer lLs remoLe ob[ecLs wlLh 8Mls slmple namlng faclllLy Lhe 8Ml reglsLry AlLernaLlvely an appllcaLlon can pass and reLurn remoLe ob[ecL references as parL of oLher remoLe lnvocaLlons
Communicate with remote objects: eLalls of communlcaLlon beLween remoLe ob[ecLs are handled by 8Ml 1o Lhe programmer remoLe communlcaLlon looks slmllar Lo regular !ava meLhod lnvocaLlons
oad cIass definitions for objects that are passed around: 8ecause 8Ml enables ob[ecLs Lo be passed back and forLh lL provldes mechanlsms for loadlng an ob[ecLs class deflnlLlons as well as for LransmlLLlng an ob[ecLs daLa 1he followlng lllusLraLlon deplcLs an 8Ml dlsLrlbuLed appllcaLlon LhaL uses Lhe 8Ml reglsLry Lo obLaln a reference Lo a remoLe ob[ecL 1he server calls Lhe reglsLry Lo assoclaLe (or blnd) a name wlLh a remoLe ob[ecL 1he cllenL looks up Lhe remoLe ob[ecL by lLs name ln Lhe servers reglsLry and Lhen lnvokes a meLhod on lL 1he lllusLraLlon also shows LhaL Lhe 8Ml sysLem uses an exlsLlng web server Lo load class deflnlLlons from server Lo cllenL and from cllenL Lo server for ob[ecLs when needed MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
7
Advantages of Dynamic Code oading Cne of Lhe cenLral and unlque feaLures of 8Ml ls lLs ablllLy Lo download Lhe deflnlLlon of an ob[ecLs class lf Lhe class ls noL deflned ln Lhe recelvers !ava vlrLual machlne All of Lhe Lypes and behavlor of an ob[ecL prevlously avallable only ln a slngle !ava vlrLual machlne can be LransmlLLed Lo anoLher posslbly remoLe !ava vlrLual machlne 8Ml passes ob[ecLs by Lhelr acLual classes so Lhe behavlor of Lhe ob[ecLs ls noL changed when Lhey are senL Lo anoLher !ava vlrLual machlne 1hls capablllLy enables new Lypes and behavlors Lo be lnLroduced lnLo a remoLe !ava vlrLual machlne Lhus dynamlcally exLendlng Lhe behavlor of an appllcaLlon 1he compuLe englne example ln Lhls Lrall uses Lhls capablllLy Lo lnLroduce new behavlor Lo a dlsLrlbuLed program
Remote Interfaces, Objects, and Methods lke any oLher !ava appllcaLlon a dlsLrlbuLed appllcaLlon bullL by uslng !ava 8Ml ls made up of lnLerfaces and classes 1he lnLerfaces declare meLhods 1he classes lmplemenL Lhe meLhods declared ln Lhe lnLerfaces and perhaps declare addlLlonal meLhods as well ln a dlsLrlbuLed appllcaLlon some lmplemenLaLlons mlghL reslde ln some !ava vlrLual machlnes buL noL oLhers Cb[ecLs wlLh meLhods LhaL can be lnvoked across !ava vlrLual machlnes are called temote objects An ob[ecL becomes remoLe by lmplemenLlng a temote lotetfoce whlch has Lhe followlng characLerlsLlcs
A remoLe lnLerface exLends Lhe lnLerface [avarml8emoLe Lach meLhod of Lhe lnLerface declares [avarml8emoLeLxcepLlon ln lLs Lhrows clause ln addlLlon Lo any appllcaLlonspeclflc excepLlons 8Ml LreaLs a remoLe ob[ecL dlfferenLly from a nonremoLe ob[ecL when Lhe ob[ecL ls passed from one !ava vlrLual machlne Lo anoLher !ava vlrLual machlne 8aLher Lhan maklng a copy of Lhe lmplemenLaLlon ob[ecL ln Lhe recelvlng !ava vlrLual machlne 8Ml passes a remoLe stob for a remoLe ob[ecL 1he sLub acLs as Lhe local represenLaLlve or proxy for Lhe remoLe ob[ecL and baslcally ls Lo Lhe cllenL Lhe remoLe reference 1he cllenL lnvokes a meLhod on Lhe local sLub whlch ls responslble for carrylng ouL Lhe meLhod lnvocaLlon on Lhe remoLe ob[ecL A sLub for a remoLe ob[ecL lmplemenLs Lhe same seL of remoLe lnLerfaces LhaL Lhe remoLe ob[ecL lmplemenLs 1hls properLy enables a sLub Lo be casL Lo any of Lhe lnLerfaces LhaL Lhe remoLe ob[ecL lmplemenLs Powever ooly Lhose meLhods deflned ln a remoLe lnLerface are avallable Lo be called from Lhe recelvlng !ava vlrLual machlne MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
8
Creating Distributed AppIications by Using RMI Using RM to develop a distributed application involves these general steps: 1. Designing and implementing the components of your distributed application. 2. Compiling sources. 3. Making classes network accessible. 4. Starting the application.
Designing and ImpIementing the AppIication Components llrsL deLermlne your appllcaLlon archlLecLure lncludlng whlch componenLs are local ob[ecLs and whlch componenLs are remoLely accesslble 1hls sLep lncludes
Defining the remote interfaces: A remoLe lnLerface speclfles Lhe meLhods LhaL can be lnvoked remoLely by a cllenL CllenLs program Lo remoLe lnLerfaces noL Lo Lhe lmplemenLaLlon classes of Lhose lnLerfaces 1he deslgn of such lnLerfaces lncludes Lhe deLermlnaLlon of Lhe Lypes of ob[ecLs LhaL wlll be used as Lhe parameLers and reLurn values for Lhese meLhods lf any of Lhese lnLerfaces or classes do noL yeL exlsL you need Lo deflne Lhem as well ImpIementing the remote objects: 8emoLe ob[ecLs musL lmplemenL one or more remoLe lnLerfaces 1he remoLe ob[ecL class may lnclude lmplemenLaLlons of oLher lnLerfaces and meLhods LhaL are avallable only locally lf any local classes are Lo be used for parameLers or reLurn values of any of Lhese meLhods Lhey musL be lmplemenLed as well ImpIementing the cIients: CllenLs LhaL use remoLe ob[ecLs can be lmplemenLed aL any Llme afLer Lhe remoLe lnLerfaces are deflned lncludlng afLer Lhe remoLe ob[ecLs have been deployed CompiIing Sources As wlLh any !ava program you use Lhe [avac compller Lo complle Lhe source flles 1he source flles conLaln Lhe declaraLlons of Lhe remoLe lnLerfaces Lhelr lmplemenLaLlons any oLher server classes and Lhe cllenL classes
Making CIasses etwork AccessibIe ln Lhls sLep you make cerLaln class deflnlLlons neLwork accesslble such as Lhe deflnlLlons for Lhe remoLe lnLerfaces and Lhelr assoclaLed Lypes and Lhe deflnlLlons for classes LhaL need Lo be downloaded Lo Lhe cllenLs or servers Classes deflnlLlons are Lyplcally made neLwork accesslble Lhrough a web server Starting the AppIication SLarLlng Lhe appllcaLlon lncludes runnlng Lhe 8Ml remoLe ob[ecL reglsLry Lhe server and Lhe cllenL 1he resL of Lhls secLlon walks Lhrough Lhe sLeps used Lo creaLe a compuLe englne
BuiIding a Generic Compute Engine 1hls Lrall focuses on a slmple yeL powerful dlsLrlbuLed appllcaLlon called a compote eoqloe 1he compuLe englne ls a remoLe ob[ecL on Lhe server LhaL Lakes Lasks from cllenLs runs Lhe Lasks and MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
9
reLurns any resulLs 1he Lasks are run on Lhe machlne where Lhe server ls runnlng 1hls Lype of dlsLrlbuLed appllcaLlon can enable a number of cllenL machlnes Lo make use of a parLlcularly powerful machlne or a machlne LhaL has speclallzed hardware 1he novel aspecL of Lhe compuLe englne ls LhaL Lhe Lasks lL runs do noL need Lo be deflned when Lhe compuLe englne ls wrlLLen or sLarLed new klnds of Lasks can be creaLed aL any Llme and Lhen glven Lo Lhe compuLe englne Lo be run 1he only requlremenL of a Lask ls LhaL lLs class lmplemenLs a parLlcular lnLerface 1he code needed Lo accompllsh Lhe Lask can be downloaded by Lhe 8Ml sysLem Lo Lhe compuLe englne 1hen Lhe compuLe englne runs Lhe Lask uslng Lhe resources on Lhe machlne on whlch Lhe compuLe englne ls runnlng 1he ablllLy Lo perform arblLrary Lasks ls enabled by Lhe dynamlc naLure of Lhe !ava plaLform whlch ls exLended Lo Lhe neLwork by 8Ml 8Ml dynamlcally loads Lhe Lask code lnLo Lhe compuLe englnes !ava vlrLual machlne and runs Lhe Lask wlLhouL prlor knowledge of Lhe class LhaL lmplemenLs Lhe Lask Such an appllcaLlon whlch has Lhe ablllLy Lo download code dynamlcally ls ofLen called a bebovlotbose oppllcotloo
7. Define the foIIowing terms: A) Socket B) Port C) Datagram Describe the steps in reading and writing data from sockets. Ans 7: A) Socket normally a server runs on a speclflc compuLer and has a sockeL LhaL ls bound Lo a speclflc porL number 1he server [usL walLs llsLenlng Lo Lhe sockeL for a cllenL Lo make a connecLlon requesL
Cn Lhe cllenLslde 1he cllenL knows Lhe hosLname of Lhe machlne on whlch Lhe server ls runnlng and Lhe porL number on whlch Lhe server ls llsLenlng 1o make a connecLlon requesL Lhe cllenL Lrles Lo rendezvous wlLh Lhe server on Lhe servers machlne and porL 1he cllenL also needs Lo ldenLlfy lLself Lo Lhe server so lL blnds Lo a local porL number LhaL lL wlll use durlng Lhls connecLlon 1hls ls usually asslgned by Lhe sysLem
lf everyLhlng goes well Lhe server accepLs Lhe connecLlon upon accepLance Lhe server geLs a new sockeL bound Lo Lhe same local porL and also has lLs remoLe endpolnL seL Lo Lhe address and porL of Lhe cllenL lL needs a new sockeL so LhaL lL can conLlnue Lo llsLen Lo Lhe orlglnal sockeL for connecLlon requesLs whlle Lendlng Lo Lhe needs of Lhe connecLed cllenL MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
10
Cn Lhe cllenL slde lf Lhe connecLlon ls accepLed a sockeL ls successfully creaLed and Lhe cllenL can use Lhe sockeL Lo communlcaLe wlLh Lhe server 1he cllenL and server can now communlcaLe by wrlLlng Lo or readlng from Lhelr sockeLs
Definition: A socket ls one endpolnL of a Lwoway communlcaLlon llnk beLween Lwo programs runnlng on Lhe neLwork A sockeL ls bound Lo a porL number so LhaL Lhe 1C layer can ldenLlfy Lhe appllcaLlon LhaL daLa ls desLlned Lo be senL An endpolnL ls a comblnaLlon of an l address and a porL number Lvery 1C connecLlon can be unlquely ldenLlfled by lLs Lwo endpolnLs 1haL way you can have mulLlple connecLlons beLween your hosL and Lhe server 1he [avaneL package ln Lhe !ava plaLform provldes a class SockeL LhaL lmplemenLs one slde of a Lwoway connecLlon beLween your !ava program and anoLher program on Lhe neLwork 1he SockeL class slLs on Lop of a plaLformdependenL lmplemenLaLlon hldlng Lhe deLalls of any parLlcular sysLem from your !ava program 8y uslng Lhe [avaneLSockeL class lnsLead of relylng on naLlve code your !ava programs can communlcaLe over Lhe neLwork ln a plaLformlndependenL fashlon AddlLlonally [avaneL lncludes Lhe ServerSockeL class whlch lmplemenLs a sockeL LhaL servers can use Lo llsLen for and accepL connecLlons Lo cllenLs 1hls lesson shows you how Lo use Lhe SockeL and ServerSockeL classes lf you are Lrylng Lo connecL Lo Lhe Web Lhe u8 class and relaLed classes (u8ConnecLlon u8Lncoder) are probably more approprlaLe Lhan Lhe sockeL classes ln facL u8s are a relaLlvely hlghlevel connecLlon Lo Lhe Web and use sockeLs as parL of Lhe underlylng lmplemenLaLlon See Worklng wlLh u8s for lnformaLlon abouL connecLlng Lo Lhe Web vla u8s
(C) Datagram Definition: A otoqtom ls an lndependenL selfconLalned message senL over Lhe neLwork whose arrlval arrlval Llme and conLenL are noL guaranLeed 1he [avaneL package conLalns Lhree classes Lo help you wrlLe !ava programs LhaL use daLagrams Lo send and recelve packeLs over Lhe neLwork aLagramSockeL aLagramackeL and MulLlcasLSockeL An appllcaLlon can send and recelve aLagramackeLs Lhrough a aLagramSockeL ln addlLlon aLagramackeLs can be broadcasL Lo mulLlple reclplenLs all llsLenlng Lo a MulLlcasLSockeLlk Reading from and Writing to a Socket eLs look aL a slmple example LhaL lllusLraLes how a program can esLabllsh a connecLlon Lo a server program uslng Lhe SockeL class and Lhen how Lhe cllenL can send daLa Lo and recelve daLa from Lhe server Lhrough Lhe sockeL MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
11
1he example program lmplemenLs a cllenL LchoCllenL LhaL connecLs Lo Lhe Lcho server 1he Lcho server slmply recelves daLa from lLs cllenL and echoes lL back 1he Lcho server ls a wellknown servlce LhaL cllenLs can rendezvous wlLh on porL 7 LchoCllenL creaLes a sockeL Lhereby geLLlng a connecLlon Lo Lhe Lcho server lL reads lnpuL from Lhe user on Lhe sLandard lnpuL sLream and Lhen forwards LhaL LexL Lo Lhe Lcho server by wrlLlng Lhe LexL Lo Lhe sockeL 1he server echoes Lhe lnpuL back Lhrough Lhe sockeL Lo Lhe cllenL 1he cllenL program reads and dlsplays Lhe daLa passed back Lo lL from Lhe server
import java.io.*; import java.net.*; public class EchoClient public static void main(String|| args) throws IOException Socket echoSocket null; PrintWriter out null; BuIIeredReader in null; try echoSocket new Socket("taranis", 7); out new PrintWriter(echoSocket.getOutputStream(), true); in new BuIIeredReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) System.err.println("Couldn't get I/O Ior " "the connection to: taranis."); System.exit(1); } BuIIeredReader stdIn new BuIIeredReader( new InputStreamReader(System.in)); String userInput; while ((userInput stdIn.readLine()) ! null) out.println(userInput); System.out.println("echo: " in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } } noLe LhaL LchoCllenL boLh wrlLes Lo and reads from lLs sockeL Lhereby sendlng daLa Lo and recelvlng daLa from Lhe Lcho server eLs walk Lhrough Lhe program and lnvesLlgaLe Lhe lnLeresLlng parLs 1he Lhree sLaLemenLs ln Lhe Lry block of Lhe maln meLhod are crlLlcal 1hese llnes esLabllsh Lhe sockeL connecLlon beLween Lhe cllenL and Lhe server and open a rlnLWrlLer and a 8uffered8eader on Lhe sockeL
echoSockeL new SockeL(Laranls 7) MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
12
ouL new rlnLWrlLer(echoSockeLgeLCuLpuLSLream() Lrue) ln new 8uffered8eader(new lnpuLSLream8eader( echoSocket.getInputStream()));
1he flrsL sLaLemenL ln Lhls sequence creaLes a new SockeL ob[ecL and names lL echoSockeL 1he SockeL consLrucLor used here requlres Lhe name of Lhe machlne and Lhe porL number Lo whlch you wanL Lo connecL 1he example program uses Lhe hosL name Laranls 1hls ls Lhe name of a hypoLheLlcal machlne on our local neLwork When you Lype ln and run Lhls program on your machlne change Lhe hosL name Lo Lhe name of a machlne on your neLwork Make sure LhaL Lhe name you use ls Lhe fully quallfled l name of Lhe machlne Lo whlch you wanL Lo connecL 1o send daLa Lhrough Lhe sockeL Lo Lhe server Lcho CllenL slmply needs Lo wrlLe Lo Lhe rlnLWrlLer 1o geL Lhe servers response Lcho CllenL reads from Lhe 8uffered8eader 1he resL of Lhe program achleves Lhls lf you are noL yeL famlllar wlLh Lhe !ava plaLforms l/C classes you may wlsh Lo read 8aslc l/C 1he nexL lnLeresLlng parL of Lhe program ls Lhe whlle loop 1he loop reads a llne aL a Llme from Lhe sLandard lnpuL sLream and lmmedlaLely sends lL Lo Lhe server by wrlLlng lL Lo Lhe rlnLWrlLer connecLed Lo Lhe sockeL String userInput; while ((userInput stdIn.readLine()) ! null) out.println(userInput); System.out.println("echo: " in.readLine()); } 1he lasL sLaLemenL ln Lhe whlle loop reads a llne of lnformaLlon from Lhe 8uffered8eader connecLed Lo Lhe sockeL 1he readlne meLhod walLs unLll Lhe server echoes Lhe lnformaLlon back Lo LchoCllenL When readllne reLurns LchoCllenL prlnLs Lhe lnformaLlon Lo Lhe sLandard ouLpuL 1he whlle loop conLlnues unLll Lhe user Lypes an endoflnpuL characLer 1haL ls LchoCllenL reads lnpuL from Lhe user sends lL Lo Lhe Lcho server geLs a response from Lhe server and dlsplays lL unLll lL reaches Lhe endoflnpuL 1he whlle loop Lhen LermlnaLes and Lhe program conLlnues execuLlng Lhe nexL four llnes of code out.close(); in.close(); stdIn.close(); echoSocket.close(); 1hese llnes of code fall lnLo Lhe caLegory of housekeeplng A wellbehaved program always cleans up afLer lLself and Lhls program ls wellbehaved 1hese sLaLemenLs close Lhe readers and wrlLers connecLed Lo Lhe sockeL and Lo Lhe sLandard lnpuL sLream and close Lhe sockeL connecLlon Lo Lhe server 1he order here ls lmporLanL ?ou should close any sLreams connecLed Lo a sockeL before you close Lhe sockeL lLself 1hls cllenL program ls sLralghLforward and slmple because Lhe Lcho server lmplemenLs a slmple proLocol 1he cllenL sends LexL Lo Lhe server and Lhe server echoes lL back When your cllenL programs are Lalklng Lo a more compllcaLed server such as an P11 server your cllenL program wlll also be more compllcaLed Powever Lhe baslcs are much Lhe same as Lhey are ln Lhls program
1. Open a socket. MCA - Semester 4 MC0078 - 1ava Programming Assignment Set - 1 Roll No.
13
2. Open an input stream and output stream to the socket. 3. Read Irom and write to the stream according to the server's protocol. 4. Close the streams. 5. Close the socket. Cnly sLep 3 dlffers from cllenL Lo cllenL dependlng on Lhe server 1he oLher sLeps remaln largely Lhe same
8. hat is the advantage of CORBA over EJB? Ans 8: 1) CC88A works wlLh more Lhan [usL !ava L!8 can use llC buL whlle we can geL an L!8 cllenL Lo Lalk Lo a CC88A server we canL geL a CC88A cllenL Lo Lalk Lo an L!8 server 2) L!8 conLalners are Lyplcally much slower Lhan cusLom CC88A servers and sLlll end up uslng all Lhe dlsLrlbuLed programmlng Lrlcks you learned from CC88A when you wrlLe L!8s 1he blg myLh of L!8 programmlng ls LhaL you donL have Lo worry abouL scalablllLy lssues slnce Lhe conLalner does lL for you 1he LruLh ls LhaL you sLlll do because mosL conLalners blow 3) L!8 lsnL qulLe as flexlble as CC88A (see my L!8 advanLages laLer for Lhe downslde Lo Lhls) WlLh CC88A you can code [usL abouL any Lype of servanL ob[ecL you wanL wlLh L!8 you are llmlLed Lo whaLever Lhe conLalner supporLs and only a few models wlLh L!8 lLself (alLhough Lhese are preLLy flexlble ln Lhelr own rlghL) 4) L!8 conLalners are sLlll falrly new and you could easlly geL locked lnLo someLhlng LhaL evaporaLes a few years laLer Many of your CC88A C88s LhaL are avallable have been around for a long Llme and have a beLLer Lrack record 3) llnally Lhere are more servlces avallable wlLh CC88A 30 and Lhey are well deflned 1hls ls a debaLable one Lhough slnce mosL vendors canL seem Lo geL Lhese rlghL
now L!8 lve found has Lhe followlng advantages 1) CC88A ls Loo damn compllcaLed (LhaLs why lLs flexlble) compared Lo L!8 ?ou can whlp up an L!8 sysLem ln no Llme 1hrow ln all Lhe oLher nlce sLuff llke ServleLs !S !MS and any oLher !ava 1A you can flnd and youve goL some nlce gear for your dlsLrlbuLe apps 2) (lm golng Lo geL ln Lrouble for Lhls one ) C++ requlres Curu sLaLus [usL Lo code slmple CC88A servers L!8 requlres codlng moron sLaLus even for advanced sLuff 3) L!8 can Lalk Lo CC88A (alLhough only rellably on Lhe L!8 cllenL slde) buL noL Lhe oLher way around 1hls means LhaL you can code all your cllenLs ln L!8 and Lhen Lhe servers LhaL musL be fasL can be ln CC88A 1hls ls an lnsLallaLlon nlghLmare buL works well enough 4) !ava ls abouL 10 Llmes easler Lo code Lhan C++ for mosL people lke l sald you donL have Lo know much Lo wrlLe a !ava program (whlch why mosL people who llke belng called smarL llke uslng C++ and C and lC818An)