网络程序设计实验指导书.doc_第1页
网络程序设计实验指导书.doc_第2页
网络程序设计实验指导书.doc_第3页
网络程序设计实验指导书.doc_第4页
网络程序设计实验指导书.doc_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

网络程序设计实验指导书实验教案(主要界面及代码) 学院:计算机科学与技术专业:非师范年级:2009一、实验名称: 实验一 进程与线程(4学时)二、仪器、设备: 教学机房1、安装有vs2008的计算机三、参考资料:C#网络应用编程四、实验目的(1)掌握进程查看、启动、停止的基本方法;(2)掌握线程创建、启动、终止的基本方法;(3)掌握开辟多线程的基本方法;(4)掌握在一个线程中引用其他线程中的控件的方法;五、实验重点、难点开辟多线程的基本方法;在一个线程中引用其他线程中的控件的方法六、实验内容1. 观察本机运行的所有进程,并显示进程相关的信息。 要求: (1)用DataGridView显示所有进程信息 (2)鼠标单击DataGridView某处时,判断单击的是否为行开头或者某个单元格,如果是,显示该行进程的详细信息2. 在Class1类中声明两个方法Method1和Method2,其中Method1不停地输出字符“a”,Method2不停地输出字符“b”,在Form1中启动线程执行Method1和Method2,并在RichTextBox中显示线程输出的字符。七、实验原理1 在VS 2008下新建Windows 窗体应用程序,并编写如下代码,并调试运行。namespace ProcessMonitor public partial class Form1 : Form Process myProcess; public Form1() InitializeComponent(); dataGridView1.AllowUserToAddRows = false; dataGridView1.AutoResizeColumns(); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.MultiSelect = false; private void Form1_Load(object sender, EventArgs e) GetAllProcess(); 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.Cells2.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 = ; private void ShowProcessInfo(Process p) StringBuilder sb = new StringBuilder(); sb.AppendLine(进程名称: + p.ProcessName + , ID: + p.Id); try sb.AppendLine(进程优先级: + p.BasePriority + (优先级类别: + p.PriorityClass + )); ProcessModule m = p.MainModule; 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) ProcessModuleCollection pmc = p.Modules; sb.AppendLine(调用的模块(.dll):); for (int i = 1; i pmc.Count; i+) sb.AppendLine( 模块名: + pmci.ModuleName + t + 版本: + pmci.FileVersionInfo.FileVersion + t + 描述: + pmci.FileVersionInfo.FileDescription); 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); 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); 程序运行结果图如下:2.在VS 2008下新建Windows 窗体应用程序,并编写如下代码,并调试运行。namespace 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 buttonStart_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 buttonStop_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); else richTextBox1.AppendText(message); private void richTextBox1_TextChanged(object sender, EventArgs e) 程序运行结果如下图:一、实验名称: 实验二 IP地址转换与网卡信息检测(2学时)二、仪器、设备: 教学机房1、安装有vs2008的计算机三、参考资料:C#网络应用编程四、实验目的(1)掌握IPAddress、IPEndPoint、IPHostEntry类的用法 ;(2)进行Dns类完成域名解析的方法。(3)掌握Encoding类的用法五、实验重点、难点IPAddress、IPEndPoint、IPHostEntry类的用法,Encoding类的用法。六、实验内容(1)演示IPAddress类、Dns类、IPHostEntry类和IPEndPoint类的使用方法,设计界面如课本图2-1所示。单击“显示本机IP信息”按钮可以显示主机名及相关的IP地址;单击“显示服务器信息”按钮可显示中央电视台服务器的IP地址信息。(2)利用Encoder类和Decoder类实现编码和解码,设计界面和运行效果如课本图3-5所示。七、实验原理1 在VS 2008下新建Windows 窗体应用程序,并编写如下代码,并调试运行。namespace IPExample public partial class MainForm : Form public MainForm() InitializeComponent(); / / 获取本机IP信息 / private void buttonLocalIP_Click(object sender, EventArgs e) listBoxLocalInfo.Items.Clear(); string name = Dns.GetHostName(); listBoxLocalInfo.Items.Add(本机主机名: + name); IPHostEntry me = 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端点: + 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); listBoxLocalInfo.Items.Add(可分配端口最小值: + IPEndPoint.MinPort); / / 获取远程主机信息 / private void buttonRemoteIP_Click(object sender, EventArgs e) this.listBoxRemoteInfo.Items.Clear(); IPHostEntry remoteHost = Dns.GetHostEntry(this.textBoxRmoteIP.Text); IPAddress remoteIP = remoteHost.AddressList; IPEndPoint iep; foreach (IPAddress ip in remoteIP) iep = new IPEndPoint(ip, 80); listBoxRemoteInfo.Items.Add(iep); 程序运行结果如下图:2 在VS 2008下新建Windows 窗体应用程序,并编写如下代码,并调试运行。namespace EncoderDecoderExample public partial class MainForm : Form public MainForm() InitializeComponent(); textBoxOldText.Text = 测试数据:abc,123,我; textBoxEncoder.ReadOnly = textBoxDecoder.ReadOnly = true; private void MainForm_Load(object sender, EventArgs e) /显示现有的编码类型 foreach (EncodingInfo ei in Encoding.GetEncodings() Encoding en = ei.GetEncoding(); comboBoxType.Items.Add(string.Format(01, 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 encoder = 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); /解码 Decoder 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; 程序结果如下:一、实验名称: 实验三 掌握如何使用WinSock编写简单的基于TCP协议的网络通信程序。(4学时)二、仪器、设备: 教学机房1、安装有vs2008的计算机三、参考资料:C#网络应用编程四、实验目的(1)掌握面向连接套接字编程、无连接套接字编程基本步骤(2)掌握FileStream、NetworkStream类的用法;五、实验重点、难点掌握面向连接套接字编程、无连接套接字编程基本步骤六、实验内容(1)编写一个控制台程序,利用同步Socket实现客户端和服务器的消息通信。其中,服务器可以与多个客户端通信,并随时接收客户端发送的消息。七、实验原理1 在VS 2008下新建Windows 控制台应用程序,并编写客户端如下代码,并调试运行。namespace SocketClient class Program private static byte result = new Byte1024; static void Main(string args) /服务器IP地址 IPAddress ip = IPAddress.Parse(); Socket clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); try clientSocket.Connect(new IPEndPoint(ip, 8889); Console.WriteLine(连接服务器成功); catch Console.WriteLine(连接服务器失败,请按回车键退出); return; /通过clientSocket接收数据 int receiveLength = clientSocket.Receive(result); Console.WriteLine(接收服务器消息:0,Encoding.ASCII.GetString(result, 0, receiveLength); / 通过clientSocket发送数据 for (int i = 0; i 10; i+) try Thread.Sleep(1000); string sendMessage = client send Message Hello + DateTime.Now; clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage); Console.WriteLine(向服务器发送消息:0, sendMessage); catch clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; Console.WriteLine(发送完毕,按回车键退出); Console.ReadLine(); 程序运行结果如下:2 在VS 2008下新建Windows 控制台应用程序,并编写服务器端如下代码,并调试运行。namespace SocketServer class Program private static byte result = new Byte1024; private static int myprot = 8889; static Socket serverSocket; static void Main(string args) /服务器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(启动监听0成功, serverSocket.LocalEndPoint.ToString(); /通过clientsocket发送数据 Thread myThread = new Thread(ListenClientConnect); myThread.Start(); Console.ReadLine(); / / 接收连接 / private static void ListenClientConnect() while (true) Socket clientsocket = serverSocket.Accept(); clientsocket.Send(Encoding.ASCII.GetBytes(Server Say Hello); Thread receiveThread = new Thread(ReceiveMessage); receiveThread.Start(clientsocket); / / 接收信息 / / 包含客户机信息的套接字 private static void ReceiveMessage(Object clientSocket) Socket myClientSocket = (Socket)clientSocket; while (true) try /通过clientsocket接收数据 int receiveNumber = myClientSocket.Receive(result); Console.WriteLine(接收客户端0消息1,myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber); catch (Exception ex) Console.WriteLine(ex.Message); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; 程序运行结果如下:一、实验名称: 实验四 掌握如何使用WinSock编写简单的基于UDP协议的网络通信程序。(4学时)二、仪器、设备: 教学机房1、安装有vs2008的计算机三、参考资料:C#网络应用编程四、实验目的(1)掌握UdpClient实现单播发送数据和接收数据的方法。(2)掌握UdpClient类实现组播及广播通信的方法。五、实验重点、难点UdpClient实现单播发送数据和接收数据的方法六、实验内容(1)利用UdpClient,编写一个网络聊天工具,程序运行效果如课本图6-1所示。(2)编写一个Windows应用程序,向子网发送广播信息,同时接收子网中的任意主机发送的广播信息,程序设计界面如课本图6-3所示。七、实验原理1.创建Windows窗体应用程序,代码如下,调试运行namespace UdpChatExample public partial class FormChat : Form / 接收用 private UdpClient receiveUdpClient; / 发送用 private UdpClient sendUdpClient; / 和本机绑定的端口号 private const int port = 18001; / 本机IP IPAddress ip; / 远程主机IP IPAddress remoteIp; public FormChat() InitializeComponent(); /获取本机可用IP地址 IPAddress ips = Dns.GetHostAddresses(Dns.GetHostName(); ip = ipsips.Length - 1; /为了在同一台机器调试,此IP也作为默认远程IP remoteIp = ip; textBoxRemoteIP.Text = remoteIp.ToString(); textBoxSend.Text = 你好!; private void FormChat_Load(object sender, EventArgs e) /创建一个线程接收远程主机发来的信息 Thread myThread = new Thread(ReceiveData); /将线程设为后台运行 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 /关闭udpClient时此句会产生异常 byte receiveBytes = receiveUdpClient.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 Thread(SendMessage); t.IsBackground = true; t.Start(textBoxSend.Text); / 发送数据到远程主机 private void SendMessage(object obj) string message = (string)obj; sendUdpClient = new UdpClient(0); byte bytes = System.Text.Encoding.Unicode.GetBytes(message); IPEndPoint iep = new IPEndPoint(remoteIp, port); try sendUdpClient.Send(bytes, bytes.Length, iep); AddItem(listBoxStatus, string.Format(向0发送:1, iep, message); ClearTextBox(); catch (Exception ex) AddItem(listBoxStatus, 发送出错: + ex.Message); delegate void AddListBoxItemDelegate(ListBox listbox, string text); private void AddItem(ListBox listbox, string text) if (listbox.InvokeRequired) AddListBoxItemDele

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论