Cac Bai Thuc Hanh

  • Published on
    27-Oct-2014

  • View
    124

  • Download
    2

Embed Size (px)

Transcript

<p>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</p> <p>NI DUNG THC HNH Xy dng chng trnh Chat n gin gia hai my tnh vi yu cu sau y: 1. Giao din</p> <p>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</p> <p>using System; using System.Text; using System.Windows.Forms;1</p> <p>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</p> <p>{ //nu c d liu gi n if (udp.Available &gt; 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:</p> <p>*** 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.</p> <p>3</p> <p>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);</p> <p>4</p> <p>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</p> <p>-</p> <p>NI DUNG Xy dng chng trnh c yu cu nh sau: 1. V giao din a. Trn my qun l</p> <p>b. Trn cc my trm</p> <p>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</p> <p>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)</p> <p>Bc 2: Thm mt ct a ch IP.</p> <p>6</p> <p>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</p> <p>Tn optRestart optRestartAll btnRefresh btnApply btnExit Timer1</p> <p>Nhn Restart Restart all Refresh Hosts Apply Exit applicaceion -</p> <p>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</p> <p>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&gt;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</p> <p>{ //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</p> <p>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</p> <p>{ 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 &gt; 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) khi nhn d liu gi n. V d c th vit li chng trnh cho pha my trm c th nh sau:</p> <p>10</p> <p>*** Nh thm:</p> <p>using System.Threading;</p> <p>Chng trnh pha my trm (S dng c ch Threading) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Text; using System.Windows.Forms; using System.Threading; 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); bool Stop = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //To mt tuyn chy ring hm X_L_D_Liu_Gi_n Thread th = new Thread(new ThreadStart(X_L_D_Liu_Gi_n)); th.Start(); } void X_L_D_Liu_Gi_n() { while (Stop==false) { if (udp.Available &gt; 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 l11</p> <p>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"); } } //Bo cho h iu hnh bit l dnh bt thi gian x l ng dng khc Application.DoEvents(); } //Kt thc tuyn th thot khi ng dng Application.Exit(); } //nu c d liu gi n t my qun tr th x l v gi li private void btnExit_Click(object sender, EventArgs e) { //ngi dng click nt Exit, t bin Stop = true kt thc vng lp trong thread Stop = true; } } }Ghi ch: Mun cho mt chng trnh con (Tuyn) chy song song vi chng trnh chnh th ta ch vic vit mt hm (khng tham s) sau truyn cho Thread. Sau chy hm ny bng cch gi phng thc Start ca Thread va to. Mt thread khi chy n c th vn tn ti ngay c chng trnh chnh thot. V vy, khi thot khi chng trnh chnh th cng cn phi tt thread to ra. y, v trong Thread c mt vng lp v hn, do vy khi kt thc chng trnh chnh, ta ch vic t mt bin nh l Stop = True v a iu kin ny vo trong vng lp ca Thread. C th s dng c ch a tuyn ny cho phn nhn d liu bn pha my qun tr.</p> <p>-</p> <p>-</p> <p>6. Bi tp m rng M rng bi ton trn vi yu cu sau: Pha my trm s gi tn my v Dung lng RAM ca my tnh. Hng dn: My trm s gi ni dung dng $. Pha my qun l s tch ring 2 phn ny ra bng hm Split. ly dung lng RAM ca my, tm trn Google vi t kha: C# get RAM</p> <p>12</p> <p>13</p> <p>BI THC HNH S 3 S DNG TcpClient XY DNG NG DNG CLIENTMC TIU: Kt thc bi thc hnh, sinh vin c th S dng c lp TcpClient xy dng ng dng gi lp Telnet Vn dng xy dng cc ng dng Client thc t.</p> <p>NI DUNG: 1. Yu cu Xy dng chng trnh tng t nh Telnet (Mt chng trnh cho php ngi dng kt ni v gi/nhn d liu vi server. Trn windows c th chy chng trnh telnet bng cch vo ch Command v g lnh: Telnet). Giao din nh sau:</p> <p>Giao din chng trnh c t x l: - Khi chng trnh khi ng, nt Disconnect v Send trng thi cm (Disable). - Click nt Connect th thc hin kt ni n server c a ch v cng tng ng nh trong textbox remotehost v remoteport. Nu kt ni thnh cng th t nt Disconnect, Send trng thi Enabled v nt Connect trng thi Disabled. - Khi click Send th gi ni dung trong Command. - Ni dung gi i hoc nhn v c lu trong textbox Sent/Received. 2. Hng dn t tn cc iu khin nh sau: 14</p> <p>t tn cho cc iu khin trn form 3. Minh ha chng trnh mu using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; namespace TK8LC_Telnet_Simulator { public partial class Form1 : Form { TcpClient client; Thread Th; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //B qua kim tra xung t gia cc lung khi truy cp vo iu khin trn form System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; }</p> <p>15</p> <p>private void btnConnect_Click(object sender, EventArgs e) { client = new TcpClient(); client.Connect(txtRemoteHost.Text, Int32.Parse(txtRemotePort.Text)); Th = new Thread(new ThreadStart(DocDuLieu)); Th.Start(); } //Tuyn (thread) c d liu gi n t Server void DocDuLieu() { while (true) { StreamReader sr = new StreamReader(client.GetStream()); string S = sr.ReadLine(); txtRcv.Text += S + "\r\n"; } } private void btnSend_Click(object sender, EventArgs e) { StreamWriter sw = new StreamWriter(client.GetStream()); sw.WriteLine(txtMsg.Text); sw.Flush(); } private void btnDisconnect_Click(object sender, EventArgs e) { Th.Abort(); //Dng tuyn ang chy client.Close(); //ng (Gii phng) TcpClient } } } 4. Chy chng trnh kim tra c chng trnh, chng ta cn chy mt ng dng Server. V d vo Google v search mt server (FTP) chng hn. Di y l ch download mt FTP Server n gin: http://www.argosoft.com/rootpages/Download.aspx Sau khi ci t, chy chng trnh, n s mc nh nghe cng 21. Ghi nh s hiu cng ny (thuc tnh Remote Port) kt ni v sau. 5. Nhn xt - Khi c d liu t mt stream, th hm ReadLine() ca streamReader s lm vic ch ng b, ngha l n s ch cho n khi c d liu gi n th cc cu lnh ng sau n mi c thc hin. - Khi ng Client th phi m...</p>