C#託管Socket的實現方法(2)

  三.本文介紹程序的設計、調試、運行的軟件環境:

(1).微軟公司視窗2000服務器版

(2).Visual Studio .Net正式版,.Net FrameWork SDK版本號3705

四.利用Socket來傳送數據:

Visual C#在使用Socket傳送數據時要注意下列問題的解決方法:

1.創建Socket實例,使用此實例創建和遠程終結點的連接,並判斷連接是否成功建立。

2.發送數據到Socket,實現數據傳送。

這些問題解決方法都可以在下面介紹代碼中找到相對應的部分。由於下面的代碼都有詳細的註解,

這裏就不詳細介紹。下面是利用Socket傳送數據的具體實現步驟:

1.啓動Visual Studio .Net,並新建一個Visual C#項目,

項目名稱爲【利用Socket來發送數據】。

2.把Visual Studio .Net的當前窗口切換到【Form1.cs(設計)】窗口,

並從【工具箱】中的【Windows窗體組件】選項卡中往Form1窗體中拖入下列組件,並執行相應操作:

二個TextBox組件,一個用以輸入遠程主機的IP地址,一個用以輸入往遠程主機傳送的數據。

一個StausBar組件,用以顯示程序的運行狀況。

一個ListBox組件,用以顯示程序已傳送的數據信息。

三個Label組件。

二個Button組件,名稱分別爲button1、button2,並在這二個組件被拖入窗體後,分別雙擊它們,則系統會在Form1.cs文件中自動產生這二個組件的Click事件對應的處理代碼。

3.【解決方案資源管理器】窗口中,雙擊Form1.cs文件,進入Form1.cs文件的編輯界面。

4.以下面代碼替代系統產生的InitializeComponent過程:
private void InitializeComponent ( )
{
this.label1 = new System.Windows.Forms.Label ( ) ;
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.button1 = new System.Windows.Forms.Button ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.listBox1 = new System.Windows.Forms.ListBox ( ) ;
this.statusBar1 = new System.Windows.Forms.StatusBar ( ) ;
this.label3 = new System.Windows.Forms.Label ( ) ;
this.button2 = new System.Windows.Forms.Button ( ) ;
this.SuspendLayout ( ) ;
this.label1.Location = new System.Drawing.Point ( 24 , 20 ) ;
this.label1.Name = "label1" ;
this.label1.Size = new System.Drawing.Size ( 74 , 30 ) ;
this.label1.TabIndex = 0 ;
this.label1.Text = "IP地址:" ;
this.textBox1.BorderStyle = System.Windows.
Forms.BorderStyle.FixedSingle ;
this.textBox1.Location = new System.Drawing.Point ( 94 , 18 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 166 , 21 ) ;
this.textBox1.TabIndex = 1 ;
this.textBox1.Text = "" ;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button1.Location = new System.Drawing.Point ( 280 , 14 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 62 , 28 ) ;
this.button1.TabIndex = 2 ;
this.button1.Text = "連接" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label2.Location = new System.Drawing.Point ( 16 , 64 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 3 ;
this.label2.Text = "發送信息:" ;
this.textBox2.BorderStyle = System.Windows.
Forms.BorderStyle.FixedSingle ;
this.textBox2.Location = new System.Drawing.Point ( 94 , 58 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 166 , 21 ) ;
this.textBox2.TabIndex = 4 ;
this.textBox2.Text = "" ;
this.listBox1.ItemHeight = 12 ;
this.listBox1.Location = new System.Drawing.Point ( 20 , 118 ) ;
this.listBox1.Name = "listBox1" ;
this.listBox1.Size = new System.Drawing.Size ( 336 , 160 ) ;
this.listBox1.TabIndex = 6 ;
this.statusBar1.Location = new System.Drawing.Point ( 0 , 295 ) ;
this.statusBar1.Name = "statusBar1" ;
this.statusBar1.Size = new System.Drawing.Size ( 370 , 22 ) ;
this.statusBar1.TabIndex = 7 ;
this.statusBar1.Text = "無連接" ;
this.label3.Location = new System.Drawing.Point ( 14 , 94 ) ;
this.label3.Name = "label3" ;
this.label3.Size = new System.Drawing.Size ( 128 , 23 ) ;
this.label3.TabIndex = 8 ;
this.label3.Text = "已經發送的信息:" ;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button2.Location = new System.Drawing.Point ( 280 , 54 ) ;
this.button2.Name = "button2" ;
this.button2.Size = new System.Drawing.Size ( 62 , 28 ) ;
this.button2.TabIndex = 9 ;
this.button2.Text = "發送" ;
this.button2.Click += new System.EventHandler
this.button2_Click ) ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 370 , 317 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control[] {
this.button2 ,
this.statusBar1 ,
this.listBox1 ,
this.textBox2 ,
this.label2 ,
this.button1 ,
this.textBox1 ,
this.label1 ,
this.label3}
 ) ;
this.FormBorderStyle = System.
Windows.Forms.FormBorderStyle.FixedSingle ;
this.MaximizeBox = false ;
this.Name = "Form1" ;
this.Text = "利用Socket來發送數據" ;
this.ResumeLayout ( false ) ;
}
至此【利用Sokcet來傳送數據】項目設計後的界面就完成了

5.在Form1.cs文件的開頭的導入命名空間的代碼區,添加下列代碼,

下列代碼是導入下面程序中使用到的類所在的命名空間:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Net.Sockets ;
//使用到TcpListen類
using System.Net ; 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章