You are on page 1of 9

CHNG 2 LP TRNH CLIENT\SERVER

I. Tng quan v lp trnh mng: 1. Gii thiu - My tnh trn Internet lin lc vi nhau dng giao thc TCP (Transmission Control Protocol) hoc UDP (User Datagram Protocol). - Khi vit chng trnh Java lin lc trn mng l vit lp ng dng (lp application) v s dng nhng lp trong gi java.net truy xut lp TCP/UDP (lp transport).

Lp trnh client/server: L vit ng dng trn mng gm hai chng trnh: chng trnh client v chng trnh server. Chng trnh client gi yu cu ti chng trnh server, ct server x l yu cu v tr kt qa v cho ct client. Ct server c th phc v ng thi nhiu yu cu ca cc ct client. - Lp trnh WEB: l trng hp c bit ca lp trnh client/server. Ct client l ct Browser (trnh duyt web), ct server l Web Server nhn yu cu trang web t Browser, Web Server tm trang web gi v cho Browser, brower thc thi trang web hin kt qa trn mn hnh client. Browser v web server lin lc qua giao thc HTTP thng qua cng mc nh l 80. Trang Web l file .html vit bng ngn ng HTML (HyperText Markup Language), Browser s thng dch trang web. Khi client mun yu cu trang web, dng browser g va chui c dng sau: http://NameServer:port/path/file.html http l giao thc lin lc gia Browser v Web server, NameServer l tn my web server ang thc thi, port l s hiu cng web server s dng, path/file.html l trang web c yu cu. v d: http://www.microsoft.com/index.html (khng c port th mc nh l 80) Chui ny gi l chui nh v ti nguyn URL (Uniform Resource Locator) dng xc nh ti nguyn trn mng Internet. Ngoi giao thc HTTP cn c th s dng cc giao thc khc nh l FTP, Gopher, File, v News. Hin c cc Browser thng dng nh: Internet Explorer, Netscape Navigator, v cc Web Server nh:IIS (Internet Information Server), PWS (Personal Web Server), JRUN, Tomcat,

2. Giao thc TCP/UDP a) Giao thc TCP: - Thit lp kt ni - m bo d liu gi, c nhn chnh xc v ng th t, ngc li s bo li. Cc giao thc Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), v Telnet l nhng ng dng dng kt ni TCP. My tnh lin lc dng giao thc TCP ging nh con ngi lin lc bng in thoi. b) Giao thc UDP: - Khng kt ni - Khng m bo d liu gi, c nhn chnh xc v ng th t, - Gi/nhn d liu dng gi (datagram), cc gi gi/nhn c lp vi nhau. UDP nhanh hn TCP v khng kim tra d liu, khng cn kt ni. Nhng ng dng nh hi gi, nhn tin, lnh ping nn dng UDP. My tnh lin lc dng giao thc UCP ging nh con ngi lin lc bng th tn. Lu : c nhng firewalls v routers khng cho php gi/nhn gi UDP do admin t cu hnh cm gi UDP. 3. a ch IP, cng (Port), socket: - a ch IP: l s 32 bit m IP dng xc nh my tnh. - Cng: l s 16 bit m TCP/UDP dng xc nh ng dng trn my tnh s nhn d liu. D liu khi gi i, c gi km theo a ch IP ca my nhn v cng m ng dng trn my nhn s dng. S hiu cng trong phm vi t 0 65,535, nhng s hiu cng t 0 n 1023 nn hn ch s dng v chng c dng cho nhng dch v thng dng nh HTTP, FTP. Dch v Cng FTP (truyn /nhn file) 21 HTTP (web) 80 TELNET (truy xut my tnh t 23 xa) SMTP (gi mail) 25 POP3 (ly mail) 110 - Socket: l cu trc d liu lu tr cc thng tin dng kt ni, d liu c gi/nhn thng qua socket. Trong lin lc TCP, ng dng server kt buc mt socket vi mt cng c th, ngha l ng dng server ng k nhn tt c d liu gi cho cng .

Trong lin lc UDP, d liu gi/nhn dng gi. Gi cha s hiu cng, UDP s gi gi cho ng dng tng ng.

II. S dng URL (Uniform Resource Locator) 1. Khi nim - L mt tham chiu n mt ti nguyn trn mng (a ch ti nguyn trn mng), Web Browser hoc cc ng dng khc dng URL tm ti nguyn trn mng. Gi java.net c lp URL dng biu din a ch URL - URL c dng chui gm: giao thc dng truy xut ti nguyn v tn ti nguyn. V d:

C th dng cc giao thc khc nh: File Transfer Protocol (FTP), Gopher, File, News Tn ti nguyn l thng tin y xc nh ti nguyn, thng tin ny ph thuc vo giao thc s dng, nhng thng c cc thng tin sau: Host Name Filename Port Number Tn my cha ti nguyn. ng dn ti ti nguyn S hiu cng dng khi kt ni

i vi http, filename mc nh l index.html 2. To v s dng i tng URL Phng thc ngha URL(String spec) To i tng URL t mt chui URL URL(String protocol, String host, To i tng URL t protocol, host, port v tn int port, String file) file URL(String protocol, String host, To i tng URL t protocol, host, port v tn String file) file, dng port mc nh Object getContent() Ly ni dung ca URL String getFile() Ly tn file cng ng dn ca URL String getHost() Ly tn my ca URL int getPort() Ly s hiu cng ca URL String getProtocol() Ly tn protocol ca URL InputStream openStream() Kt ni ti url v m lung nhp c thng tin t url URLConnection openConnection() M kt ni ti URL Bi tp 1: Vit chng trnh c file t xa thng qua web server 3

Bi tp 2: Vit chng trnh download file thng qua web server III. Lp trnh TCP - S dng socket 1. Khi nim: - Socket l mt u trong kt ni lin lc hai chiu gia hai chng trnh trn mng. Gi java.net cung cp lp Socket ci t kt ni pha client v ServerSocket ci t kt ni pha server. - SocketServer i, lng nghe yu cu kt ni t SocketClient

Khi chp nhn kt ni, SocketServer to mt socket mi kt buc vi mt cng khc phc v cho client kt ni, trong khi SocketServer vn tip tc lng nghe yu cu kt ni t cc client khc.

Sau server v client s lin lc vi nhau thng qua socket ca chng. Lu : + Client dng cng cc b trn my ca client. + Nu kt ni ti Web Server th lp URL thch hp hn lp Socket, thc ra lp URL cng s s dng lp socket 2. Cu trc chng trnh client ST T 1 2 Thao tc M socket (dng kt ni vi server) M lung c (dng c dl do server gi) S dng lnh

3 4 5 6 7

Socket cs = new Socket("ServerName", port); BufferedReader in = new BufferedReader (new InputStreamReader( cs.getInputStrea m() )); M lung ghi (dng gi dl cho PrintWriter out = new PrintWriter server) (cs.getOutputStream(), true); M lung c dl t bn phm (nu BufferedReader stdIn = new cn) BufferedReader (new InputStreamReader(System.in) ); Gi dl cho server out.println(String str); Ly dl do server gi String str=in.readLine(); ng cc lung c, ghi, ng in.close();out.close(); sdtIn.close(); 4

socket cs.close(); Ghi ch: - Reader, writer: c th c ghi k t Unicode qua socket - ng lung c/ghi, sau mi ng socket * Chng trnh client mu import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { Socket cs = new Socket("ServerName", 1234); PrintWriter out = new PrintWriter(cs.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( cs.getInputStream() )); //cc lnh gi, nhn d liu vi server out.close(); in.close(); cs.close(); } } 3. Cu trc chng trnh server ST T 1 2 3 Thao tc M socket (dng kt ni vi client) Ch client kt ni v chp nhn kt ni M lung c (dng c dl do server gi) S dng lnh ServerSocket ss = new ServerSocket(port); Socket cs= ss.accept();

4 5 6 7 8

BufferedReader in = new BufferedReader (new InputStreamReader( cs.getInputStrea m() )); M lung ghi (dng gi dl cho PrintWrite out = new PrintWriter client) (cs.getOutputStream(), true); To lung c dl t bn phm BufferedReader stdIn = new (nu cn) BufferedReader (new InputStreamReader(System.in) ); Gi dl cho client out.println(String str); Ly dl do client gi String str=in.readLine(); ng cc lung c/ghi, ng cc in.close();out.close(); sdtIn.close(); socket cs.close(); ss.close();

* Chng trnh server mu (1 client) import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { 5

ServerSocket ss = new ServerSocket(1234); Socket cs = ss.accept(); PrintWriter out = new PrintWriter(cs.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( cs.getInputStream())); //cc lnh gi/nhn d liu vi mt client out.close(); in.close(); cs.close(); ss.close(); } } phc v nhiu client kt ni ng thi th server cn to ra cc tiu trnh, mi tiu trnh s gi/nhn d liu vi mt client. * Chng trnh server mu (nhiu client) import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(1234); //c th thay port khc boolean listening = true; while (listening) { Socket cs=ss.accept(); new ServiceThread(cs).start(); } ss.close(); } } //lp ServiceThread : tiu trnh phc v cho mt 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() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); //cc lnh gi, nhn d liu vi mt client 6

out.close(); in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } * Chng trnh server mu (nhiu client) (cch khc) 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 IOException { ServerSocket ss = new ServerSocket(1234); //c th thay port khc boolean listening = true; while (listening) { Socket cs=ss.accept(); new Server(cs).start(); } ss.close(); } public void run() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); //cc lnh gi, nhn d liu vi mt client out.close(); in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } Bi tp 3: Vit chng trnh gptbn dng client/server dng giao thc TCP trong hai trng hp: server phc v cho mt client v server phc v cho nhiu client. Bi tp 4 : vit chng trnh download, upload Bi tp 5 : Vit chng trnh qun l im c cc chc nng nhp,xem,xa, sa im. IV. Lp trnh UDP- s dng datagram 1. Khi nim 7

Datagram l gi cha d liu c UDP s dng gi d liu qua mng. Th t nhn, ni dung cc gi khng m bo ging nh khi gi. Gi java.net c cc lp DatagramSocket v DatagramPacket, MulticastSocket dng gi/nhn gi. 2. Cu trc chung trnh client ST Thao tc S dng lnh T 1 M mt datagram DatagramSocket socket = new socket dng lin lc DatagramSocket(); vi my server 2 To gi gi v gi gi byte[] buf = new byte[256]; //mng dng cha d liu cha dl String str=; //d liu cn gi buf=str.getBytes(); //ct dl vo mng InetAddress address = InetAddress.getByName(NameServer); /ly IP ca my server DatagramPacket packet = new DatagramPacket(buf, buf.length, address,1234); //to gi gi socket.send(packet); //gi gi 3 To gi nhn v nhn packet = new DatagramPacket(buf, gi tr li buf.length); socket.receive(packet); 4 Ly d liu trong gi String received = new String(packet.getData()).trim(); 5 ng socket socket.close(); * Chng trnh client mu import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(); byte[] buf = new byte[256]; InetAddress address = InetAddress.getByName(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(); //cc lnh x l d liu socket.close(); } } 3. Cu trc chung trnh server ST T Thao tc 8 S dng lnh

1 2

byte[] buf = new byte[256]; packet = new DatagramPacket(buf, buf.length); socket.receive(packet); 3 Ly d liu trong gi String received = new String(packet.getData()).trim(); 4 To gi gi v gi gi String sendStr =chuoi dl goi; tr li cho client buf = sendStr.getBytes(); InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); 5 ng socket socket.close(); Ct server khng cn to tiu trnh v khng c kt ni no cn duy tr gia client v server. phc v nhiu client, chng trnh server ch cn dng mt vng lp ln lt nhn cc gi ca cc client v tr li. Nu cng vic tr li thc hin tn nhiu thi gian th khi nn to tiu trnh thc hin cng vic tr li. * Chng trnh server mu import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(1234); while (true) { byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData()).trim(); //x l dl nhn String sendStr =chuoi dl goi; buf = sendStr.getBytes(); InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); } socket.close(); } Bi tp 6 : Vit chng trnh hi thi tit, bit rng thng tin thi tit lu trong file thoitiet.txt Bi tp 7: Vit chng trnh hi t ga, bit rng thng tin t gi lu trong csdl sql server. 9

To mt datagram socket dng lin lc vi my client To gi nhn v nhn gi do client gi

DatagramSocket socket = new DatagramSocket(1234);

You might also like