You are on page 1of 42

Table of Contents

Course Description 2
INTRO
:
Java API 3
LAB
:
Developin! "rap#ical $ser Interface
%"$I&

LAB 2
:
Resolvin! IP an' DN( )
LAB 3
:
Developin! $DP applications 22
LAB *
:
Developin! "roup+are applications 2)
LAB ,
:
Developin! TCP-IP applications 32
1 | 3 6
COURSE OUTLINE
COURSE : FN513 NETWORK PROGRAMMING
INSTRUCTIONAL DURATION : 15 WEEKS
CREDIT (S) : 3
PRE-REQUISITES(S) : FP301OBJECT ORIENTED PROGRAMMING
SYNOPSIS
NETWORK PROGRAMMING course focuses on the concept of sockets as means of using Internet
Protocol IP! to communicate "et#een machines an$ using %a&a programming language to esta"lish
the net#ork communication' The course intro$uces elements of net#ork programming an$ concepts
in&ol&e$ in creating net#ork applications using sockets' This opens up a #hole ne# class of
applications to programmers'
COURSE LEARNING OUTCOMES
(pon completion of this course) stu$ents shoul$ "e a"le to:
*' i$entif+ the specific t+pe of socket an$ port for communication to "e use$ in a gi&en net#ork
application' ,-!
-' construct appropriate t+pe of socket programming that meets the re.uirement of a net#ork
application' P/!
/' propose appropriate t+pe of internet socket interface in the $e&elopment of a gi&en net#ork
application' A/!
'
2 | 3 6
INTRO: Java API
Learnin! Outco.es
This 0a"sheet encompasses of acti&ities #hich is A) 1) an$ ,'
1+ the en$ of this la"orator+ session) +ou shoul$ "e a"le to:
*' Rea$ 2a&a API Application Programming Interfaces!
-' Write an$ generate our o#n API
!"#$!"%& S'()$!"% : Te3tPa$ #ith latest &ersion of %4K
Activit/ A
Acti&it+ Outcome: Rea$ an$ use %a&a API to check the e3isting of a file
P"'*%#+"% :
a. ,reate one 't3t file calle$ Lab1.txt in +our #orking fol$er' Write an application
name$ 5ile,hecking'2a&a that "ase$ on the Algorithm* an$ %a&a API *
pro&i$e$ "elo#' The application is a"le to check #hether the e3isting of a file'
Start
1.0 Instantiate object File
2.0 if file exists
2.1 print File is exist!
3.0 else
3.1 print File is not exist!
End
Al!orit#. : C#ec0in! t#e e1istin! of 2le
3 | 3 6












Java API : Class
File
4 | 3 6
Activit/ B
Activit/ outco.e:
To c#ec0 t#e e1istin! of a 2le an' creatin! ne+ 2le if t#e 2le 'on3t e1ist
P"'*%#+"% :
Modify your FileChecking.java based on the Algorithm 2 and the API
provided in ava API.
!sed "able# given as your guide.
Al!orit#. 2:
Start
1.0 Instantiate object File
2.0 if file exists
2.1 Instantiate object FileWriter named f t!at allo to
append
2.2 Instantiate object "rintWriter named p1
2.3 #all met!od println and $i%e inp&t to t!e met!od
parameter Soc'et "ro$rammin$ #o&rse
2.( #all met!od fl&s! and close t!e FileWriter and
"rintWriter object
3.0 Else
3.1 Instantiate object "rintWriter named p2 and
s&ppl) inp&t to t!e constr&ctor*s parameter
$reetin$.txt
3.2 #all met!od println and $i%e inp&t to t!e met!od
parameter Welcome to "ol)tec!ni+&e
3.3 #all met!od fl&s! and close t!e "rintWriter object
End
5 | 3 6
Table :
Class Constructors or return value Methods

4
5ile6riter
FileWriter,File file-
6,onstructs a 5ileWriter o"2ect gi&en a 5ile o"2ect'
FileWriter,File file. boolean append-
6,onstructs a 5ileWriter o"2ect gi&en a 5ile o"2ect'
6"oolean appen$ true 7 false to #rite ne# $ata #ithout
$eleting e3isting $ata'!
voi'%& close%&78us#%&
Print6riter

PrintWriter,Writer o&t-
6,reate a ne# PrintWriter) #ithout automatic line flushing
PrintWriter,File file-
6,reates a ne# PrintWriter) #ithout automatic line flushing)
#ith the specifie$ file'
voi'%& println ,Strin g x-
6Prints a 8tring an$
then terminates the
line'
%oid,- flush ,-
65lushes the stream
close ,-



T#e e1pecte' Output


6 | 3 6
Activit/ C
Activit/ outco.e: To print out 2le content into console
A'' eit#er one of t#e co'es snippet belo+ insi'e /ou FileChecking.java4

T#e co'es +ill print all t#e content in t#e 9greeting.t$t: into console4

Co'e (nippet
File/eader reader 0 ne File/eader,m)file-1
2&ffered/eaderbr 0 ne 2&ffered/eader,reader-1
Strin$ str 0331
Strin$ content0br.read4ine,-1
!ile,content!0n&ll-
5
str60content637n31
content0br.read4ine,-1
8 S)stem.o&t.println,str-1


Co'e (nippet 2
c!ar 9: data 0 ne c!ar91000:1
File/eader reader 0 ne File/eader,m)file-1
for,int i 001 i;m)file.len$t!,-1 i66-
data9i: 0 ,c!ar-reader.read,-1
Strin$ content 0 ne Strin$,data-1
S)stem.o&t.println,content.trim,--1



LAB ;1ercise: Intro to Java API
I,-)"+*).',-:
Ans#er all .uestions "ase$ on %a&a API'
In or$er to make sense of program listing +ou ha&e #ritten in the past it is a goo$ i$ea to "eing
each program #ith the follo#ing sections:
<=
>&t!or?
"ro$ram @itle?
Aescription?
Aate?
Bersion n&mber?
=<
,omment +our co$e9 Trust me it:s #orth the e3tra effort'
In$ent +our co$e to increase clarit+'
;uestion *:
,omplete the source co$e gi&en "elo#' <ou are re.uire$ to refer to the 2a&a API from
+our computer'
<=
>&t!or?
"ro$ram @itle?
Aescription?
Aate?
=<
import ja%a.io.=1
p&blic class File>pi extends File
5
<<Bariables declaration
<<declare object FileWriter and File/eader
FileWriter f1
File/eader fr1
"rintWriter p1
<< #onstr&ctor to create file object t!at in!erited from File class
p&blic File>pi,Strin$ s- t!ros Exception
5
s&per,s-1
p 0 ne "rintWriter,f 0ne FileWriter,s. tr&e--1
8
<< Instantiate
objects through
constructor
<==
<< @!is met!od is &sed to create a ne file in t!e c&rrent director)
p&blic %oid createFile,- t!ros ICException
5
/* Start:
1. Instantiate object PrintWriter
End
*/
8
-- @!is met!od is &sed to create a ne file in t!e c&rrent director)
p&blic %oid createFile,Strin$ s- t!ros ICException
5
/* Start:
1. Instantiate object File
1. instantiate object PrintWriter
End
*/
File fail 0 ne File,s-1
p 0 ne "rintWriter,fail-1
8
<<@!is met!od is &sed to rite text into file
p&blic %oid riteFile,Strin$ s-
5
/*
Start:
1. Write into file
2. Ino!e "ethod flush#$
End
*/
8
<< Instantiate
objects through
"ethod
<< Instantiate
objects through
"ethod




-- @!is met!od is &sed to read text from file
p&blic Strin$ readFile,- t!ros Exception
5
/* Start:
1. instantiate object File%eader
2. create arra& di"ention
'. loop until the end of the file
'.1 set char () to int ** + int is a alue fro"
"ethod read in File%eader class
,. conert char () to String using alue-f"ethod in
String class
.. return String
End
*/
8
p&blic %oid close,- t!ros ICException
5
p.close,-1
fr.close,-1
f.close,-1
8
p&blic static %oid main,Strin$ 9: ar$- t!ros Exception
5
File>pifileapi 0 ne File>pi,3$reetin$.txt3-1
if,fileapi.exists,--
5
S)stem.o&t.println,3File exists!3-1
fileapi.riteFile,3"&traja)a3-1
8
S)stem.o&t.println,fileapi.readFile,--1
Fileapi.close,-1
8
8
LAB : Developin! "rap#ical $ser
Interface %"$I&
Learnin! Outco.es %CLO :3&
This 0a"sheet encompass of acti&it+ *A'
1+ the en$ of this la"orator+ session) +ou shoul$ "e a"le to:
*' ,reating G(I an$ a$$ing 0isteners to the G(I
-' Appl+ing custom API 5ileApi'2a&a!
!"#$!"%& S'()$!"% : Te3tPa$ #ith latest &ersion of %4K
Activit/ A
Acti&it+ Outcome: ,reating G(I an$ A$$ing 0isteners to t h e G( I
P"'*%#+"% :
1ase$ on 5igure * an$ Ta"le *) complete the source co$e name$ M+Gui'
(se the 2a&a API to help +ou'

5i!ure : "rap#ical $ser Interface for </"ui4=ava

Table : Co.ponents in
</"ui4=ava
Co.ponents (ub Co.p (ub>?sub Co.p Listener Descriptions
J5ra.e fra.e La/out @
Bor'erLa/out%& (iAe
@ %,BB7*BB&
Location: center of +in'o+s
NORTH
JPanel panelCnort#
JLabel labelCtitle T#e constructor use' is
JLabel%Icon i.a!e& I.a!e na.e
is 0pt4pn!
CENTER
JPanel panelCcenter
JTe1tArea taC'ata

(iAe of te1t area %,73,&
JscrollPane pane
EAST
JPanel panelCeast La/out @ "ri'La/out%D7&
JButton EF btn ActionListener T#e button te1ts are :(ave:7 9Reset:
an' 9Close:
SOUTH
JPanel panelCsout#
JLabel lblCinput T#e te1t is 9<essa!e::
JTe1t5iel'
t1tCinput
Ge/Listen
er>?
Ge/A'apte
T#e colu.n siAe is 3B
JButton btnCsen'

ActionListener

So&rce code? D)E&i.ja%a import ja%a.at.=1
import ja%a.at.e%ent.=1
import ja%ax.sin$.=1
p&blic class D)E&i implements >ction4istener
5
<<component on t!e center
F@ext>rea taGdata1
<<component on t!e east
F2&tton btn1
<<#omponents on t!e so&t!
F4abel lblGinp&t1 F@extField
txtGinp&t1 F2&tton btnGsend1
p&blic D)E&i,Strin$ title-
5
init,title-1
8
p&blic %oid init,Strin$ title-
5
FFrame frame 0 ne FFrame,title-1
<<4a)o&t of FFrame frame.set4a)o&t,ne
2order4a)o&t,--1
<<siHe of FFrame
frame.setSiHe,I00.(00-1
<< #enter on t!e comp&ter screen
frame.set4ocation/elati%e@o,n&ll-1
<<#odes for ot!er components
<=
/-%01
=<
//2our code here
frame.add,panelGnort!. 2order4a)o&t.JC/@K-1
<=
3E/0E%
=<
F"anel panelGcenter 0 ne F"anel,-1
//2our code here
FScroll"ane pane 0 ne FScroll"ane,taGdata-1
panelGcenter.add,pane-1
frame.add,panelGcenter. 2order4a)o&t.#EJ@E/-1
<=
E4S0
=<
F"anel panelGeast 0 ne F"anel,ne Erid4a)o&t,L.1--1
Strin$ 9: btnGnames 0 53Sa%e3. 3/eset3. 3#lose381
for,Strin$ btnJame ? btnGnames-
5
//&our code here
btn.add>ction4istener,t!is-1
panelGeast.add,btn-1
8
frame.add,panelGeast. 2order4a)o&t.E>S@-1
<=
S-501
=<
F"anel panelGso&t! 0 ne F"anel,ne Flo4a)o&t,--1
//2our code here
txtGinp&t.addMe)4istener,ne Me)>dapter,-
5
p&blic %oid 'e)"ressed,Me)E%ent e%t-
5
if,e%t.$etMe)#ode,-00Me)E%ent.BMGEJ@E/-
5
taGdata.append,txtGinp&t.$et@ext,-637n3-1
txtGinp&t.set@ext,33-1
8
8
8-1
btnGsend 0 ne F2&tton,3Send3-1
btnGsend.add>ction4istener,t!is-1
panelGso&t!.add,lblGinp&t-1
panelGso&t!.add,txtGinp&t-1
panelGso&t!.add,btnGsend-1
frame.add,panelGso&t!. 2order4a)o&t.SCN@K-1
frame.setBisible,tr&e-1
8
p&blic %oid action"erformed,>ctionE%ent e-
5
if,e.$et>ction#ommand,-.e+&als,3Send3--
5
taGdata.append,txtGinp&t.$et@ext,-637n3-1
txtGinp&t.set@ext,33-1
8
if,e.$et>ction#ommand,-.e+&als,3Sa%e3--
5
8
if,e.$et>ction#ommand,-.e+&als,3/eset3--
5
8
if,e.$et>ction#ommand,-.e+&als,3#lose3--
5
8
8
p&blic static %oid main,Strin$ 9: ar$s-
5
ne D)E&i,3first $&i3-1
8
8


LAB ;1ercise : Appl/in! "$I an' custo. API
I,-)"+*).',-:
Ans#er all .uestions "ase$ on G(I an$ %a&a API'
In or$er to make sense of program listing +ou ha&e #ritten in the past it is a goo$ i$ea to "eing
each program #ith the follo#ing sections:
<=
>&t!or?
"ro$ram @itle?
Aescription?
Aate?
Bersion n&mber?
=<
,omment +our co$e9 Trust me it:s #orth the e3tra effort'
In$ent +our co$e to increase clarit+'
;uestion *:
Appl+ing custom API 5ileApi'2a&a!
<our task is to appl+ +our o#n API file to the G(I file'
In the metho$ ActionPerforme$ in the M+Gui'2a&a) use 5ileApi to sa&e all the contents in
the %Te3tArea into file'



LAB 2: Resolvin! IP an' DN(
Learnin! Outco.es %CLO :2&
This 0a"sheet encompasses of acti&ities #hich is -A) -1) an$ -,'
1+ the en$ of this la"orator+ session) +ou shoul$ "e a"le to:
*' Resol&ing IP form 4N8
-' Resol&ing 4N8 from IP
/' 4e&eloping custom API for Resol&ing IP an$ 4N8 an$ appl+ing to G(I
!"#$!"%& S'()$!"% : Te3tPa$ #ith latest &ersion of %4K
Activit/ 2A
Acti&it+ Outcome: Resol&ing IP from 4N8
P"'*%#+"%:
,omplete the source co$e * "+ using metho$s pro&i$e$ in Ta"le * "elo#'
The e3pecte$ output is as sho#n in 5igure *'
8ource co$e *: M+Resol&ing4N8*
Ta"le *: API for class InetA$$ress
Class Return value <et#o' ;1planation
InetA$$ress

static InetA$$ress

get1+Name8tring host!

4etermines the IP
a$$ress of a host) gi&en the host=s name
8tring get>ostA$$ress! Returns the IP a$$ress string in te3tual
presentation'
The E3pecte$ Output:

5igure *: E3pecte$ Output for M+Resol&ing4N8*

Activit/ 2B
Acti&it+ Outcome: Resol&ing IPs from 4N8
P"'*%#+"%:
The source co$e - "elo# is a"le to print IP a$$resses from an+ 4N8'
,omplete the source co$e - "+ using API pro&i$e$ in Ta"le -'
The e3pecte$ output sho#s in Ta"le -'
8ource co$e -: M+Resol&ing4N8-

Class Return value <et#o' ;1planation
InetA$$ress

static InetA$$ress

getAll1+Name8tring
host!
4etermines the IP
a$$ress of a host) gi&en the host=s name
8tring get>ostA$$ress! Returns the IP a$$ress string in te3tual
presentation'
Ta"le *: API for class InetA$$ress
The e3pecte$ output:

5igure-: The e3pecte$ output for M+Resol&ing4N8-

Activit/ 2C
Acti&it+ Outcome: Resol&ing 4N8 from IP
P"'*%#+"%:
In the M+Resol&ing4N8* in source co$e*) suppl+ this input ?-@A'BC'*DC'*BE in the console'
<ou are re.uire$ to a$$ another metho$ name$ get>ostName ! into the source co$e'
Refer to the 2a&a API in the package 2a&a'net'
The e3pecte$ output is as sho#n in 5igure /'


5igure /: The e3pecte$ output for in&oking metho$ get >ostName!'

LAB ;1ercise 2: Resolvin! DN( fro. IP %"$I
app&
I,-)"+*).',-:
Ans#er all .uestions'
,omment +our co$e9 Trust me it:s #orth the e3tra effort'
In$ent +our co$e to increase clarit+'
;uestion *:
i' Write the custom API "elo# name$ M+Resol&er'2a&a' source co$e /!
ii' Appl+ the API into M+G(I'
iii' The metho$ that +ou nee$ to in&oke is resol&eIPs8tring $ns!'
i&' The location of the co$e nee$ to "e a$$e$ is as sho#n in the co$e
snippet *'
&' 5igure F sho#s the e3pecte$ output
Note: ,op+ M+Gui'2a&a from +our pre&ious $irector+ into +our current $irector+'
8ource co$e /: M+Resol&er
import 2a&a'net'InetA$$ressG
pu"lic class M+Resol&er
H
pu"lic static 8tring resol&eIP8tring $ns! thro#s E3ception
H InetA$$ress inet I InetA$$ress'get1+Name$ns!G
return inet'get>ostA$$ress!G
J
pu"lic static 8tring KL resol&eIPs8tring $ns! thro#s E3ception
H InetA$$ress KL inets I InetA$$ress'getAll1+Name$ns!G
8tring ipsKL I ne# 8tringKinets'lengthLG
for int i I @G iMinets'lengthGiNN!
ipsKiLIinetsKiL'get>ostA$$ress!G
return ipsG
J
pu"lic static 8tring resol&e4N88tring ip! thro#s E3ception
H InetA$$ress inet I InetA$$ress'get1+Nameip!G
return inet'get>ostName!G
J
pu"lic static &oi$ main8tring ''' args! thro#s E3ception
H 8tring ip*IM+Resol&er'resol&eIPO###'poliku'e$u'm+O!G
8+stem'out'printlnip*!G
8+stem'out'printlnOO!G
8+stem'out'printlnM+Resol&er'resol&eIPO###'face"ook'comO!!G
8+stem'out'printlnOO!G
for8tring ip : M+Resol&er'resol&eIPsO###'gmail'comO!!
8+stem'out'printlnip!G
8+stem'out'printlnOO!G
8tring $ns*IM+Resol&er'resol&e4N8OPA'*D*'-/D'/-O!G
8+stem'out'printlnip*!G
8+stem'out'printlnOO!G
8+stem'out'printlnM+Resol&er'resol&e4N8O-@A'BC'*DC'*BO!!G
J
J
,o$e 8nippet *

The E3pecte$ Output:

5igure F: The E3pecte$ Output for Resol&ing IP from 4N8

LAB 3: Developin! $DP applications
Learnin! Outco.es %CLO :2&
This 0a"sheet encompasses of acti&ities #hich is /A) /1) an$ /,'
1+ the en$ of this la"orator+ session) +ou shoul$ "e a"le to:
*' 4e&eloping sen$er an$ recei&er console applications
-' 4e&eloping custom API for sen$ing an$ recei&ing messages
/' 4eplo+ing the custom API to G(I
!"#$!"%& S'()$!"% : Te3tPa$ #ith latest &ersion of %4K
Activit/ 3A
Acti&it+ Outcome: 4e&eloping sen$er console applications
P"'*%#+"%:
The source co$e * "elo# is the console application using (4P protocol to
sen$ $ata to another computer' ,omplete the source co$e "+ using
constructor an$ metho$ pro&i$e$ in the Ta"le *'
The e3pecte$ output is as sho# in 5igure *'
N')%:
The localhost in the source co$e nee$ to "e change$ #ith the IP pro&i$e$ "+ the
instructor' <our message #ill "e $ispla+e$ at the instructor:s monitor'

8ource co$e *: M+(4Psen$
Ta"le *: %a&a API for M+(4Psen$
,lass ,onstructors an$
return &alue
Metho$ E3planation
4atagramPacket 4atagramPacket"+te KL "uf)
int length) InetA$$ress a$$ress)
int port!
,onstructs a $atagram packet for
sen$ing packets of length length to
the specifie$ port num"er on
the specifie$ host'
4atagram8ocket &oi$ sen'%Data!ra.Pac0et
p&
8en$s a $atagram packet from this
socket

The E3pecte$ Output:
5i!ure%a&(en'er%participant3s co.puter& 5i!ure%b&Receiver %Instructor3s
co.puter&

5igure *: (4P console application


Activit/ 3B
Acti&it+ Outcome: 4e&eloping recei&er console applications
P"'*%#+"%:
1ase$ on 5igure "!Recei&er) complete source co$e - name$ M+(4Precei&e'2a&a'
The 2a&a API is pro&i$e$ in Ta"le -''
8ource ,o$e -: M+(4Precei&e
Ta"le -: %a&a API for receri&er
,lass ,onstructors an$
return &alue
Metho$ E3planation
4atagramPacket 4atagramPacket"+te KL "uf) int length!

,onstructs a
4atagramPacket for
recei&ing packets of
length length'
&oi$
recei&e4atagramPacket p!

Recei&es a $atagram
packet from this socket
8tring 8tring"+teKL "+tes!
,onstructs a ne# 8tring
"+ $eco$ing the specifie$
arra+ of "+tes using the
platform=s $efault charset'
8tring trim! Returns a cop+ of the
string) #ith lea$ing an$
trailing #hitespace
omitte$'
Activit/ 3C
Acti&it+ Outcome: 4e&eloping custom API for sen$ing an$ recei&ing message
P"'*%#+"%:
1ase$ on the API in the source co$e / name$ M+(4PAPI'2a&a) +ou
are re.uire$ to $e&elop t#o console application name$ ApplicationA.java an$
ApplicationB.java' The application #ill pro$uce the output like 5igure -'
8ource co$e /: M+(4PAPI
5igure -: 8en$ing an$ Recei&ing form t#o console applications'
LAB ;1ercise 3: (en'in! an' Receivin! App
%"$I&
I,-)"+*).',-:
Ans#er all .uestions'
,omment +our co$e9 Trust me it:s #orth the e3tra effort'
In$ent +our co$e to increase clarit+'
;uestion *:
The G(I in the 5igure /A an$ 5igure /1 sho# the chat applications'
<ou are re.uire$ to cop+ the M+Gui from the pre&ious la" an$ rename the MyGui to
MyGuiA an$ MyGuiB'
N')%:
The rename of the files re.uire$ mo$ification of the class name an$ constructor'
5ollo# instructions from instructor'
5igure /A 5igure /1
LAB *: Developin!
"roup+are applications
Learnin! Outco.es %CLO :3&
This 0a"sheet encompasses of acti&ities #hich is FA) an$ F1'
1+ the en$ of this la"orator+ session) +ou shoul$ "e a"le to:
*' 4e&eloping multicast sen$er an$ recei&er console applications
-' 4e&eloping custom API for group#are applications
/' 4eplo+ing the custom API to G(I
!"#$!"%& S'()$!"% : Te3tPa$ #ith latest &ersion of %4K
Activit/ *A
Acti&it+ Outcome: 4e&eloping sen$er an$ recei&er console applications for group 7 multicast net#ork
P"'*%#+"%:
,omplete source co$e * an$ source co$e - "elo#' Refer to the gi&en
API ta"le' 1oth applications #ill use IP a$$ress ?--C'C'C'CE an$ port no I
-@-@' The e3pecte$ output sho#s in 5igure *'
8ource co$e *: M+Group8en$


8ource co$e -: M+GroupRecei&e

The E3pecte$ Output:
5i!ure : $DP console application

Ta"le *: %a&a API for recei&er
,lass ,onstructors an$ return
&alue
Metho$ E3planation
InetA$$ress static InetA$$ress get1+Name8tring host! 4etermines the IP
a$$ress of a host)
gi&en the host=s
name'
Multicast8ocket Multicast8ocketint port! 2oinGroupInetA$$ multicastA$$! ,reate a multicast
socket an$ "in$ it to a
specific
port'
4atagramPacket

4atagramPacket"+te KL "uf)
int length)
InetA$$ress a$$ress) int port!

,onstructs a $atagram
packet for sen$ing
packets of length
length to the
specifie$ port num"er
on the specifie$ host'
4atagramPacket"+te KL "uf)
int length!
,onstructs
a 4atagramPacket for
recei&ing packets of
length length'
4atagram8ocket

&oi$ sen$4atagramPacket p! 8en$s a $atagram
packet from this
socket'
&oi$ recei&e4atagramPacket p!
Recei&es a
$atagram packet
from this socket'
8tring 8tring"+teKL "+tes! ,onstructs a
ne# 8tring "+
$eco$ing the
specifie$ arra+ of
"+tes using the
8tring trim! Returns a cop+ of
the string) #ith
lea$ing an$ trailing
#hitespace omitte$'
Activit/ *B
Acti&it+ Outcome: 4e&eloping sen$er an$ recei&er console applications for group 7 multicast net#ork
P"'*%#+"%:
,reate a ne# file GroupA8en$ an$ Group1Recei&e "+ appl+ing the API M+GroupAPI'
8ource ,o$e /: M+GroupAPI




LAB ;1ercise *:"roup+are-<ulticast %"$I
app&
I,-)"+*).',-:
Ans#er all .uestions'
,omment +our co$e9 Trust me it:s #orth the e3tra effort'
In$ent +our co$e to increase clarit+'
;uestion *:
,op+ the original MyGui file together #ith the image an$ paste them into current $irector+'
Rename MyGui into MyGuiMultiCastA an$ MyGuiMultiCastB'
Then) make them as group chatting applications "+ using the gi&en MyGroupAPI in 8ource co$e /'
N')%:
The rename of the files re.uire$ mo$ification of the class name an$ constructor'

LAB ,: Developin! TCP-IP applications
Learnin! Outco.es %CLO :3&
This 0a"sheet encompasses of acti&ities #hich is CA) an$ C1'
1+ the en$ of this la"orator+ session) +ou shoul$ "e a"le to:
*' 4e&eloping T,P7IP ser&er an$ client console applications
-' 4e&eloping custom API for group application
/' 4eplo+ing the custom API to G(I
!"#$!"%& S'()$!"% : Te3tPa$ #ith latest &ersion of %4K
Activit/ ,A
Acti&it+ Outcome: Esta"lishing connection using console applications for client7ser&er net#ork T,P7IP!
P"'*%#+"%:
,omplete source co$e * an$ #rite source co$e - "elo#'
Refer to the 2a&a API ta"le gi&en' The e3pecte$ output is as sho#n in 5igure *'
8ource co$e *: M+8er&er


8ource co$e -: M+,lient

The e3pecte$ output:


5igure *: Output for M+8er&er an$ M+,lient
i.port =ava4io4HI
i.port =ava4net4(oc0etI
i.port =ava4net4InetA''ressI
i.port =ava4net4(erver(oc0etI
public class </Client
J
public static voi' .ain%(trin! EF ar!s& t#ro+s ;1ception
J
InetA''ress inet K InetA''ress4!etB/Na.e%Llocal#ostL&I
(oc0et soc0et2 K ne+ (oc0et%inet7 2B2B&I
--receive fro. server
Input(trea. is K soc0et24!etInput(trea.%&I
BuMere'Rea'er br K ne+ BuMere'Rea'er %ne+ Input(trea.Rea'er
%is&&I
(trin! .s! K br4rea'Line%&I
(/ste.4out4println%Lfro. server : LN.s!&I
O
O
Activit/ ,B
Acti&it+ Outcome: 4e&eloping sen$er an$ recei&er console applications for client7ser&er net#ork T,P7IP!
P"'*%#+"%:
Write t#o applications name$ MyServerA.java an$ MyServerB.java for the interaction "et#een
ser&er an$ client as in 5igure - "elo#'
(se the API file name$ MyTCPAPI pro&i$e$ in 8ource co$e /'
The e3pecte$ output is as in 5igure /'

5igure - : ,ommunication "et#een M+8er&erA an$
M+,lientA M+8er&er1!

The e3pecte$ output:

5igure /: Output for M+8er&erA an$ M+8er&er1
(ource Co'e 3: </TCPAPI4=ava
LAB ;1ercise ,:TCP-IP co..unications %"$I
app&
I,-)"+*).',-:
Ans#er all .uestions'
,omment +our co$e9 Trust me it:s #orth the e3tra effort'
In$ent +our co$e to increase clarit+'
;uestion *:
,op+ MyGUI file from pre&ious acti&it+) rename the MyGui file to MyGuiServer.java an$
MyGuiClient.java'
(se the API file) MyTCPAPI.java into MyGuiServer.java an$ MyGuiClient.java'
The outputs of the applications shoul$ "e as in 5igure F'
N')%:
The rename of the
file s re.uire$
mo $ification of the
cla ss name an$
con structor'




5igure F: Output for MyGuiServer.java an$ MyGuiClient.java

You might also like