自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#網(wǎng)絡(luò)編程客戶端程序?qū)崿F(xiàn)源碼淺析

開(kāi)發(fā) 后端
C#網(wǎng)絡(luò)編程客戶端程序?qū)崿F(xiàn)在這里我們沒(méi)有使用到線程,這是和服務(wù)器端程序的一個(gè)區(qū)別的地方,本文會(huì)具體的給你演示C#網(wǎng)絡(luò)編程客戶端程序的開(kāi)發(fā)過(guò)程。

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ò)編程客戶端程序,具體如下:

  1. //C#網(wǎng)絡(luò)編程客戶端程序  
  2. using System ;  
  3. using System.Drawing ;  
  4. using System.Collections ;  
  5. using System.ComponentModel ;  
  6. using System.Windows.Forms ;  
  7. using System.Data ;  
  8. using System.Net.Sockets ;  
  9. using System.IO ;  
  10. using System.Threading ;  
  11. //C#網(wǎng)絡(luò)編程客戶端程序  
  12. //導(dǎo)入程序中使用到的名字空間  
  13. public class Form1 : Form  
  14. {  
  15. private ListBox ListBox1 ;  
  16. private Label label1 ;  
  17. private TextBox textBox1 ;  
  18. private Button button3 ;  
  19. private NetworkStream networkStream ;  
  20. private StreamReader streamReader ;  
  21. private StreamWriter streamWriter ;  
  22. TcpClient myclient ;  
  23. private Label label2 ;  
  24. private System.ComponentModel.Container   
  25. components = null ;  
  26. public Form1 ( )  
  27. {  
  28. InitializeComponent ( ) ;  
  29. }  
  30. //C#網(wǎng)絡(luò)編程客戶端程序  
  31. //清除程序中使用的各種資源  
  32. protected override void Dispose ( bool disposing )  
  33. {  
  34. if ( disposing )  
  35. {  
  36. if ( components != null )  
  37. {  
  38. components.Dispose ( ) ;  
  39. }  
  40. }  
  41. base.Dispose ( disposing ) ;  
  42. }  
  43. private void InitializeComponent ( )  
  44. {  
  45. label1 = new Label ( ) ;  
  46. button3 = new Button ( ) ;  
  47. ListBox1 = new ListBox ( ) ;  
  48. textBox1 = new TextBox ( ) ;  
  49. label2 = new Label ( ) ;  
  50. SuspendLayout ( ) ;  
  51. label1.Location = new Point ( 8 , 168 ) ;  
  52. label1.Name = "label1" ;  
  53. label1.Size = new Size ( 56 , 23 ) ;  
  54. label1.TabIndex = 3 ;  
  55. label1.Text = "信息:" ;  
  56. //C#網(wǎng)絡(luò)編程客戶端程序  
  57. //同樣方法設(shè)置其他控件  
  58. AutoScaleBaseSize = new Size ( 6 , 14 ) ;  
  59. ClientSize = new Size ( 424 , 205 ) ;  
  60. this.Controls.Add ( button3 ) ;  
  61. this.Controls.Add ( textBox1 ) ;  
  62. this.Controls.Add ( label1 ) ;  
  63. this.Controls.Add ( label2 ) ;  
  64. this.Controls.Add ( ListBox1 ) ;  
  65. this.MaximizeBox = false ;  
  66. this.MinimizeBox = false ;  
  67. this.Name = "Form1" ;  
  68. this.Text = "C#的網(wǎng)絡(luò)編程客戶器端!" ;  
  69. this.Closed += new System.EventHandler ( this.Form1_Closed ) ;  
  70. this.ResumeLayout ( false ) ;  
  71. //連接到服務(wù)器端口,在這里是選用本地機(jī)器作為服務(wù)器,  
  72. //你可以通過(guò)修改IP地址來(lái)改變服務(wù)器  
  73. try 
  74. {  
  75. myclient = new TcpClient ( "localhost" , 1234 ) ;  
  76. }  
  77. catch 
  78. {  
  79. MessageBox.Show ( "沒(méi)有連接到服務(wù)器!" ) ;  
  80. return ;  
  81. }  
  82. //C#網(wǎng)絡(luò)編程客戶端程序  
  83. //創(chuàng)建networkStream對(duì)象通過(guò)網(wǎng)絡(luò)套節(jié)字來(lái)接受和發(fā)送數(shù)據(jù)  
  84. networkStream = myclient.GetStream ( ) ;  
  85. streamReader = new StreamReader ( networkStream ) ;  
  86. streamWriter = new StreamWriter ( networkStream ) ;  
  87. }  
  88. static void Main ( )  
  89. {  
  90. Application.Run ( new Form1 ( ) ) ;  
  91. }  
  92. private void button3_Click (   
  93. object sender , System.EventArgs e )  
  94. {  
  95. if ( textBox1.Text == "" )  
  96. {  
  97. MessageBox.Show ( "請(qǐng)確定文本框?yàn)榉强眨? ) ;  
  98. textBox1.Focus ( ) ;  
  99. return ;  
  100. }  
  101. try 
  102. {  
  103. string s ;  
  104. //往當(dāng)前的數(shù)據(jù)流中寫(xiě)入一行字符串  
  105. streamWriter.WriteLine ( textBox1.Text ) ;  
  106. //刷新當(dāng)前數(shù)據(jù)流中的數(shù)據(jù)  
  107. streamWriter.Flush ( ) ;  
  108. //從當(dāng)前數(shù)據(jù)流中讀取一行字符,返回值是字符串  
  109. s = streamReader.ReadLine ( ) ;  
  110. ListBox1.Items.Add ( "讀取服務(wù)器端發(fā)送內(nèi)容:" + s ) ;  
  111. }  
  112. //C#網(wǎng)絡(luò)編程客戶端程序  
  113. catch ( Exception ee )  
  114. {  
  115. MessageBox.Show (   
  116. "從服務(wù)器端讀取數(shù)據(jù)出現(xiàn)錯(cuò)誤,類型為:" +   
  117. ee.ToString ( ) ) ;  
  118. }  
  119. }  
  120. private void Form1_Closed ( object sender ,   
  121. System.EventArgs e )  
  122. {  
  123. streamReader.Close ( ) ;  
  124. streamWriter.Close ( ) ;  
  125. networkStream.Close ( ) ;  
  126. }  

C#網(wǎng)絡(luò)編程客戶端程序的具體實(shí)現(xiàn)代碼就向你介紹到這里,希望地你了解和開(kāi)發(fā)C#網(wǎng)絡(luò)編程客戶端程序有所幫助。

【編輯推薦】

  1. ASP.NET異步回調(diào)開(kāi)發(fā)實(shí)例淺析
  2. C#網(wǎng)絡(luò)編程入門(mén)基礎(chǔ)知識(shí)淺析
  3. C#網(wǎng)絡(luò)編程服務(wù)器端程序設(shè)計(jì)淺析
  4. C#網(wǎng)絡(luò)編程服務(wù)器端程序?qū)崿F(xiàn)源碼淺析
  5. C#網(wǎng)絡(luò)編程客戶端程序設(shè)計(jì)淺析
責(zé)任編輯:仲衡 來(lái)源: 天極網(wǎng)
相關(guān)推薦

2009-08-21 17:48:43

C#網(wǎng)絡(luò)編程

2009-08-21 17:39:20

服務(wù)器端程序C#網(wǎng)絡(luò)編程

2009-08-21 16:37:54

C#客戶端程序

2009-08-21 15:36:41

服務(wù)端與客戶端

2009-08-21 15:59:22

服務(wù)端與客戶端通信

2009-08-21 17:33:34

服務(wù)器端程序C#網(wǎng)絡(luò)編程

2009-08-21 15:54:40

服務(wù)端與客戶端

2009-07-15 17:33:08

Swing客戶端

2009-08-06 17:12:13

C# WebServi

2010-02-24 16:39:27

WCF客戶端處理

2011-08-25 10:37:15

leveldb的訪問(wèn)封C#客戶端源碼

2009-10-15 10:46:03

PPC客戶端程序VB.NET創(chuàng)建

2009-08-21 14:03:04

C#網(wǎng)絡(luò)編程

2009-08-28 16:03:15

C#程序?qū)崿F(xiàn)鼠標(biāo)移動(dòng)

2020-03-19 08:00:00

客戶端KubernetesAPI

2009-08-21 14:33:15

C#異步傳輸字符串

2011-04-07 09:33:01

Activex

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2023-11-03 08:15:27

PythonC#

2009-08-21 14:47:39

C#網(wǎng)絡(luò)編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)