jolielee 发表于 2008-12-14 11:26:52

实例解析SOCKET编程模型(1)

<br>  Socket(套接字)是一种独立于协议的网络编程接口,在OSI模型中,主要集中于会话层和传输层。Socket 实际上代表的是两个实体之间进行通信的有效端点。通过socket可以获得源IP地址和源端口、终点IP地址和终点端口。用户可以将多个socket连入同一个端口,以便对于单个端口可以有多个连接。通过socket客户/服务器编程可以创建一个能被许多人使用的分布式程序,并且所有客户均可以用统一的前端进行工作,并与服务器进行通信。这与老式或新式电话机之间的通话过程是十分相似的。<br>  <br>  在.NET中,System.Net.Sockets 命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现。Socket 类为网络通信提供了一套丰富的方法和属性。<br>  <br>  如果应用程序在执行期间只需要一个线程,请使用下面的方法,这些方法适用于同步操作模式。 <br>  <br>  如果当前使用的是面向连接的协议(如 TCP),则服务器可以使用 Listen 方法侦听连接。Accept 方法处理任何传入的连接请求,并返回可用于与远程主机进行数据通信的 Socket。可以使用此返回的 Socket 来调用 Send 或 Receive 方法。如果要指定本地 IP 地址和端口号,请在调用 Listen 方法之前先调用 Bind 方法。如果不调用 Bind,基础服务提供程序将为您分配这些值。此后,可以使用 LocalEndPoint 属性来标识分配给 Socket 的 IP 地址和端口号。如果想连接到侦听主机,请调用 Connect 方法。若要进行数据通信,请调用 Send 或 Receive 方法。 <br>  <br>  如果当前使用的是无连接协议(如 UDP),则根本不需要侦听连接。调用 ReceiveFrom 方法可接受任何传入的数据报。使用 SendTo 方法可将数据报发送到远程主机。<br>  <center><img src=/bbs/attachments/computer/20081214/2008121411183185977801.gif></center><br>  开发一个简单的同步网络聊天程序程序分为服务器和客户端。连接成功后,服务器和客户端之间可以进行对话。源码中对每一个涉及到的对初学者较难理解的类\方法\属性都有详细的注释。以下是服务器端代码。<br>  using System;<br>  using System.Drawing;<br>  using System.Collections;<br>  using System.ComponentModel;<br>  using System.Windows.Forms;<br>  using System.Data;<br>  using System.Net;<br>  using System.Net.Sockets;<br>  using System.Threading;<br>  using System.Text;<br>  namespace 聊天_socket<br>  {<br>   /// <summary><br>   /// Form1 的摘要说明。<br>   /// </summary><br>   public class Form1 : System.Windows.Forms.Form<br>   {<br>   private System.Windows.Forms.StatusBar statusBar1;<br>   private System.Windows.Forms.Label label1;<br>   private System.Windows.Forms.Label label2;<br>   private System.Windows.Forms.Label label3;<br>   private System.Windows.Forms.Label label4;<br>   private System.Windows.Forms.RichTextBox rtbReceive;<br>   private System.Windows.Forms.RichTextBox rtbSend;<br>   private System.Windows.Forms.TextBox txtServer;<br>   private System.Windows.Forms.TextBox txtPort;<br>   private System.Windows.Forms.Button btnListen;<br>   private System.Windows.Forms.Button btnSend;<br>   private System.Windows.Forms.Button btnStop;<br>   private IPAddress hostIPAddress=IPAddress.Parse("127.0.0.1");<br>   private IPEndPoint Server;<br>   private Socket listeningSocket;<br>   private Socket mySocket;<br>   string port;<br>   /// <summary><br>   /// 必需的设计器变量。<br>   /// </summary><br>   private System.ComponentModel.Container components = null;<br>  <br>   public Form1()<br>   {<br>  //<br>  // Windows 窗体设计器支持所必需的<br>  //<br>  InitializeComponent();<br>  <br>  //<br>  // TODO: 在 InitializeComponent 调用后添加任何构造函数代码<br>  //<br>   }<br>  <br>   /// <summary><br>   /// 清理所有正在使用的资源。<br>   /// </summary><br>   protected override void Dispose( bool disposing )<br>   {<br>  if( disposing )<br>  {<br>  if (components != null) <br>  {<br>   components.Dispose();<br>  }<br>  }<br>  base.Dispose( disposing );<br>   }<br>  <br>   #region Windows 窗体设计器生成的代码<br>   /// <summary><br>   /// 设计器支持所需的方法 - 不要使用代码编辑器修改<br>   /// 此方法的内容。<br>   /// </summary><br>   private void InitializeComponent()<br>   {<br>  this.rtbReceive = new System.Windows.Forms.RichTextBox();<br>  this.rtbSend = new System.Windows.Forms.RichTextBox();<br>  this.txtServer = new System.Windows.Forms.TextBox();<br>  this.txtPort = new System.Windows.Forms.TextBox();<br>  this.statusBar1 = new System.Windows.Forms.StatusBar();<br>  this.btnListen = new System.Windows.Forms.Button();<br>  this.btnSend = new System.Windows.Forms.Button();<br>  this.btnStop = new System.Windows.Forms.Button();<br>  this.label1 = new System.Windows.Forms.Label();<br>  this.label2 = new System.Windows.Forms.Label();<br>  this.label3 = new System.Windows.Forms.Label();<br>  this.label4 = new System.Windows.Forms.Label();<br>  this.SuspendLayout();<br>  // <br>  // rtbReceive<br>  // <br>  this.rtbReceive.Location = new System.Drawing.Point(80, 56);<br>  this.rtbReceive.Name = "rtbReceive";<br>  this.rtbReceive.Size = new System.Drawing.Size(264, 96);<br>  this.rtbReceive.TabIndex = 0;<br>  this.rtbReceive.Text = "";<br>  // <br>  // rtbSend<br>  // <br>  this.rtbSend.Location = new System.Drawing.Point(80, 152);<br>  this.rtbSend.Name = "rtbSend";<br>  this.rtbSend.Size = new System.Drawing.Size(264, 96);<br>  this.rtbSend.TabIndex = 1;<br>  this.rtbSend.Text = "";<br>  // <br>  // txtServer<br>  // <br>  this.txtServer.Location = new System.Drawing.Point(72, 16);<br>  this.txtServer.Name = "txtServer";<br>  this.txtServer.TabIndex = 2;<br>  this.txtServer.Text = "127.0.0.1";<br>  // <br>  // txtPort<br>  // <br>  this.txtPort.Location = new System.Drawing.Point(288, 16);<br>  this.txtPort.Name = "txtPort";<br>  this.txtPort.Size = new System.Drawing.Size(48, 21);<br>  this.txtPort.TabIndex = 3;<br>  this.txtPort.Text = "19811";<br>  // <br>  // statusBar1<br>  // <br>  this.statusBar1.Location = new System.Drawing.Point(0, 287);<br>  this.statusBar1.Name = "statusBar1";<br>  this.statusBar1.ShowPanels = true;<br>  this.statusBar1.Size = new System.Drawing.Size(360, 22);<br>  this.statusBar1.TabIndex = 4;<br>  this.statusBar1.Text = "statusBar1";<br>  // <br>  // btnListen<br>  // <br>  this.btnListen.Location = new System.Drawing.Point(32, 256);<br>  this.btnListen.Name = "btnListen";<br>  this.btnListen.TabIndex = 5;<br>  this.btnListen.Text = "开始监听";<br>  this.btnListen.Click= new System.EventHandler(this.btnListen_Click);<br>  // <br>  // btnSend<br>  // <br>  this.btnSend.Location = new System.Drawing.Point(144, 256);<br>  this.btnSend.Name = "btnSend";<br>  this.btnSend.TabIndex = 6;<br>  this.btnSend.Text = "发送信息";<br>  this.btnSend.Click= new System.EventHandler(this.btnSend_Click);<br>  // <br>  // btnStop<br>  // <br>  this.btnStop.Location = new System.Drawing.Point(256, 256);<br>  this.btnStop.Name = "btnStop";<br>  this.btnStop.TabIndex = 7;<br>  this.btnStop.Text = "停止监听";<br>  this.btnStop.Click= new System.EventHandler(this.btnStop_Click);<br>  // <br>  // label1<br>  // <br>  this.label1.Location = new System.Drawing.Point(16, 16);<br>  this.label1.Name = "label1";<br>  this.label1.Size = new System.Drawing.Size(56, 23);<br>  this.label1.TabIndex = 8;<br>  this.label1.Text = "服务器:";<br>  // <br>  // label2<br>  // <br>  this.label2.Location = new System.Drawing.Point(216, 16);<br>  this.label2.Name = "label2";<br>  this.label2.Size = new System.Drawing.Size(64, 23);<br>  this.label2.TabIndex = 9;<br>  this.label2.Text = "监听端口:";<br>  // <br>  // label3<br>  // <br>  this.label3.Location = new System.Drawing.Point(16, 64);<br>  this <
页: [1]
查看完整版本: 实例解析SOCKET编程模型(1)