Cac Bai Thuc Hanh

  • View
    126

  • Download
    2

Embed Size (px)

Text of Cac Bai Thuc Hanh

LP TRNH MNG MY TNH BI THC HNH S 1: S dng lp UdpClient MC TIU: Kt thc bi thc hnh ny, sinh vin c th S dng c lp UdpClient xy dng ng dng Chat n gin gia hai my tnh Vn dng xy dng cc ng dng thc t khc trn c s giao thc Udp

NI DUNG THC HNH Xy dng chng trnh Chat n gin gia hai my tnh vi yu cu sau y: 1. Giao din

Giao din chng trnh (km tn cc iu khin) 2. c t x l - Khi click nt Send (btnSend) th s gi xu trong textbox txtMsg ti ng dng nhn c a ch v cng tng ng trong txtRemoteHost v txtRemotePort. Xu gi i cng ng thi c chn vo txtSent. - Khi c d liu gi n th ng dng s nhn v chn vo txtReceived. 3. Hng dn - Khai bo bin ton cc UdpClient s dng trong c gi v nhn d liu - S dng iu khin Timer hoc vng lp thng xuyn kim tra d liu gi n. Dng timer s d dng hn. - Cn kim tra c d liu trc khi gi hm Receive trnh b tnh trng Blocking 4. Chng trnh mu

using System; using System.Text; using System.Windows.Forms;1

using System.Net; using System.Net.Sockets; //Chat Simple: By Software Engineering Department - FIT-UTEHY 2012. namespace Chat_Simple { public partial class Form1 : Form { UdpClient udp; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Tm thi cha cho hot ng khi cha t thng s btnSend.Enabled = false; timer1.Enabled = false; } //Thit lp thng s cng theo gi tr do ngi dng nhp private void btnApply_Click(object sender, EventArgs e) { //Do phi thit lp theo a ch v cng mi nht nhp vo //nn cn phi xa (close) udp c xin cp pht trc if (udp != null) udp.Close(); //To udp theo cc thng s cng c nhp trong localport udp = new UdpClient(Int32.Parse(txtLocalPort.Text)); //Bt u cho timer hot ng v cho php gi d liu khi c //thng s nhp vo trong remotehost, remoteport, localport timer1.Enabled = true; timer1.Interval = 100; //100 miligiay = 1/10 giy btnSend.Enabled = true; } //Gi d liu sang ng dng khc private void btnSend_Click(object sender, EventArgs e) { byte[] Data = Encoding.UTF8.GetBytes(txtMsg.Text); udp.Send(Data, Data.Length, txtRemoteHost.Text, Int32.Parse(txtRemotePort.Text)); //chn ni dung va gi vo trong textbox sent txtSent.Text = txtMsg.Text+"\r\n" + txtSent.Text ; } //Nhn d liu t ng dng khc private void timer1_Tick(object sender, EventArgs e)2

{ //nu c d liu gi n if (udp.Available > 0) { byte[] Data; IPEndPoint iep = new IPEndPoint( IPAddress.Parse(txtRemoteHost.Text), Int32.Parse(txtRemotePort.Text)); Data = udp.Receive(ref iep); txtReceived.Text +="\r\n"+ Encoding.UTF8.GetString(Data); } } } }5. Chy chng trnh Bin dch chng trnh, sau chy file exe 2 ln. Nhp gi tr nh hnh sau:

*** Sau khi nhp (lu : gi tr cng cho nhau gia hai ng dng), click nt Apply. 6. Nhn xt - Khng dng vng lp kim tra d liu gi n v Timer tng t nh vng lp. - C th to mt tuyn (thread) trong c vng lp thc hin vic nhn d liu thay v timer. - xung dng trong textbox, cn thm chui k t \r\n - Trong phn khai bo, ta vit: UdpClient udp; thay v vit nh thng l l: UdpClient udp = new UdpClient(gi tr cng); L do l v s hiu cng s c ly t textbox txtLocalPort do ngi dng nhp vo khi chy chng trnh.

3

7. Bi tp lm thm M rng chng trnh trn ngoi chc nng chat c th t mt my ra lnh cho my kia tt, khi ng li my tnh. Gi : Bn gi ch cn nhp ni dung gi i theo dng: #01 , hoc #02 hoc mt xu thng thng. Cn i vi pha nhn th s kim tra 3 k t u tin ca xu nhn v (S dng hm SubString(0, 3) trch 3 k t u tin). Nu l #01 th khi ng li my; nu l #02 th tt my, tri li th hiu n gin l dng chat thng thng (khi s a vo txtReceived nh bi thc hnh). - Dng lnh thc hin khi ng li my: System.Diagnostics.Process.Start(Shutdown,-r -t 3); - Dng lnh thc hin tt my: System.Diagnostics.Process.Start(Shutdown,-s -t 3);

4

LP TRNH MNG MY TNH BI THC HNH S 2: Gi tin qung b Mc tiu: Kt thc bi thc hnh ny, sinh vin c th: Thc hin gi tin qung b ti nhiu my tnh ng thi trong mng Vn dng xy dng cc ng dng nh H tr qun l phng mng, gim st cc my tnh trong phng thc hnh

-

NI DUNG Xy dng chng trnh c yu cu nh sau: 1. V giao din a. Trn my qun l

b. Trn cc my trm

2. Yu cu a. Trn my qun l o Khi ngi dng click nt Refresh hosts th danh sch a ch (tn my) ca cc my khc trong mng s lit k trong danh sch (GridView) o Khi ngi dng chn mt a ch trong gridview v chn Restart sau click Apply th tin hnh restart my ng vi a ch tng ng. o Khi ngi dng chn Restart All v click Apply th restart li tt c cc my c a ch trong gridview. b. Trn my trm 5

My trm khi khi ng s chy ngm v lng nghe cc lnh gi n t my qun tr. Nu c yu cu restart th restart li my, nu c yu cu gi a ch my m n ang chy trn th tin hnh gi. 3. Hng dn - cc my trm gi tn my ca n ti ng dng qun l, ta tin hnh gi mt gi tin qung b ti my trm vi quy c: please_send_me_your_ip yu cu gi a ch hoc tn my. Sau s dng mt timer lin tc kim tra d liu tr v t cc my trm. Vic ly tn my trn cc my trm c th dng hm Dns.GetHostName(). Hm ny tr v tn my m n ang chy. - restart mt my, ta ch vic ly tn ca my m ngi dng chn trn gridview, sau gi gi tin l please_restart. My trm khi nhn c xu ny s thc hin restart my bng hm System.Diagnostics.Process.Start(Shutdown,-r -t 3). Lu , trong hm gi d liu i, ta c th vit tn my thay v a ch IP: Send(Data, length, HostName, port). - Restart tt c cc my, ta ch n gin gi cho mi my xu please_restart bng c ch gi qung b. - truy cp n mt ti hng i, ct j trong gridview c tn l dgrHost, theo c php: o dgrHost.Rows[i].Cells[j].Value 4. Thc hin Bc 1: To ng dng winform v a iu khin gridview vo form lu danh sch a ch IP cc my. Sau click chn Edit columns (bm vo biu tng mi tn gc trn bn phi ca iu khin)

Bc 2: Thm mt ct a ch IP.

6

Bc 3: Thm cc iu khin vo form STT iu khin 1. Raido button 2. Raido button 3. Button 4. Button 5. Button 6. Timer Bc 4: Vit code

Tn optRestart optRestartAll btnRefresh btnApply btnExit Timer1

Nhn Restart Restart all Refresh Hosts Apply Exit applicaceion -

Code cho ng dng pha my qun tr using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace TH_so2___Gui_tin_quang_ba__Admin { public partial class Form1 : Form { //Udp trn my ngi qun tr s s dng (bind) cng 100 UdpClient udp = new UdpClient(100); public Form1() {7

InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { timer1.Enabled = true; timer1.Interval = 100; dgrHosts.AllowUserToAddRows = false; //Khng cho php t ng thm dng mi dgrHosts.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } //khi refresh, ch vic xa danh sch trong gridview //v gi lnh yu cu (broadcast) cc my gi li a ch IP private void btnRefresh_Click(object sender, EventArgs e) { //xa ni dung trong gridview dgrHosts.Rows.Clear(); //Gi tn hiu yu cu cc my trm (cng 200) gi li IP byte[] Data = Encoding.UTF8.GetBytes("please_send_me_your_ip"); udp.EnableBroadcast = true; //Gi thng bo ti tt c cc ng dng ang s dng giao thc UDP, cng 200 udp.Send(Data, Data.Length, "255.255.255.255", 200); } //Timer ny s nhn cc thng tin gi t cc my trm (gm IP) private void timer1_Tick(object sender, EventArgs e) { if (udp.Available>0) { //Khai bo mt IPEndpoint cho php nhn t nhiu my gi ti IPEndPoint iep = new IPEndPoint(IPAddress.Any,200); udp.EnableBroadcast=true; byte[] Data = udp.Receive(ref iep); String HostAddress =Encoding.UTF8.GetString(Data); //thm mt dng mi vo gridview lu a ch IP va gi ti. dgrHosts.Rows.Add(); int Dng_Cui = dgrHosts.Rows.Count - 1; dgrHosts.Rows[Dng_Cui].Cells[0].Value = HostAddress; } } //x l theo cc ty chn ca ngi dng (Restart, restartall) private void btnApply_Click(object sender, EventArgs e) { //ch gi ti mt IP c chn if (optRestart.Checked == true)8

{ //nu khng chn my no th thot. if (dgrHosts.SelectedRows.Count == 0) return; //Ch gi tn hiu restart n a ch IP c chn String HostAddress = dgrHosts.SelectedRows[0].Cells[0].Value.ToString(); //ly IP byte[] Data = Encoding.UTF8.GetBytes("please_restart"); udp.Send(Data, Data.Length, HostAddress, 200); } //Gi ti tt c cc my ang s dng cng 200, UDP if (optRestartAll.Checked == true) { byte[] Data = Encoding.UTF8.GetBytes("please_restart"); udp.EnableBroadcast = true; udp.Send(Data, Data.Length, "255.255.255.255", 200); //a ch qung b } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } }Code cho ng dng chy trn my trm

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace TH_so_2___Gui_tin_quang_ba___Host { public partial class Form1 : Form { //To mt th hin UdpClient, s dng cng 200 UdpClient udp = new UdpClient(200); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)9

{ timer1.Enabled = true; timer1.Interval = 100; } //nu c d liu gi n t my qun tr th x l v gi li private void timer1_Tick(object sender, EventArgs e) { if (udp.Available > 0) { //v bn gi gi d liu kiu broadcast (qung b) t cng 100 //nn a ch nhn t l IPAddress.Any, cng l 100 IPEndPoint iep = new IPEndPoint(IPAddress.Any, 100); udp.EnableBroadcast = true; byte [] Data = udp.Receive(ref iep); //nhn d liu String S = Encoding.UTF8.GetString(Data); //chuyn thnh xu x l if (S.CompareTo("please_send_me_your_ip") == 0) { String Hostname = Dns.GetHostName(); Data = Encoding.UTF8.GetBytes(Hostname); udp.Send(Data, Data.Length, iep); } if (S.CompareTo("please_restart") == 0) { System.Diagnostics.Process.Start("shutdown", "-r -t 3"); } } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } }5. Nhn xt - Khi nhn d liu gi ti t mt my, ta thng vit udp.Receive(ref iep), th iep ny s cha thng tin (a ch, cng) ca my gi. V vy, khi cn reply li th ta s dng chnh gi tr ny khi gi, lc s dng hm Send c dng: udp.Send(Data, Data.Length, iep) thay v s dng hm Send dng: Send(Data, Data.Length, Host, Port). - C th ly tn ca my tnh bng cch gi hm: Dns.GetHostname() - C th thay timer bng c ch a lung (a tuyn Multi threading