




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、網(wǎng)絡(luò)程序設(shè)計實驗指導(dǎo)書實驗教案(主要界面及代碼) 學院:計算機科學與技術(shù)專業(yè):非師范年級:2009一、實驗名稱: 實驗一 進程與線程(4學時)二、儀器、設(shè)備: 教學機房1、安裝有vs2008的計算機三、參考資料:C#網(wǎng)絡(luò)應(yīng)用編程四、實驗?zāi)康模?)掌握進程查看、啟動、停止的基本方法;(2)掌握線程創(chuàng)建、啟動、終止的基本方法;(3)掌握開辟多線程的基本方法;(4)掌握在一個線程中引用其他線程中的控件的方法;五、實驗重點、難點開辟多線程的基本方法;在一個線程中引用其他線程中的控件的方法六、實驗內(nèi)容1. 觀察本機運行的所有進程,并顯示進程相關(guān)的信息。 要求: (1)用DataGridView顯示所有進
2、程信息 (2)鼠標單擊DataGridView某處時,判斷單擊的是否為行開頭或者某個單元格,如果是,顯示該行進程的詳細信息2. 在Class1類中聲明兩個方法Method1和Method2,其中Method1不停地輸出字符“a”,Method2不停地輸出字符“b”,在Form1中啟動線程執(zhí)行Method1和Method2,并在RichTextBox中顯示線程輸出的字符。七、實驗原理1 在VS 2008下新建Windows 窗體應(yīng)用程序,并編寫如下代碼,并調(diào)試運行。namespace ProcessMonitor public partial class Form1 : Form Process
3、 myProcess; public Form1() InitializeComponent(); dataGridView1.AllowUserToAddRows = false; dataGridView1.AutoResizeColumns(); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.MultiSelect = false; private void Form1_Load(object sender, EventArgs e) GetAllPr
4、ocess(); private void GetAllProcess() dataGridView1.Rows.Clear(); myProcess = Process.GetProcesses(); foreach (Process p in myProcess) int newRowIndex = dataGridView1.Rows.Add(); DataGridViewRow row = dataGridView1.RowsnewRowIndex; row.Cells0.Value = p.Id; row.Cells1.Value = p.ProcessName; row.Cells
5、2.Value = string.Format("0:#,#0.00MB", p.WorkingSet64 / 1024.0f / 1024.0f); /有些進程無法獲取啟動時間和文件名信息,所以要用try/catch try row.Cells3.Value = string.Format("0", p.StartTime); row.Cells4.Value = p.MainModule.FileName; catch row.Cells3.Value = "" row.Cells4.Value = "" pr
6、ivate void ShowProcessInfo(Process p) StringBuilder sb = new StringBuilder(); sb.AppendLine("進程名稱:" + p.ProcessName + ", ID:" + p.Id); try sb.AppendLine("進程優(yōu)先級:" + p.BasePriority + "(優(yōu)先級類別: " + p.PriorityClass + ")"); ProcessModule m = p.MainModule;
7、sb.AppendLine("文件名:" + m.FileName); sb.AppendLine("版本:" + m.FileVersionInfo.FileVersion); sb.AppendLine("描述:" + m.FileVersionInfo.FileDescription); sb.AppendLine("語言:" + m.FileVersionInfo.Language); sb.AppendLine("-"); if (p.Modules != null) ProcessM
8、oduleCollection pmc = p.Modules; sb.AppendLine("調(diào)用的模塊(.dll):"); for (int i = 1; i < pmc.Count; i+) sb.AppendLine( "模塊名:" + pmci.ModuleName + "t" + "版本:" + pmci.FileVersionInfo.FileVersion + "t" + "描述:" + pmci.FileVersionInfo.FileDescript
9、ion); catch sb.AppendLine("其他信息:無法獲取"); this.richTextBox1.Text = sb.ToString(); private void buttonRefresh_Click(object sender, EventArgs e) GetAllProcess(); private void dataGridView1_MouseClick(object sender, MouseEventArgs e) DataGridView.HitTestInfo h = dataGridView1.HitTest(e.X, e.Y);
10、 if (h.Type= DataGridViewHitTestType.Cell | h.Type = DataGridViewHitTestType.RowHeader) dataGridView1.Rowsh.RowIndex.Selected = true; int processeId = (int)dataGridView1.CurrentRow.Cells0.Value; ShowProcessInfo(Process.GetProcessById(processeId); 程序運行結(jié)果圖如下:2.在VS 2008下新建Windows 窗體應(yīng)用程序,并編寫如下代碼,并調(diào)試運行。n
11、amespace ThreadExample public partial class Form1 : Form Thread thread1, thread2; Class1 class1; public Form1() InitializeComponent(); class1 = new Class1(this); / buttonStart.Click += new EventHandler(buttonStart_Click); /buttonStop.Click += new EventHandler(buttonStop_Click); private void buttonSt
12、art_Click(object sender, EventArgs e) richTextBox1.Clear(); class1.shouldStop = false; thread1 = new Thread(class1.Method1); thread1.IsBackground = true; thread2 = new Thread(class1.Method2); thread2.IsBackground = true; thread1.Start("a method startn"); thread2.Start(); private void butto
13、nStop_Click(object sender, EventArgs e) class1.shouldStop = true; thread1.Join(0); thread2.Join(0); private delegate void AddMessageDelegate(string message); public void AddMessage(string message) if (richTextBox1.InvokeRequired) AddMessageDelegate d = AddMessage; richTextBox1.Invoke(d, message); el
14、se richTextBox1.AppendText(message); private void richTextBox1_TextChanged(object sender, EventArgs e) 程序運行結(jié)果如下圖:一、實驗名稱: 實驗二 IP地址轉(zhuǎn)換與網(wǎng)卡信息檢測(2學時)二、儀器、設(shè)備: 教學機房1、安裝有vs2008的計算機三、參考資料:C#網(wǎng)絡(luò)應(yīng)用編程四、實驗?zāi)康模?)掌握IPAddress、IPEndPoint、IPHostEntry類的用法 ;(2)進行Dns類完成域名解析的方法。(3)掌握Encoding類的用法五、實驗重點、難點IPAddress、IPEndPoint
15、、IPHostEntry類的用法,Encoding類的用法。六、實驗內(nèi)容(1)演示IPAddress類、Dns類、IPHostEntry類和IPEndPoint類的使用方法,設(shè)計界面如課本圖2-1所示。單擊“顯示本機IP信息”按鈕可以顯示主機名及相關(guān)的IP地址;單擊“顯示服務(wù)器信息”按鈕可顯示中央電視臺服務(wù)器的IP地址信息。(2)利用Encoder類和Decoder類實現(xiàn)編碼和解碼,設(shè)計界面和運行效果如課本圖3-5所示。七、實驗原理1 在VS 2008下新建Windows 窗體應(yīng)用程序,并編寫如下代碼,并調(diào)試運行。namespace IPExample public partial class
16、 MainForm : Form public MainForm() InitializeComponent(); / <summary> / 獲取本機IP信息 / </summary> private void buttonLocalIP_Click(object sender, EventArgs e) listBoxLocalInfo.Items.Clear(); string name = Dns.GetHostName(); listBoxLocalInfo.Items.Add("本機主機名:" + name); IPHostEntry m
17、e = Dns.GetHostEntry(name); listBoxLocalInfo.Items.Add("本機所有IP地址:"); foreach (IPAddress ip in me.AddressList) listBoxLocalInfo.Items.Add(ip); IPAddress localip = IPAddress.Parse(""); IPEndPoint iep = new IPEndPoint(localip, 80); listBoxLocalInfo.Items.Add("IP端點: &qu
18、ot; + iep.ToString(); listBoxLocalInfo.Items.Add("IP端口: " + iep.Port); listBoxLocalInfo.Items.Add("IP地址: " + iep.Address); listBoxLocalInfo.Items.Add("IP地址族: " + iep.AddressFamily); listBoxLocalInfo.Items.Add("可分配端口最大值: " + IPEndPoint.MaxPort); listBoxLocalInf
19、o.Items.Add("可分配端口最小值: " + IPEndPoint.MinPort); / <summary> / 獲取遠程主機信息 / </summary> private void buttonRemoteIP_Click(object sender, EventArgs e) this.listBoxRemoteInfo.Items.Clear(); IPHostEntry remoteHost = Dns.GetHostEntry(this.textBoxRmoteIP.Text); IPAddress remoteIP = remo
20、teHost.AddressList; IPEndPoint iep; foreach (IPAddress ip in remoteIP) iep = new IPEndPoint(ip, 80); listBoxRemoteInfo.Items.Add(iep); 程序運行結(jié)果如下圖:2 在VS 2008下新建Windows 窗體應(yīng)用程序,并編寫如下代碼,并調(diào)試運行。namespace EncoderDecoderExample public partial class MainForm : Form public MainForm() InitializeComponent(); tex
21、tBoxOldText.Text = "測試數(shù)據(jù):abc,123,我" textBoxEncoder.ReadOnly = textBoxDecoder.ReadOnly = true; private void MainForm_Load(object sender, EventArgs e) /顯示現(xiàn)有的編碼類型 foreach (EncodingInfo ei in Encoding.GetEncodings() Encoding en = ei.GetEncoding(); comboBoxType.Items.Add(string.Format("01&
22、quot;, en.HeaderName, en.EncodingName); comboBoxType.SelectedIndex = comboBoxType.FindString("gb2312"); private void buttonRun_Click(object sender, EventArgs e) /編碼 String codeType = boBoxType.SelectedItem.ToString(); codeType = codeType.Substring(0, codeType.IndexOf(''); Encoder e
23、ncoder = Encoding.GetEncoding(codeType).GetEncoder(); char chars = this.textBoxOldText.Text.ToCharArray(); Byte bytes = new Byteencoder.GetByteCount(chars, 0, chars.Length, true); encoder.GetBytes(chars, 0, chars.Length, bytes, 0, true); textBoxEncoder.Text = Convert.ToBase64String(bytes); /解碼 Decod
24、er decoder = Encoding.GetEncoding(codeType).GetDecoder(); int charLen = decoder.GetChars(bytes, 0, bytes.Length, chars, 0); String strResult = "" foreach (char c in chars) strResult = strResult + c.ToString(); textBoxDecoder.Text = strResult; 程序結(jié)果如下:一、實驗名稱: 實驗三 掌握如何使用WinSock編寫簡單的基于TCP協(xié)議的網(wǎng)絡(luò)
25、通信程序。(4學時)二、儀器、設(shè)備: 教學機房1、安裝有vs2008的計算機三、參考資料:C#網(wǎng)絡(luò)應(yīng)用編程四、實驗?zāi)康模?)掌握面向連接套接字編程、無連接套接字編程基本步驟(2)掌握FileStream、NetworkStream類的用法;五、實驗重點、難點掌握面向連接套接字編程、無連接套接字編程基本步驟六、實驗內(nèi)容(1)編寫一個控制臺程序,利用同步Socket實現(xiàn)客戶端和服務(wù)器的消息通信。其中,服務(wù)器可以與多個客戶端通信,并隨時接收客戶端發(fā)送的消息。七、實驗原理1 在VS 2008下新建Windows 控制臺應(yīng)用程序,并編寫客戶端如下代碼,并調(diào)試運行。namespace SocketClie
26、nt class Program private static byte result = new Byte1024; static void Main(string args) /服務(wù)器IP地址 IPAddress ip = IPAddress.Parse(""); Socket clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); try clientSocket.Connect(new IPEndPoint(ip, 8889
27、); Console.WriteLine("連接服務(wù)器成功"); catch Console.WriteLine("連接服務(wù)器失敗,請按回車鍵退出"); return; /通過clientSocket接收數(shù)據(jù) int receiveLength = clientSocket.Receive(result); Console.WriteLine("接收服務(wù)器消息:0",Encoding.ASCII.GetString(result, 0, receiveLength); / 通過clientSocket發(fā)送數(shù)據(jù) for (int i =
28、 0; i < 10; i+) try Thread.Sleep(1000); string sendMessage = "client send Message Hello" + DateTime.Now; clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage); Console.WriteLine("向服務(wù)器發(fā)送消息:0", sendMessage); catch clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close()
29、; break; Console.WriteLine("發(fā)送完畢,按回車鍵退出"); Console.ReadLine(); 程序運行結(jié)果如下:2 在VS 2008下新建Windows 控制臺應(yīng)用程序,并編寫服務(wù)器端如下代碼,并調(diào)試運行。namespace SocketServer class Program private static byte result = new Byte1024; private static int myprot = 8889; static Socket serverSocket; static void Main(string args)
30、 /服務(wù)器IP地址 IPAddress ip = IPAddress.Parse(""); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(ip, myprot); serverSocket.Listen(10); Console.WriteLine("啟動監(jiān)聽0成功", serverSocket.LocalEndPoint.ToStrin
31、g(); /通過clientsocket發(fā)送數(shù)據(jù) Thread myThread = new Thread(ListenClientConnect); myThread.Start(); Console.ReadLine(); / <summary> / 接收連接 / </summary> private static void ListenClientConnect() while (true) Socket clientsocket = serverSocket.Accept(); clientsocket.Send(Encoding.ASCII.GetBytes(
32、"Server Say Hello"); Thread receiveThread = new Thread(ReceiveMessage); receiveThread.Start(clientsocket); / <summary> / 接收信息 / </summary> / <param name="clientSocket">包含客戶機信息的套接字</param> private static void ReceiveMessage(Object clientSocket) Socket myCli
33、entSocket = (Socket)clientSocket; while (true) try /通過clientsocket接收數(shù)據(jù) int receiveNumber = myClientSocket.Receive(result); Console.WriteLine("接收客戶端0消息1",myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber); catch (Exception ex) Console.WriteLine(ex.M
34、essage); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; 程序運行結(jié)果如下:一、實驗名稱: 實驗四 掌握如何使用WinSock編寫簡單的基于UDP協(xié)議的網(wǎng)絡(luò)通信程序。(4學時)二、儀器、設(shè)備: 教學機房1、安裝有vs2008的計算機三、參考資料:C#網(wǎng)絡(luò)應(yīng)用編程四、實驗?zāi)康模?)掌握UdpClient實現(xiàn)單播發(fā)送數(shù)據(jù)和接收數(shù)據(jù)的方法。(2)掌握UdpClient類實現(xiàn)組播及廣播通信的方法。五、實驗重點、難點UdpClient實現(xiàn)單播發(fā)送數(shù)據(jù)和接收數(shù)據(jù)的方法六、實驗內(nèi)容(1)利用Ud
35、pClient,編寫一個網(wǎng)絡(luò)聊天工具,程序運行效果如課本圖6-1所示。(2)編寫一個Windows應(yīng)用程序,向子網(wǎng)發(fā)送廣播信息,同時接收子網(wǎng)中的任意主機發(fā)送的廣播信息,程序設(shè)計界面如課本圖6-3所示。七、實驗原理1.創(chuàng)建Windows窗體應(yīng)用程序,代碼如下,調(diào)試運行namespace UdpChatExample public partial class FormChat : Form / <summary>接收用</summary> private UdpClient receiveUdpClient; / <summary>發(fā)送用</summary
36、> private UdpClient sendUdpClient; / <summary>和本機綁定的端口號</summary> private const int port = 18001; / <summary>本機IP</summary> IPAddress ip; / <summary>遠程主機IP</summary> IPAddress remoteIp; public FormChat() InitializeComponent(); /獲取本機可用IP地址 IPAddress ips = Dns.Ge
37、tHostAddresses(Dns.GetHostName(); ip = ipsips.Length - 1; /為了在同一臺機器調(diào)試,此IP也作為默認遠程IP remoteIp = ip; textBoxRemoteIP.Text = remoteIp.ToString(); textBoxSend.Text = "你好!" private void FormChat_Load(object sender, EventArgs e) /創(chuàng)建一個線程接收遠程主機發(fā)來的信息 Thread myThread = new Thread(ReceiveData); /將線程設(shè)為
38、后臺運行 myThread.IsBackground = true; myThread.Start(); textBoxSend.Focus(); private void ReceiveData() IPEndPoint local = new IPEndPoint(ip, port); receiveUdpClient = new UdpClient(local); IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); while (true) try /關(guān)閉udpClient時此句會產(chǎn)生異常 byte receiveBytes = r
39、eceiveUdpClient.Receive(ref remote); string receiveMessage = Encoding.Unicode.GetString( receiveBytes, 0, receiveBytes.Length); AddItem(listBoxReceive, string.Format("來自0:1", remote, receiveMessage); catch break; private void buttonSend_Click(object sender, EventArgs e) Thread t = new Thre
40、ad(SendMessage); t.IsBackground = true; t.Start(textBoxSend.Text); / <summary>發(fā)送數(shù)據(jù)到遠程主機</summary> private void SendMessage(object obj) string message = (string)obj; sendUdpClient = new UdpClient(0); byte bytes = System.Text.Encoding.Unicode.GetBytes(message); IPEndPoint iep = new IPEndPo
41、int(remoteIp, port); try sendUdpClient.Send(bytes, bytes.Length, iep); AddItem(listBoxStatus, string.Format("向0發(fā)送:1", iep, message); ClearTextBox(); catch (Exception ex) AddItem(listBoxStatus, "發(fā)送出錯:" + ex.Message); delegate void AddListBoxItemDelegate(ListBox listbox, string text); private void AddItem(ListBox listbox, string text) if (listbox.InvokeRequired) AddListBoxItemDelegate d = AddItem; listbox.Invoke(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 云服務(wù)與網(wǎng)絡(luò)架構(gòu)關(guān)系試題及答案
- 公路工程未來發(fā)展趨勢試題及答案
- 計算機四級備考軟件測試試題及答案
- 嵌入式開發(fā)中的質(zhì)量控制試題及答案
- 探索公路工程可持續(xù)發(fā)展考點試題及答案
- 獸藥人員健康管理制度
- 農(nóng)牧審批事項管理制度
- 小區(qū)跑步保安管理制度
- 學校雜物電梯管理制度
- 室內(nèi)裝修現(xiàn)場管理制度
- 山東省煙草專賣局(公司)筆試試題2024
- 2025-2030中國公共安全無線通信系統(tǒng)行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 應(yīng)急救援安全應(yīng)知應(yīng)會題庫
- 2024-2025學年七年級下學期英語人教版(2024)期末達標測試卷A卷(含解析)
- 2025年河南省鄭州市中原區(qū)中考數(shù)學第三次聯(lián)考試卷
- 2024年廣東高校畢業(yè)生“三支一扶”計劃招募筆試真題
- 5年級語文下冊看拼音寫詞語漢字生字擴詞日積月累專項練習電子版
- 《法律文書情境訓(xùn)練》課件-第一審民事判決書的寫作(上)
- 2025至2030年中國護腰帶行業(yè)投資前景及策略咨詢報告
- 廣告宣傳服務(wù)方案投標文件(技術(shù)方案)
- 2025年山東省聊城市東昌府區(qū)中考二模語文試題(含答案)
評論
0/150
提交評論