网络管理实验指导书.doc_第1页
网络管理实验指导书.doc_第2页
网络管理实验指导书.doc_第3页
网络管理实验指导书.doc_第4页
网络管理实验指导书.doc_第5页
已阅读5页,还剩101页未读 继续免费阅读

下载本文档

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

文档简介

网络管理实验指导书(计科专业)2013.8目 录实验一 Microsoft .net开发环境的熟悉和使用1*实验二 C#的数据类型和控制语句5*实验三 类及多线程开发11实验四 套接字编程17实验五 TCP协议26实验六 UDP协议71实验七 PING程序设计实现76*实验八 网络聊天室的设计与实现87注:*为选做实验实验一 Microsoft .net开发环境的熟悉和使用实验目的:熟悉Microsoft .net开发环境,掌握.net环境下控制台程序和windows应用程序的开发方法实验类型:验证型实验要求:必修仪器设备:计算机实验内容、方法、步骤:实验一、利用VS.Net开发环境分别编写控制台应用程序和windows应用程序。控制台程序要求显示“hello world”。Windows应用程序要求用一个label控件和一个button控件,点击button将在label控件上显示hello world。实验步骤及代码: 控制台应用程序1)在装有Visual Studio.NET的计算机上,运行Visual Studio.Net后,选择“文件”-“新建”-“项目”打开新建项目对话框。2)项目类型选择“Visual C#项目”,模板选择“控制台程序”,输入项目名ConsoleApplication1,并选择一个合适的保存位置。如下图所示:3)单击确定,然后将自动生成的文件更改为下面的内容:using System;namespace ConsoleApplication1/ / Class1 的摘要说明。/ class Class1/ / 应用程序的主入口点。/ STAThreadstatic void Main(string args)Console.WriteLine(Hello World);/添加的语句4)按键编译并运行应用程序,此时屏幕就会弹出一个窗口,显示“Hello World”,按一下键又返回到开发环境。 Windows应用程序1)在装有Visual Studio.NET的计算机上,运行Visual Studio.Net后,选择“文件”-“新建”-“项目”打开新建项目对话框。2)项目类型选择“Visual C#项目”,模板选择“Windows应用程序”,输入项目名lab1-2,并选择一个合适的保存位置,单击确定。如下图所示:3)从工具箱中向设计窗体拖放一个Label控件和Button控件,如下图所示:4)双击“button1”按钮,在光标所在位置添加相应代码:private void button1_Click(object sender, System.EventArgs e)label1.Text=Hello World;/添加的代码5)按键编译并运行程序,屏幕上就会弹出一个窗口,在按纽上点击则相应的label上显示“Hello World”,运行效果如下图:*实验二 C#的数据类型和控制语句实验目的:掌握c#的各种控制语句及数据类型实验类型:验证型实验要求:必修仪器设备:计算机实验内容、方法、步骤:一、编写一个控制台应用程序,输出1-5的平方值,要求:1)用for语句实现2)用while语句实现3)用do-while语句实现4)用foreach语句实现步骤及代码:新建一个控制台应用程序,在Main方法中添加如下代码。using System;namespace ConsoleApplication1/ / Class1 的摘要说明。/ class Class1/ / 应用程序的主入口点。/ STAThreadstatic void Main(string args) int i;for( i=1;i=5;i+)Console.WriteLine(i*i);i=1;while(i=5) Console.WriteLine(i*i);i+;i=1;doConsole.WriteLine(i*i);i+;while(i=5);int j=1,2,3,4,5;foreach(int x in j)Console.WriteLine(x*x);Console.Read();二、 编写一个控制台程序,采用数组实现排序算法(冒泡排序、选择排序、插入排序、希尔排序任选)步骤及代码:新建一个控制台应用程序。添加如下代码:冒泡排序using System; namespace BubbleSorterpublic class BubbleSorterpublic void Sort(int list)int i,j,temp;bool done=false;j=1;while(jlist.Length)&(!done)done=true;for(i=0;ilist.Length-j;i+)if(listilisti+1)done=false;temp=listi;listi=listi+1;listi+1=temp;j+;public class MainClass public static void Main()int iArrary=new int1,5,13,6,10,55,99,2,87,12,34,75,33,47;BubbleSorter sh=new BubbleSorter();sh.Sort(iArrary);for(int m=0;miArrary.Length;m+)Console.Write(0 ,iArrarym); Console.WriteLine();选择排序using System;namespace SelectionSorterpublic class SelectionSorter private int min;public void Sort(int list)for(int i=0;ilist.Length-1;i+)min=i;for(int j=i+1;jlist.Length;j+)if(listjlistmin)min=j;int t=listmin;listmin=listi;listi=t;public class MainClass public static void Main()int iArrary=new int1,5,3,6,10,55,9,2,87,12,34,75,33,47;SelectionSorter ss=new SelectionSorter();ss.Sort(iArrary);for(int m=0;miArrary.Length;m+)Console.Write(0 ,iArrarym); Console.WriteLine();插入排序using System;namespace InsertionSorterpublic class InsertionSorterpublic void Sort(int list)for(int i=1;ilist.Length;i+)int t=listi;int j=i;while(j0)&(listj-1t)listj=listj-1;-j;listj=t;public class MainClass public static void Main()int iArrary=new int1,13,3,6,10,55,98,2,87,12,34,75,33,47;InsertionSorter ii=new InsertionSorter();ii.Sort(iArrary);for(int m=0;miArrary.Length;m+)Console.Write(0,iArrarym); Console.WriteLine();希尔排序using System; namespace ShellSorterpublic class ShellSorterpublic void Sort(int list)int inc;for(inc=1;inc=list.Length/9;inc=3*inc+1);for(;inc0;inc/=3)for(int i=inc+1;i=list.Length;i+=inc)int t=listi-1;int j=i;while(jinc)&(listj-inc-1t)listj-1=listj-inc-1;j-=inc;listj-1=t;public class MainClass public static void Main()int iArrary=new int1,5,13,6,10,55,99,2,87,12,34,75,33,47;ShellSorter sh=new ShellSorter();sh.Sort(iArrary);for(int m=0;miArrary.Length;m+)Console.Write(0 ,iArrarym); Console.WriteLine(); *实验三 类及多线程开发实验题目: 实验目的:熟悉和掌握c#类和多线程实验类型:验证型实验要求:必修仪器设备:计算机实验内容、方法、步骤:一、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。1) 声明Shape类,两个构造函数,一个带参数,一个不带参数,一个抽象方法Area()。并声明Shape类的子类Rectagle和Ecllipse类,在子类中重载抽象方法。在Rectagle类中添加两个属性length和width.using System;namespace ConsoleApplication2/ / Class1 的摘要说明。/ class Class1/ / 应用程序的主入口点。/ STAThreadstatic void Main(string args)/ TODO: 在此处添加代码以启动应用程序/ double len=2.5;double wid=3;double rad=4.1;Rectangle aRect=new Rectangle();aRect.length=len;aRect.width=wid;Circle aCirc=new Circle(rad);Console.WriteLine(aRect.Area();Console.WriteLine(aCirc.Area();abstract class Shape public const double pi=3.14;protected double x,y;public Shape() x=y=0;public Shape(double x,double y)this.x=x;this.y=y;public abstract double Area(); class Rectangle:Shapepublic Rectangle():base()public Rectangle(double x,double y):base(x,y)public override double Area()return (x*y);public double lengthgetreturn x;setif (value0)x=value;public double widthgetreturn y;setif(value0)y=value;class Ellipse:Shapepublic Ellipse(double x,double y):base(x,y)public override double Area()return pi*x*y;class Circle:Ellipsepublic Circle(double r):base(r,0)public override double Area()return pi*x*x;二多线程:编写如下Windows应用程序,实现线程的启动、休眠、挂起、停止功能。这里线程是用三重循环模拟的一个计算线程。步骤及代码:1)新建一个windows应用程序,并添加一个新窗体“Form2”2)在Form1和Form2中分别添加控件,如图所示。3)在Form1的Form1_Load中添加代码。 private void Form1_Load(object sender, System.EventArgs e)calcForm=new Form2();calcForm.Hide();private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)switch(toolBar1.Buttons.IndexOf(e.Button)case 0: if(calcThread=null) calcForm.Show(); calcThread=new Thread(new ThreadStart(calcForm.MyCalculate); calcThread.Priority=ThreadPriority.Normal; calcThread.Start(); break;case 1:Thread.Sleep(10*1000);break;case 2:if (calcThread.ThreadState=ThreadState.Running)calcThread.Suspend();e.Button.Text=Resume;else if(calcThread.ThreadState=ThreadState.Suspended)calcThread.Resume();e.Button.Text=Suspend;break;case 3:if(calcThread.IsAlive)calcThread.Abort();calcForm.Hide();break;4)在form2中添加一个方法。public void MyCalculate()while(true)for(int i=0;i1000;i+)for(int j=0;j1000;j+)double k=123.456;progressBar1.Value%=progressBar1.Maximum;progressBar1.Value+; 5)按键编译并运行程序,运行效果如图所示。实验四 套接字编程实验目的:理解Winsock API编程的原理,掌握网络程序设计的基本方法。实验类型:验证型实验要求:必修仪器设备:计算机实验内容、方法、步骤:1)编写一个windows应用程序,实现域名解析(使用IPHostEntry类)。例如输入某个网站的域名,给出该站点的 IP地址、主机名等相关信息;输入IP地址,如果有域名,作相反转换,给出域名。代码参考:下面的代码列出了服务器的相关别名列表以及IP地址列表的长度并将所有的IP地址列出: IPHostEntry IPHost = Dns.Resolve(/); string aliases = IPHost.Aliases; Console.WriteLine(aliases.Length); IPAddress addr = IPHost.AddressList; Console.WriteLine(addr.Length); for(int i= 0; i addr.Length ; i+) Console.WriteLine(addri); 2) 使用TcpClient和TcpListener简化TCP编程。代码参考:服务器端using System;using System.Net.Sockets;using System.IO ;public class Echoserverpublic static void Main()TcpListener tcpListener = new TcpListener(1234); tcpListener.Start(); Console.WriteLine(Server Started) ;Socket socketForClient = tcpListener.AcceptSocket(); try if (socketForClient.Connected) while(true) Console.WriteLine(Client connected);NetworkStream networkStream = new NetworkStream(socketForClient); StreamWriter streamWriter = new StreamWriter(networkStream); StreamReader streamReader = new StreamReader(networkStream); string line = streamReader.ReadLine(); Console.WriteLine(Read: +line);line=line.ToUpper()+ !;streamWriter.WriteLine(line);Console.WriteLine(Wrote:+line); streamWriter.Flush() ; socketForClient.Close(); Console.WriteLine(Exiting.); catch(Exception e) Console.WriteLine(e.ToString() ; 客户端using System;using System.Net.Sockets;using System.Windows.Forms;using System.IO ;using System.ComponentModel ;using System.Drawing;public class Echoclient: Form /Define the components. private Button b1; private TextBox t1,ta; TcpClient myclient; private NetworkStream networkStream ; private StreamReader streamReader ; private StreamWriter streamWriter ; public Echoclient() InitializeComponent(); public static void Main() Echoclient df=new Echoclient(); df.FormBorderStyle=FormBorderStyle.Fixed3D; Application.Run(df); public void InitializeComponent() b1=new Button(); b1.Location = new System.Drawing.Point(170,20); b1.Size = new System.Drawing.Size (80,40); b1.Text=Click Here; b1.Click += new System.EventHandler(b1_Click); b1.BackColor = System.Drawing.Color.Transparent ;b1.ForeColor = System.Drawing.Color.Red ;b1.BackgroundImage = Image.FromFile(back4.gif) ; b1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; b1.Font = new System.Drawing.Font(Microsoft Sans Serif, 8f, System.Drawing.FontStyle.Bold); t1=new TextBox(); t1.Location = new System.Drawing.Point(20,20); t1.Size = new System.Drawing.Size (100,100); ta=new TextBox();ta.Multiline=true;ta.ScrollBars = ScrollBars.Vertical; ta.AcceptsReturn = true; ta.AcceptsTab = true; ta.WordWrap = true; ta.Location = new System.Drawing.Point(20,80); this.SuspendLayout(); this.Text = Socket Programming ;this.MaximizeBox = false;this.BackgroundImage = Image.FromFile(back3.gif) ; this.Name = Form1; this.Controls.Add(this.b1); this.Controls.Add(this.t1); this.Controls.Add(this.ta); this.Closing+= new CancelEventHandler(form1_closing) ;try myclient = new TcpClient(localhost, 1234); catch Console.WriteLine(Failed to connect to server at 0:999, localhost); return; networkStream = myclient.GetStream(); streamReader = new StreamReader(networkStream); streamWriter = new StreamWriter(networkStream); private void b1_Click(object sender, EventArgs e) ta.Text= ; if(t1.Text=)MessageBox.Show(Please enter something in the textbox);t1.Focus();return ; try string s; streamWriter.WriteLine(t1.Text);Console.WriteLine(Sending Message); streamWriter.Flush();s= streamReader.ReadLine();Console.WriteLine(Reading Message) ;Console.WriteLine(s) ;ta.Text=s; catch(Exception ee) Console.WriteLine(Exception reading from Server:+ee.ToString(); public void form1_closing(object o , CancelEventArgs ec) /close all streams.streamReader.Close() ; streamWriter.Close() ; networkStream.Close(); 实验五 TCP协议实验目的:熟悉和掌握TCP协议开发实验类型:验证型实验要求:必修仪器设备:计算机实验内容、方法、步骤:一、实现局域网点对点(P2P)通讯。在P2P模型中,每一台计算机既可以看成是服务器,也可以看成是客户机。计算机不仅接收数据,还有发送数据,不仅提出服务请求,还有接收对方的服务请求。 利用此通讯程序进行通讯的任一计算机,在通讯之前,都需要侦听端口号,接受其他机器的连接申请,并在连接建立后,就可以接收对方发送来的数据;同时也可以向其他机器提出连接申请,并在对方计算机允许建立连接请求后,发送数据到对方。二步骤及方法: 关键是实现信息在网络中的发送和接收。数据接收使用的是Socket,数据发送使用的是NetworkStream。 1.利用Socket来接收信息: 为了更清楚的说明问题,程序在处理数据发送和接收时采用了不同的端口号,发送数据程序在缺省状态设定的端口号为8889。下面代码是侦听端口号8889,接受网络中对此端口号的连接请求,并在建立连接后,通过Socket接收远程计算机发送来的数据: try TcpListener tlListen1 = new TcpListener ( 8889 ) ; /侦听端口号 tlListen1.Start ( ) ; Socket skSocket = tlListen1.AcceptSocket ( ); /接受远程计算机的连接请求,并获得用以接收数据的Socket实例 EndPoint tempRemoteEP = skSocket.RemoteEndPoint; /获得远程计算机对应的网络远程终结点 while (true) Byte byStream = new Byte80; /定义从远程计算机接收到数据存放的数据缓冲区 int i = skSocket.ReceiveFrom(byStream,ref tempRemoteEP); /接收数据,并存放到定义的缓冲区中 string sMessage = System.Text.Encoding.UTF8.GetString(byStream); /以指定的编码,从缓冲区中解析出内容 MessageBox.Show ( sMessage ); /显示传送来的数据 catch ( System.Security.SecurityException ) MessageBox.Show ( 防火墙安全错误!,错误, MessageBoxButtons.OK , MessageBoxIcon.Exclamation); 2.利用NetworkStream来传送信息: 在使用StreamWriter处理NetworkStream传送数据时,数据传送的编码类型是UTF8,下列代码是对IP地址为的计算机的8888端口号提出连接申请,并在连接申请建立后,以UTF8编码发送字符串您好,见到您很高兴到对方,下列代码也是使用NetworkStream传送数据的典型代码: try TcpClient tcpc = new TcpClient (,8888); /对IP地址为的计算机的8888端口提出连接申请 NetworkStream tcpStream = tcpc.GetStream ( ); /如果连接申请建立,则获得用以传送数据的数据流 catch ( Exception ) MessageBox.Show ( 目标计算机拒绝连接请求! ) ; break ; try string sMsg = 您好,见到您很高兴 ; StreamWriter reqStreamW = new StreamWriter (tcpStream); /以特定的编码往向数据流中写入数据 ,默认为UTF8编码 reqStreamW.Write (sMsg); /将字符串写入数据流中 reqStreamW.Flush ( ); /清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流 catch(Exception) MessageBox.Show (无法发送信息到目标计算机!) ; 三具体步骤: 1.启动Visual Studio .Net,并新建一个Visual C项目,名称为【Visual C实现网络点对点通讯程序】。 2.在Visual Studio .Net集成开发环境中的【解决方案资源管理器】窗口中,双击Form1.cs文件,进入Form1.cs文件的编辑界面。 3.在Form1.cs文件的开头,用下列导入命名空间代码替代系统缺省的导入命名空间代码。 using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Net.Sockets ; using System.Net ; using System.IO ; using System.Text ; using System.Threading ; 4.再把Visual Studio.Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往窗体中拖入下列组件: 四个Button组件;二个ListBox组件;四个TextBox组件;一个StatusBar组件;五个Label组件。并在四个Button组件拖入窗体后,分别在窗体设计界面中双击它们,则系统会在Form1.cs文件中分别产生这四个组件的Click事件对应的处理代码。 5.在【解决方案资源管理器】窗口中,双击Form1.cs文件,进入Form1.cs文件的编辑界面。以下面代码替代系统产生的InitializeComponent过程。下面代码是对上面添加的组件进行初始化: private void InitializeComponent ( ) this.listBox1 = new System.Windows.Forms.ListBox ( ) ; this.textBox1 = new System.Windows.Forms.TextBox ( ) ; this.label3 = new System.Windows.Forms.Label ( ) ; this.label2 = new System.Windows.Forms.Label ( ) ; this.textBox3 = new System.Windows.Forms.TextBox ( ) ; this.button1 = new System.Windows.Forms.Button ( ) ; this.textBox2 = new System.Windows.Forms.TextBox ( ) ; this.label1 = new System.Windows.Forms.Label ( ) ; this.label4 = new System.Windows.Forms.Label ( ) ; this.label5 = new System.Windows.Forms.Label ( ) ; this.button2 = new System.Windows.Forms.Button ( ) ; this.button3 = new System.Windows.Forms.Button ( ) ; this.button4 = new System.Windows.Forms.Button ( ) ; this.textBox4 = new System.Windows.Forms.TextBox ( ) ; this.statusBar1 = new System.Windows.Forms.StatusBar ( ) ; this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel( ); this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel( ); this.label6 = new System.Windows.Forms.Label ( ) ; this.listBox2 = new System.Windows.Forms.ListBox ( ) ; ( ( System.ComponentModel.ISupportInitialize ) ( this.statusBarPanel1 ) ).BeginInit ( ) ; ( ( System.ComponentModel.ISupportInitialize ) ( this.statusBarPanel2 ) ).BeginInit ( ) ; this.SuspendLayout ( ) ; this.listBox1.ItemHeight = 12 ; this.listBox1.Location = new System.Drawing.Point ( 122 , 110 ) ; this.listBox1.Name = listBox1 ; this.listBox1.Size = new System.Drawing.Size ( 212 , 88 ) ; this.listBox1.TabIndex = 4 ; this.textBox1.Location = new System.Drawing.Point ( 122 , 18 ) ; this.textBox1.Name = textBox1 ; this.textBox1.Size = new System.Drawing.Size ( 210 , 21 ) ; this.textBox1.TabIndex = 1 ; this.textBox1.Text = ; this.label3.Location = new System.Drawing.Point ( 220 , 52 ) ; this.label3.Name = label3 ; this.label3.Size = new System.Drawing.Size ( 66 , 23 ) ; this.label3.TabIndex = 7 ; this.label3.Text = 本地端口: ; this.label2.Location = new System.Drawing.Point ( 38 , 54 ) ; this.label2.Name = label2 ; this.label2.Size = new System.Drawing.Size ( 80 , 23 ) ; this.label2.TabIndex = 20 ; this.label2.Text = 远程端口号: ; this.textBox3.Location = new System.Drawi

温馨提示

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

评论

0/150

提交评论