Network Programming in Java
Network Programming in Java
"uto ation
Development platform
Unix/Linux/Windows available in t e department or computing center !ore information ttp"//www#tkk#fi/cc/computers/ Using $un JD% 'our program must run on desktop at !aari()
Deplo&ment platform
Using ID- *-clipse. Net/eans. J0reator ###, Use existing libraries *)pac e 0ommons ###, Use of existing protocol implementations is forbidden )utomate compiling *)pac e )nt, and testing *JUnit, /ot programs are available in 1%% linux mac ines
1r& version control s&stems to s are &our code wit in &our group
ttp"//goblin#tkk#fi/c22/tutorials/svn# tml
Information sources
1oda&4s slides and examples $un Java Documentation -xamples and tutorials available via searc engines $end mail to assistants *if ever&t ing else as failed,
/asic concepts
-nvironment 5andling $treams 5andling 0 annels 5andling b&te arra&s 6esolving ostname 5andling address information 0reating $ockets $ending and receiving data using blocking / non(blocking met ods
,
7r use t e existing packages like" ( )pac e 0ommons 0LI. see ttp"//commons#apac e#org/cli/
6esolve ostname
1ransform a s&mbolic name into a protocol(specific address $elect t e most suitable implementation for t e specific task Inet)ddress class for 3+(bit and 8+9(bit IP addresses used for unicast or multicast traffic Inet$ocket)ddress class is an implementation for t e IP address and port number pair used b& sockets for binding and connecting :ava#net#Inet)ddress :ava#net#Inet$ocket)ddress
.
)PI classes
&
0onnection(oriented *10P,
0reates a stream socket and connects it to t e specified port number at t e specified IP address# Write into 7utput$tream using suitable $tream writers
:ava#net#$ocket#get7utput$tream*,
)%
0onnectionless *UDP,
:ava#net#Datagram$ocket*int port,
0onstructs a datagram socket and binds it to t e specified port on t e local ost mac ine#
0onstructs a datagram packet for sending packets of lengt to t e specified port number on t e specified $ends a datagram packet from t is socket#
ost#
:ava#net#Datagram$ocket#send*DatagramPacket p,
))
6eceiving *blocking,
Input$tream $ocket#getInput$tream*, 6ead Input$tream using suitable classes Datagram$ocket#receive*DatagramPacket pPacket, 6eceives a datagram packet from t is socket# 1 e DatagramPacket contains t e b&tes transmitted# 1o modif& socket be avior c eck t e setter met ods of t e specified implementation
)$
)*
)+
),
0oncurrenc&
-vent /ased *$ingle 1 read 5andling man& connections,
See event #ased sol!tion ro' e,a'ples !sing java.nio.Channels
Using 1 reads
// // 3eceiver-hread i'ple'ents 3!nna#le inter ace 3eceiver-hread reveicerConnection = new 3eceiver-hread(); receiver = new -hread(reveicerConnection); receiver.start();
=or t e beginners read tutorials like ttp"//:ava#sun#com/docs/books/tutorial/essential/concurrenc&/ ttp"//:ava#sun#com/:+se/8#>#?/docs/guide/concurrenc&/index# tml ttp"//www#ibm#com/developerworks/edu/:(dw(:avat read(i# tml
)-
7t ers *8,
=or example 7bserver @ 7bservable pattern can be used to deliver t e received data for multiple users i#e# $erver must replicate data for multiple receivers"
$ocket container *source, implements 7bservable interface 0lient connection creates an instance of t e client container t at implements an 7bserver interface W en client container is created t e client observer is registered to t e observable source
).
7t ers *+,
6emember alwa&s to terminate program and release resources
-o handle sh!tdown signal !se addSh!tdown4ook() 'ethod or 3!nti'e class 3!nti'e.get3!nti'e().addSh!tdown4ook(new -hread() $ p!#lic void r!n() $ S&ste'.o!t.println ((Called at sh!tdown.(); % %); *ther alternative is to !se handle() 'ethod in s!n.'isc.Signal class to catch signals p!#lic static void 'ain(String56 args) throws +,ception $ Signal.handle(new Signal((I7-(), new Signal4andler () $ p!#lic void handle(Signal sig) $ S&ste'.o!t.println( (3eceived a interr!pt88(); % %); // %
# $%%& 'i((o Kiis(i )/