溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何進行C#網(wǎng)絡(luò)編程客戶端程序的實現(xiàn)源碼分析

發(fā)布時間:2021-11-24 09:12:10 來源:億速云 閱讀:147 作者:柒染 欄目:編程語言

本篇文章給大家分享的是有關(guān)如何進行C#網(wǎng)絡(luò)編程客戶端程序的實現(xiàn)源碼分析,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

C#網(wǎng)絡(luò)編程客戶端程序?qū)崿F(xiàn)是如何辦到的呢?由于在客戶端不需要偵聽網(wǎng)絡(luò),所以在調(diào)用上面沒有程序阻塞情況,所以在下面的代碼中,我們沒有使用到線程,這是和服務(wù)器端程序的一個區(qū)別的地方??偨Y(jié)上面的這些關(guān)鍵步驟,可以得到一個用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ù)器端口,在這里是選用本地機器作為服務(wù)器,  //你可以通過修改IP地址來改變服務(wù)器  try {  myclient = new TcpClient ( "localhost" , 1234 ) ;  }  catch {  MessageBox.Show ( "沒有連接到服務(wù)器!" ) ;  return ;  }  //C#網(wǎng)絡(luò)編程客戶端程序  //創(chuàng)建networkStream對象通過網(wǎng)絡(luò)套節(jié)字來接受和發(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 ( "請確定文本框為非空!" ) ;  textBox1.Focus ( ) ;  return ;  }  try {  string s ;  //往當(dāng)前的數(shù)據(jù)流中寫入一行字符串  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)錯誤,類型為:" +   ee.ToString ( ) ) ;  }  }  private void Form1_Closed ( object sender ,   System.EventArgs e )  {  streamReader.Close ( ) ;  streamWriter.Close ( ) ;  networkStream.Close ( ) ;  }  }

以上就是如何進行C#網(wǎng)絡(luò)編程客戶端程序的實現(xiàn)源碼分析,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI