C#網(wǎng)絡(luò)編程客戶端程序?qū)崿F(xiàn)源碼淺析
C#網(wǎng)絡(luò)編程客戶端程序?qū)崿F(xiàn)是如何辦到的呢?由于在客戶端不需要偵聽(tīng)網(wǎng)絡(luò),所以在調(diào)用上面沒(méi)有程序阻塞情況,所以在下面的代碼中,我們沒(méi)有使用到線程,這是和服務(wù)器端程序的一個(gè)區(qū)別的地方。總結(jié)上面的這些關(guān)鍵步驟,可以得到一個(gè)用C#網(wǎng)絡(luò)編程客戶端程序,具體如下:
- //C#網(wǎng)絡(luò)編程客戶端程序
- using System ;
- using System.Drawing ;
- using System.Collections ;
- using System.ComponentModel ;
- using System.Windows.Forms ;
- using System.Data ;
- using System.Net.Sockets ;
- using System.IO ;
- using System.Threading ;
- //C#網(wǎng)絡(luò)編程客戶端程序
- //導(dǎo)入程序中使用到的名字空間
- public class Form1 : Form
- {
- private ListBox ListBox1 ;
- private Label label1 ;
- private TextBox textBox1 ;
- private Button button3 ;
- private NetworkStream networkStream ;
- private StreamReader streamReader ;
- private StreamWriter streamWriter ;
- TcpClient myclient ;
- private Label label2 ;
- private System.ComponentModel.Container
- components = null ;
- public Form1 ( )
- {
- InitializeComponent ( ) ;
- }
- //C#網(wǎng)絡(luò)編程客戶端程序
- //清除程序中使用的各種資源
- protected override void Dispose ( bool disposing )
- {
- if ( disposing )
- {
- if ( components != null )
- {
- components.Dispose ( ) ;
- }
- }
- base.Dispose ( disposing ) ;
- }
- private void InitializeComponent ( )
- {
- label1 = new Label ( ) ;
- button3 = new Button ( ) ;
- ListBox1 = new ListBox ( ) ;
- textBox1 = new TextBox ( ) ;
- label2 = new Label ( ) ;
- SuspendLayout ( ) ;
- label1.Location = new Point ( 8 , 168 ) ;
- label1.Name = "label1" ;
- label1.Size = new Size ( 56 , 23 ) ;
- label1.TabIndex = 3 ;
- label1.Text = "信息:" ;
- //C#網(wǎng)絡(luò)編程客戶端程序
- //同樣方法設(shè)置其他控件
- AutoScaleBaseSize = new Size ( 6 , 14 ) ;
- ClientSize = new Size ( 424 , 205 ) ;
- this.Controls.Add ( button3 ) ;
- this.Controls.Add ( textBox1 ) ;
- this.Controls.Add ( label1 ) ;
- this.Controls.Add ( label2 ) ;
- this.Controls.Add ( ListBox1 ) ;
- this.MaximizeBox = false ;
- this.MinimizeBox = false ;
- this.Name = "Form1" ;
- this.Text = "C#的網(wǎng)絡(luò)編程客戶器端!" ;
- this.Closed += new System.EventHandler ( this.Form1_Closed ) ;
- this.ResumeLayout ( false ) ;
- //連接到服務(wù)器端口,在這里是選用本地機(jī)器作為服務(wù)器,
- //你可以通過(guò)修改IP地址來(lái)改變服務(wù)器
- try
- {
- myclient = new TcpClient ( "localhost" , 1234 ) ;
- }
- catch
- {
- MessageBox.Show ( "沒(méi)有連接到服務(wù)器!" ) ;
- return ;
- }
- //C#網(wǎng)絡(luò)編程客戶端程序
- //創(chuàng)建networkStream對(duì)象通過(guò)網(wǎng)絡(luò)套節(jié)字來(lái)接受和發(fā)送數(shù)據(jù)
- networkStream = myclient.GetStream ( ) ;
- streamReader = new StreamReader ( networkStream ) ;
- streamWriter = new StreamWriter ( networkStream ) ;
- }
- static void Main ( )
- {
- Application.Run ( new Form1 ( ) ) ;
- }
- private void button3_Click (
- object sender , System.EventArgs e )
- {
- if ( textBox1.Text == "" )
- {
- MessageBox.Show ( "請(qǐng)確定文本框?yàn)榉强眨? ) ;
- textBox1.Focus ( ) ;
- return ;
- }
- try
- {
- string s ;
- //往當(dāng)前的數(shù)據(jù)流中寫(xiě)入一行字符串
- streamWriter.WriteLine ( textBox1.Text ) ;
- //刷新當(dāng)前數(shù)據(jù)流中的數(shù)據(jù)
- streamWriter.Flush ( ) ;
- //從當(dāng)前數(shù)據(jù)流中讀取一行字符,返回值是字符串
- s = streamReader.ReadLine ( ) ;
- ListBox1.Items.Add ( "讀取服務(wù)器端發(fā)送內(nèi)容:" + s ) ;
- }
- //C#網(wǎng)絡(luò)編程客戶端程序
- catch ( Exception ee )
- {
- MessageBox.Show (
- "從服務(wù)器端讀取數(shù)據(jù)出現(xiàn)錯(cuò)誤,類型為:" +
- ee.ToString ( ) ) ;
- }
- }
- private void Form1_Closed ( object sender ,
- System.EventArgs e )
- {
- streamReader.Close ( ) ;
- streamWriter.Close ( ) ;
- networkStream.Close ( ) ;
- }
- }
C#網(wǎng)絡(luò)編程客戶端程序的具體實(shí)現(xiàn)代碼就向你介紹到這里,希望地你了解和開(kāi)發(fā)C#網(wǎng)絡(luò)編程客戶端程序有所幫助。
【編輯推薦】