You are on page 1of 13

TRNG I HC K THUT CNG NGH

KHOA CNG NGH THNG TIN Mn: Lp trnh mng

Bi thc hnh 6

Bi 1: Vit chng trnh giao tip gia client v server s dng giao thc UDP, thc hin cc chc nng sau: + client truyn 1 chui ln server, server nhn chui ny v chuyn n thnh ch in hoa sau gi tr kt qu cho client. Client nhn kt qu ri sau xut ra mn hnh kt qu va nhn. Yu cu: S dng Multithread server c th giao tip c vi nhiu client cng lc.
a. Bc 1: To 2 project mi l UDPServer v UDPClient

b. Bc 2: To Form frmClient c giao din nh sau:

X l s kin cho button Truyn chui.

X l s kin cho button Thot

c. Bc 3: To lp UDPServer nh sau:

d. Bc 4: Chy th ng dng Chy file UDPServer.java Chy file frmClient.java

Bi 2: Vit chng trnh giao tip gia client v server s dng giao thc UDP, thc hin cc chc nng sau: + client truyn hai s nguyn v php ton (cng, tr, nhn, chia) ln server, server sau khi nhn c th thc hin php ton gia hai s nguyn v tr kt qu v cho client, client nhn li kt qu v xut ra mn hnh. Yu cu: S dng Multithread server c th giao tip c vi nhiu client cng lc.
a. Bc 1: To 2 project mi l UDPServer v UDPClient

b. Bc 2: To Form frmClient c giao din nh sau:

X l s kin cho button Tnh.

X l s kin cho button Thot

c. Bc 3: To lp UDPServer nh sau:

d. Bc 4: Chy th ng dng Chy file UDPServer.java Chy file frmClient.java

Bi 3: Vit chng trnh cho php hai my chat vi nhau.


a. Bc 1: Thit k giao din: lp Chat.java

b. Bc 2: Vit code cho button Send ca lp Chat.java

c. Bc 4: Vit code cho lp Main.java to ra i tng giao din chat v code x l lng nghe gi d liu chat.

Bi 4. Vit chng trnh cho php hai my chat vi nhau. Dng Multicast
B1: To JFrame frmChat B2: To giao din nh hnh sau:

Khai bo cc th vin c s dng


import java.io.*; import java.net.*; import javax.swing.*;

B3: Khai bo cc thuc tnh ca Form

B4: Cc hm trong chng trnh

B5: S kin cho nt kt ni

private void btnketnoiActionPerformed(java.awt.event.ActionEvent evt) { if(btnketnoi.getText().equals("Kt ni")){ //Khi nhn nt kt ni btnketnoi.setText("Ngt kt ni"); //chuyn nt kt ni thnh nt ngt kt ni txtchat.setEnabled(true); txtnoidung.setEnabled(true); btnsend.setEnabled(true); txtgroup.setEnabled(false); txtport.setEnabled(false);

txtnick.setEnabled(false); try{ group= InetAddress.getByName(txtgroup.getText()); if(group.isMulticastAddress()){ //Kim tra xem a ch nhm c phi a ch multicast hay khng nick=txtnick.getText(); port=Integer.parseInt(txtport.getText()); if(mythread==null){ //To ra v thit lp ban u cho cc i tng mng socket=new MulticastSocket(port); socket.setTimeToLive(1); //Thit lp ng i cho gi tin socket.joinGroup(group); //ng k vi router l chng trnh my mnh ng k vo nhm group truyen=new DatagramPacket(new byte[1],1,group,port); nhan=new DatagramPacket(new byte[65507],65507); //To ra thread x l d liu truyn n mythread=new Thread(this); mythread.start(); //Bt u nhn d liu - Lc ny hm run s dc gi thc thi } }else JOptionPane.showMessageDialog(null, "a ch nhp sai ri!!"); }catch(Exception e){ JOptionPane.showMessageDialog(null,e); } }else{ //S kin nhn nt ngt kt ni txtchat.setEnabled(false); txtnoidung.setEnabled(false); btnsend.setEnabled(false); txtgroup.setEnabled(true); txtport.setEnabled(true); txtnick.setEnabled(true); btnketnoi.setText("Kt ni"); //chuyn nt ngt kt ni thnh nt kt ni if(mythread!=null){ mythread.interrupt(); //dng vic nhn d liu mythread=null; try{ socket.leaveGroup(group);//Ra khi group

}catch(IOException e){} socket.close(); } } }

B6:Hm run: l hm s c gi khi thread bt u

B7: S kin cho nt ng

B8: S kin cho nt Send

B9: S kin cho vic ang ng form

B10: Kim tra bng cch chy th chng trnh trn nhiu my mng LAN ch cn thay nick khc nhau cho tng my. Nhng my no thit lp cng group(cng a ch Multicast) th c th chat c vi nhau.

Bi 5. Vit chng trnh minh hot giao thc FTP cho php hai my gi tp tin cho nhau s dng UDP Socket.

Bi 6. Vit chng trnh cho php gia nhp vo mt a ch multicast. Thc hin chc nng gi mt thng ip vo a ch ny v nhn cc thng ip c gi ti a ch ny.

You might also like