当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

用Visual C#实现局域网点对点通讯

  
  点对点即Peer-To-Peer,通常简写为P2P。所谓网络中的点对点,其实可以看成是一种对等的网络模型。P2P其实是实现网络上不同计算机之间,不经过中继设备直接交换数据或服务的一种技术。P2P由于允许网络中任一台计算机可以直接连接到网络中其他计算机,并与之进行数据交换,这样既消除了中间环节,也使得网络上的沟通变得更容易、更直接。
  
  P2P作为一种网络的模型,它有别于传统的客户/服务器模型。客户/服务器模型一般都有预定义的客户机和服务器。而在P2P模型转并没有明确的客户端和服务器,但其实在P2P模型中,每一台计算机既可以看成是服务器,也可以看成是客户机。在网络中,传统上的客户机/服务器通讯模型中,发送服务请求或者发送数据的计算机,一般称为客户机;而接收、处理服务或接收数据的计算机称为服务器。而在P2P网络模型中,计算机不仅接收数据,还有发送数据,不仅提出服务请求,还有接收对方的服务请求。
  
  在下面介绍的用Visual C#实现的局域网点对点通讯程序,就有如下特点,在网络利用此通讯程序进行通讯的任一计算机,在通讯之前,都需要侦听端口号,接受其他机器的连接申请,并在连接建立后,就可以接收对方发送来的数据;同时也可以向其他机器提出连接申请,并在对方计算机允许建立连接请求后,发送数据到对方。可见在网络中利用此软件进行P2P网络通讯的任一计算机既是客户机,同样也是服务器。
  
  一.程序的设计、调试、运行的软件环境:
  
  (1).微软公司视窗2000服务器版
  
  (2).Visual Studio .Net正式版,.Net FrameWork SDK版本号3705
  
  二.关键步骤及其解决方法:
  
  关键步骤就是实现信息在网络中的发送和接收。数据接收使用的是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 Byte[80];
  //定义从远程计算机接收到数据存放的数据缓冲区
  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地址为"10.138.198.213"的计算机的"8888"端口号提出连接申请,并在连接申请建立后,以UTF8编码发送字符串"您好,见到您很高兴"到对方,由于下列代码中的注释比较详细,这里就不具体介绍了,下列代码也是使用NetworkStream传送数据的典型代码:
  
  try
  {
  TcpClient tcpc = new TcpClient ("10.138.198.213",8888);
  //对IP地址为"10.138.198.213"的计算机的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 ("无法发送信息到目标计算机!") ;
  }
  
  当然在具体用Visual C#实现网络点对点通讯程序时,还必须掌握很多其他方面的知识,如资源的回收。在用Visual C#编写网络应用程序的时候,很多朋友遇到这样的情况。当程序退出后,通过Windows的"资源管理器"看到的是进程数目并没有减少。这是因为程序中使用的线程可能并没有有效退出。虽然Thread类中提供了"Abort"方法用以中止进程,但并不能够保证成功退出。因为进程中使用的某些资源并没有回收。在某些情况下垃圾回收器也不能保证完全的回收资源,还是需要我们自己手动回收资源的。在本文介绍的程序中也涉及到资源手动回收的问题。实现方法可参阅下面具体实现步骤中的第十二步。
  
  三.具体步骤:
  
  在了解、掌握了上面的关键问题及其解决方法后,再实现用Visual C#实现网络点对点通讯程序相对就容易许多,下面是具体的实现步骤:
  
  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.Na
相关内容
赞助商链接