Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

BI THC HNH S 2: Gi tin nhn 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 hnhVn dng xy dng cc ng dng thc t
khc trn c s giao thc Udp
NI DUNG THC HNH
Xy dng chng trnh Cht n gin gia hai my tnh vi yu cu sau y:
1. Giao din
a. Trn my qun l

b. Trn cc my trm

2. Yu cu
a. Trn my qun l
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)
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.
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
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:
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

Bc 3: Thm cc iu khin vo form


STT iu khin Tn Nhn
1 Radio button optRestart Restart
2 Radio button optRestartAll Restart all
3 Button btnRefresh Refresh Hosts
4 Button btnApply Apply
5 Button btnExit Exit Application
6 Timer Timer1 -

Bc 4: Code Tham Kho


a. Code cho ng dng 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 GuiTinQuangBaAdmin
{
public partial class Form1 : Form
{
//Udp trn my ngi qun tr s s dng (bind) cng 100
UdpClient udp = new UdpClient(100);
public Form1()
{
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)
{
//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();
}
}
}
b. Code cho ng dng chy trn my chm
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 GuiTinQuangBaHost
{
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)
{
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()

You might also like