You are on page 1of 10

1

CHlONC 2

LlP TR!NH CL!ENT\SERvER

!. Tlng quan vl llp trinh mlng:

1. Cili thilu
tinh trn !nternet lin llc vli nhau dng giao thlc TCP (Transmission Control Protocol)
holc UDP (User Datagram Protocol).
Khi vilt chllng trinh ]ava lin llc trn mlng la vilt l llp lng dlng (llp application)
va sl dlng nhlng llp trong gi java.net dl tru xult llp TCP/UDP (llp transport).














Llp trinh client/server La vilt lng dlng trn mlng glm hai chllng trinh chllng
trinh client va chllng trinh server. Chllng trinh client gli u clu tli chllng trinh
server, ct server xl l u clu va trl klt qla vl cho ct client. Ct server c thl phlc vl
dlng thli nhilu u clu cla cc ct client.
Llp trinh WEB la trllng hlp dlc bilt cla llp trinh client/server. Ct client la ct Browser
(trinh dult web), ct server la Web Server nhln u clu trang web tl Browser, Web Server
tim trang web gli vl cho Browser, brower thlc thi trang web hiln klt qla trn man hinh
client. Browser va web server lin llc qua giao thlc HTTP thng qua clng mlc dlnh la 80.
Trang Web la file .html vilt blng ngn ngl HTL (HperText arkup Language), Browser sl
thng dlch trang web. Khi client muln u clu trang web, dng browser go va chuli c dlng
sau
http://NameServer:port/path/file.html
http la giao thlc lin llc gila Browser va Web server, NameServer la tn may web
server dang thlc thi, port la sl hilu clng web server sl dlng, path/file.html la trang
web dllc yu clu.
vi dl http//www.microsoft.com/index.html (khng c port thi mlc dlnh la 80)

Chuli na gli la chuli dlnh vl tai ngun URL (Uniform Resource Locator) dng dl xc
dlnh tai ngun trn mlng !nternet. Ngoai giao thlc HTTP cn c thl sl dlng cc giao thlc
khc nhl la FTP, Copher, File, va News. Hiln c cc Browser thng dlng nhl !nternet Explorer,
Netscape Navigator, va cc Web Server nhl!!S (!nternet !nformation Server), PWS (Personal Web
Server), ]RUN, Tomcat,.


2

2. Ciao thlc TCP/UDP
a) Ciao thlc TCP:
Thilt llp klt nli
lm blo dl lilu gli, dllc nhln chinh xc va dng thl tl, ngllc lli sl bo lli.
Cc giao thlc Hpertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), va Telnet la nhlng
lng dlng dng klt nli TCP. tinh lin llc dng giao thlc TCP gilng nhl con nglli lin
llc blng diln tholi.
b) Ciao thlc UDP:
Khng klt nli
Khng dlm blo dl lilu gli, dllc nhln chinh xc va dng thl tl,
Cli/nhln dl lilu dlng gi (datagram), cc gi gli/nhln dlc llp vli nhau.
UDP nhanh hln TCP vi khng kilm tra dl lilu, khng cln klt nli. Nhlng lng dlng nhl
hli gil, nhln tin, llnh ping nn dng UDP. tinh lin llc dng giao thlc UCP gilng nhl
con nglli lin llc blng thl tin.
Llu c nhlng firewalls va routers khng cho php gli/nhln gi UDP do admin da dlt clu
hinh clm gi UDP.
. la chl !P, clng (Port), socket:
la chl !P la sl 32 bit ma !P dng dl xc dlnh m tinh.
Clng la sl 16 bit ma TCP/UDP dng dl xc dlnh lng dlng trn m tinh sl nhln dl
lilu. Dl lilu khi gli di, dllc gli km theo dla chl !P cla m nhln va clng ma lng
dlng trn m nhln sl dlng. Sl hilu clng trong phlm vi tl 0 . 6S,S3S, nhlng sl
hilu clng tl 0 dln 1023 nn hln chl sl dlng vi chng da dllc dng cho nhlng
dlch vl thng dlng nhl HTTP, FTP.

Dlch vl Clng
FTP (truln /nhln file) 21
HTTP (web) 80
TELNET (tru xult m tinh tl
xa)
23
STP (gli mail) 2S
POP3 (ll mail) 110

Socket: la clu trc dl lilu llu trl cc thng tin dng dl klt nli, dl lilu dllc
gli/nhln thng qua socket. Trong lin llc TCP, lng dlng server klt bulc mlt socket vli
mlt clng cl thl, nghia la lng dlng server dang k nhln tlt cl dl lilu gli cho clng d.

Trong lin llc UDP, dl lilu gli/nhln dlng gi. Ci chla sl hilu clng, UDP sl gli gi cho
lng dlng tllng lng.
3

!!. Sl dlng URL (Uniform Resource Locator)
1. Khai nilm
La mlt tham chilu dln mlt tai ngun trn mlng (dla chl tai ngun trn mlng), Web
Browser holc cc lng dlng khc dng URL dl tim tai ngun trn mlng. Ci java.net c
llp URL dng dl bilu diln dla chl URL
URL c dlng chuli glm giao thlc dng dl tru xult tai ngun va tn tai ngun.
vi dl


C thl dng cc giao thlc khc nhl File Transfer Protocol (FTP), Copher, File, News.
Tn tai ngun la thng tin dl dl dl xc dlnh tai ngun, thng tin na phl thulc vao
giao thlc sl dlng, nhlng thllng c cc thng tin sau

Host Name Tn m chla tai ngun.
ilename ung dn ti ti nguyn
Port Number Sl hilu clng dng khi klt nli
i vi http, filename mc djnh l index.html

2. Tlo va sl dlng dli tllng URL

Phllng thlc nghia
URL(String spec) Tlo dli tllng URL tl mlt chuli URL
URL(String protocol, String host, int port,
String file)
Tlo dli tllng URL tl protocol, host, port va tn
file
URL(String protocol, String host, String file)

Tlo dli tllng URL tl protocol, host, port va tn
file, dng port mlc dlnh
Object getContent() Ll nli dung cla URL
String getFile() Ll tn file cng dllng dln cla URL
String getHost() Ll tn m cla URL
int getPort() Ll sl hilu clng cla URL
String getProtocol() Ll tn protocol cla URL
!nputStream openStream() Klt nli tli url va ml lulng nhlp dl dlc thng
tin tl url
URLConnection openConnection() l klt nli tli URL

Bai tlp 1: vilt chllng trinh dlc file tl xa thng qua web server
4
Bai tlp 2: vilt chllng trinh download file thng qua web server

!!!. Llp trinh TCP Sl dlng socket
1. Khai nilm:
Socket la mlt dlu trong klt nli lin llc hai chilu gila hai chllng trinh trn mlng. Ci
java.net cung clp llp Socket dl cai dlt klt nli phia client va ServerSocket dl cai dlt klt
nli phia server.
SocketServer dli, llng nghe u clu klt nli tl SocketClient



Khi chlp nhln klt nli, SocketServer tlo mlt socket mli klt bulc vli mlt clng
khc dl phlc vl cho client da klt nli, trong khi SocketServer vln tilp tlc llng nghe u
clu klt nli tl cc client khc.




Sau d server va client sl lin llc vli nhau thng qua socket cla chng.
Llu
+ Client dng clng clc bl trn m cla client.
+ Nlu klt nli tli Web Server thi llp URL thich hlp hln llp Socket, thlc ra llp URL cng
sl sl dlng llp socket

2. Clu trc chllng trinh client

STT Thao tac Sl dlng llnh
1 l socket (dng dl klt nli vli
server)
Socket cs = new Socket("ServerName",
port),
2 l lulng dlc (dng dl dlc dl do
server gli)
BufferedReader in = new BufferedReader
(new !nputStreamReader(
cs.get!nputStream() )),
3 l lulng ghi (dng dl gli dl cho
server)
PrintWriter out = new PrintWriter
(cs.getOutputStream(), true),
4 l lulng dlc dl tl ban phim
(nlu cln)
BufferedReader std!n = new
BufferedReader (new
!nputStreamReader(Sstem.in) ),
S Cli dl cho server out.println(String str),
6 Ll dl do server gli String str=in.readLine(),
7 ng cc lulng dlc, ghi, dng
socket
in.close(),out.close(), sdt!n.close(),
cs.close(),
S
Chi ch:
Reader, writer c thl dlc ghi ki tl Unicode qua socket
ng lulng dlc/ghi, sau d mli dng socket

* Chllng trinh client mlu
import java.io.*,
import java.net.*,
public class Client
{
public static void main(String|] args) throws !OException
{
Socket cs = new Socket("ServerName", 1234),
PrintWriter out = new PrintWriter(cs.getOutputStream(), true),
BufferedReader in = new BufferedReader(new !nputStreamReader( cs.get!nputStream() )),
//cac llnh gli, nhln dl lilu vli server
out.close(), in.close(), cs.close(),
}
}

. Clu trc chllng trinh server

STT Thao tac Sl dlng llnh
1 l socket (dng dl klt nli vli
client)
ServerSocket ss = new
ServerSocket(port),
2 Chl client klt nli va chlp nhln
klt nli
Socket cs= ss.accept(),
3 l lulng dlc (dng dl dlc dl do
server gli)
BufferedReader in = new BufferedReader
(new !nputStreamReader(
cs.get!nputStream() )),
4 l lulng ghi (dng dl gli dl cho
client)
PrintWrite out = new PrintWriter
(cs.getOutputStream(), true),
S Tlo lulng dlc dl tl ban phim
(nlu cln)
BufferedReader std!n = new
BufferedReader (new
!nputStreamReader(Sstem.in) ),
6 Cli dl cho client out.println(String str),
7 Ll dl do client gli String str=in.readLine(),
8 ng cc lulng dlc/ghi, dng cc
socket
in.close(),out.close(), sdt!n.close(),
cs.close(), ss.close(),


* Chllng trinh server mlu (1 client)
import java.net.*,
import java.io.*,
public class Server {
public static void main(String|] args) throws !OException
{
ServerSocket ss = new ServerSocket(1234),
Socket cs = ss.accept(),
6
PrintWriter out = new PrintWriter(cs.getOutputStream(), true),
BufferedReader in = new BufferedReader(new !nputStreamReader(
cs.get!nputStream())),
//cac llnh gli/nhln dl lilu vli mlt client
out.close(), in.close(), cs.close(), ss.close(),
}
}

l phlc vl nhilu client klt nli dlng thli thi server cln tlo ra cac tilu trinh,
mli tilu trinh sl gli/nhln dl lilu vli mlt client.

* Chllng trinh server mlu (nhilu client)
import java.net.*,
import java.io.*,
public class Server
{
public static void main(String|] args) throws !OException
{
ServerSocket ss = new ServerSocket(1234), //c thl tha port khc
boolean listening = true,
while (listening)
{
Socket cs=ss.accept(),
new ServiceThread(cs).start(),
}
ss.close(),
}
}

//llp ServiceThread : tilu trinh phlc vl cho mlt client
import java.net.*,
import java.io.*,
public class ServiceThread extends Thread
{
private Socket socket = null,
public ServiceThread(Socket socket)
{
super("ServiceThread"),
this.socket = socket,
}
public void run()
{
tr
{
PrintWriter out = new PrintWriter(socket.getOutputStream(), true),
BufferedReader in = new BufferedReader( new !nputStreamReader(
socket.get!nputStream())),
//cac llnh gli, nhln dl lilu vli mlt client
7
out.close(), in.close(), socket.close(),
} catch (!OException e) { e.printStackTrace(), }
}
}

* Chllng trinh server mlu (nhilu client) (cach khac)
import java.net.*,
import java.io.*,
public class Server extends Thread
{
private Socket socket = null,
public Server(Socket socket)
{
super("ServiceThread"),
this.socket = socket,
}
public static void main(String|] args) throws !OException
{
ServerSocket ss = new ServerSocket(1234), //c thl tha port khc
boolean listening = true,
while (listening)
{
Socket cs=ss.accept(),
new Server(cs).start(),
}
ss.close(),
}
public void run()
{
tr
{
PrintWriter out = new PrintWriter(socket.getOutputStream(), true),
BufferedReader in = new BufferedReader( new !nputStreamReader(
socket.get!nputStream())),
//cac llnh gli, nhln dl lilu vli mlt client
out.close(), in.close(), socket.close(),
} catch (!OException e) { e.printStackTrace(), }
}
}

Bai tlp :
vilt chllng trinh gptbn dlng client/server dng giao thlc TCP trong hai trllng hlp server
phlc vl cho mlt client va server phlc vl cho nhilu client.
Bai tlp 4 vilt chllng trinh download, upload
Bai tlp S vilt chllng trinh quln l dilm c cc chlc nang nhlp,xem,xa, sla dilm.


!v. Llp trinh UDP sl dlng datagram
8

1. Khai nilm
Datagram la gi chla dl lilu dllc UDP sl dng dl gli dl lilu qua mlng. Thl tl nhln,
nli dung cc gi khng dlm blo gilng nhl khi gli. Ci java.net c cc llp DatagramSocket
va DatagramPacket, ulticastSocket dng dl gli/nhln gi.

2. Clu trc chulng trinh client
STT Thao tac Sl dlng llnh
1 l mlt datagram socket
dng dl lin llc vli m
server
DatagramSocket socket = new DatagramSocket(),
2 Tlo gi gli va gli gi
chla dl lilu
bte|] buf = new bte|2S6], //mlng dng dl
chla dl
String str=., //dl lilu cln gli
buf=str.getBtes(), //clt dl vao mlng
!netAddress address =
!netAddress.getBName(NameServer), /ll !P cla
m server
DatagramPacket packet = new DatagramPacket(buf,
buf.length, address,1234), //tlo gi gli
socket.send(packet), //gli gi
3 Tlo gi nhln va nhln
gi trl lli
packet = new DatagramPacket(buf, buf.length),
socket.receive(packet),
4 Ll dl lilu trong gi

String received = new
String(packet.getData()).trim(),
S ng socket socket.close(),

* Chllng trinh client mlu
import java.io.*,
import java.net.*,
public class Client {
public static void main(String|] args) throws !OException {
DatagramSocket socket = new DatagramSocket(),
bte|] buf = new bte|2S6],
!netAddress address = !netAddress.getBName(NameServer),
DatagramPacket packet = new DatagramPacket(buf, buf.length, address,1234),
socket.send(packet),
packet = new DatagramPacket(buf, buf.length),
socket.receive(packet),
String received = new String(packet.getData()).trim(),
//cac llnh xl l dl lilu
socket.close(),
}
}

. Clu trc chulng trinh server

STT Thao tac Sl dlng llnh
3
1 Tlo mlt datagram socket
dng dl lin llc vli m
client
DatagramSocket socket = new
DatagramSocket(1234),
2 Tlo gi nhln va nhln
gi do client gli
bte|] buf = new bte|2S6],
packet = new DatagramPacket(buf, buf.length),
socket.receive(packet),
3 Ll dl lilu trong gi

String received = new
String(packet.getData()).trim(),
4 Tlo gi gli va gli gi
trl lli cho client
String sendStr ="chuoi dl goi",
buf = sendStr.getBtes(),
!netAddress address = packet.getAddress(),
int port = packet.getPort(),
packet = new DatagramPacket(buf, buf.length,
address, port),
socket.send(packet),
S ng socket socket.close(),
Ct server khng cln tlo tilu trinh vi khng c klt nli nao cln du tri gila client va server.
l phlc vl nhilu client, chllng trinh server chl cln dng mlt vng llp lln lllt nhln
cc gi cla cc client va trl lli. Nlu cng vilc trl lli thlc hiln tln nhilu thli gian thi khi
d nn tlo tilu trinh thlc hiln cng vilc trl lli.

* Chllng trinh server mlu

import java.io.*,
import java.net.*,
public class Server {
public static void main(String|] args) throws !OException {
DatagramSocket socket = new DatagramSocket(1234),
while (true) {
bte|] buf = new bte|2S6],
DatagramPacket packet = new DatagramPacket(buf, buf.length),
socket.receive(packet),
String received = new String(packet.getData()).trim(),
//xl l dl nhln
String sendStr ="chuoi dl goi",
buf = sendStr.getBtes(),
!netAddress address = packet.getAddress(),
int port = packet.getPort(),
packet = new DatagramPacket(buf, buf.length, address, port),
socket.send(packet),
}
socket.close(),
}
Bai tlp 6 :
vilt chllng trinh hli thli tilt, bilt rlng thng tin thli tilt llu trong file thoitiet.txt

Bai tlp 7
vilt chllng trinh hli tl gia, bilt rlng thng tin tl gi llu trong csdl sql server.
10

You might also like