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

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

開發(fā) 后端
C#網(wǎng)絡(luò)編程服務(wù)器端程序?qū)崿F(xiàn)源碼向你介紹了用結(jié)構(gòu)是異步阻塞方式下的程序?qū)崿F(xiàn),希望對你了解和學(xué)習(xí)C#網(wǎng)絡(luò)編程服務(wù)器端程序有所幫助。

C#網(wǎng)絡(luò)編程服務(wù)器端程序?qū)崿F(xiàn)源碼是怎么樣的呢?讓我們來看看其中重要的一部分:

由于在此次程序中我們采用的結(jié)構(gòu)是異步阻塞方式,所以在實(shí)際的程序中,為了不影響服務(wù)器端程序的運(yùn)行速度,我們在程序中設(shè)計(jì)了一個(gè)線程,使得對網(wǎng)絡(luò)請求偵聽,接受和發(fā)送數(shù)據(jù)都在線程中處理,請?jiān)谙旅娴拇a中注意這一點(diǎn),下面是C#網(wǎng)絡(luò)編程服務(wù)器端程序的完整代碼:

  1. //server.cs  
  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. using System.Net ;  
  12. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  13. //導(dǎo)入程序中使用到的名字空間  
  14. public class Form1 : Form  
  15. {  
  16. private ListBox ListBox1 ;  
  17. private Button button2 ;  
  18. private Label label1 ;  
  19. private TextBox textBox1 ;  
  20. private Button button1 ;  
  21. private Socket socketForClient ;  
  22. private NetworkStream networkStream ;  
  23. private TcpListener tcpListener ;  
  24. private StreamWriter streamWriter ;  
  25. private StreamReader streamReader ;  
  26. private Thread _thread1 ;  
  27. private System.ComponentModel.Container components = null ;  
  28. public Form1 ( )  
  29. {  
  30. InitializeComponent ( ) ;  
  31. }  
  32. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  33. //清除程序中使用的各種資源  
  34. protected override void Dispose ( bool disposing )  
  35. {  
  36. if ( disposing )  
  37. {  
  38. if ( components != null )  
  39. {  
  40. components.Dispose ( ) ;  
  41. }  
  42. }  
  43. base.Dispose ( disposing ) ;  
  44. }  
  45. private void InitializeComponent ( )  
  46. {  
  47. label1 = new Label ( ) ;  
  48. button2 = new Button ( ) ;  
  49. button1 = new Button ( ) ;  
  50. ListBox1 = new ListBox ( ) ;  
  51. textBox1 = new TextBox ( ) ;  
  52. SuspendLayout ( ) ;  
  53. label1.Location = new Point ( 8 , 168 ) ;  
  54. label1.Name = "label1" ;  
  55. label1.Size = new Size ( 120 , 23 ) ;  
  56. label1.TabIndex = 3 ;  
  57. label1.Text = "往客戶端反饋信息:" ;  
  58. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  59. //同樣的方式設(shè)置其他控件,這里略去  
  60. this.Controls.Add ( button1 ) ;  
  61. this.Controls.Add ( textBox1 ) ;  
  62. this.Controls.Add ( label1 ) ;  
  63. this.Controls.Add ( button2 ) ;  
  64. this.Controls.Add ( ListBox1 ) ;  
  65. this.MaximizeBox = false ;  
  66. this.MinimizeBox = false ;  
  67. this.Name = "Form1" ;  
  68. this.Text = "C#的網(wǎng)絡(luò)編程服務(wù)器端!" ;  
  69. this.Closed += new System.EventHandler ( this.Form1_Closed ) ;  
  70. this.ResumeLayout ( false ) ;  
  71. }  
  72. private void Listen ( )  
  73. {  
  74. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  75. //創(chuàng)建一個(gè)tcpListener對象,此對象主要是對給定端口進(jìn)行偵聽  
  76. tcpListener = new TcpListener ( 1234 ) ;  
  77. //開始偵聽  
  78. tcpListener.Start ( ) ;  
  79. //返回可以用以處理連接的Socket實(shí)例  
  80. socketForClient = tcpListener.AcceptSocket ( ) ;  
  81. try 
  82. {  
  83. //如果返回值是"true",則產(chǎn)生的套節(jié)字已經(jīng)接受來自遠(yuǎn)方的連接請求  
  84. if ( socketForClient.Connected )  
  85. {  
  86. ListBox1.Items.Add ( "已經(jīng)和客戶端成功連接!" ) ;  
  87. while ( true )  
  88. {  
  89. //創(chuàng)建networkStream對象通過網(wǎng)絡(luò)套節(jié)字來接受和發(fā)送數(shù)據(jù)  
  90. networkStream = new NetworkStream ( socketForClient ) ;  
  91. //從當(dāng)前數(shù)據(jù)流中讀取一行字符,返回值是字符串  
  92. streamReader = new StreamReader ( networkStream ) ;  
  93. string msg = streamReader.ReadLine ( ) ;  
  94. ListBox1.Items.Add ( "收到客戶端信息:" + msg ) ;  
  95. streamWriter = new StreamWriter ( networkStream ) ;  
  96. if ( textBox1.Text != "" )  
  97. {  
  98. ListBox1.Items.Add ( "往客戶端反饋信息:" +   
  99. textBox1.Text ) ;  
  100. //往當(dāng)前的數(shù)據(jù)流中寫入一行字符串  
  101. streamWriter.WriteLine ( textBox1.Text ) ;  
  102. //刷新當(dāng)前數(shù)據(jù)流中的數(shù)據(jù)  
  103. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  104. streamWriter.Flush ( ) ;  
  105. }  
  106. }  
  107. }  
  108. }  
  109. catch ( Exception ey )  
  110. {  
  111. MessageBox.Show ( ey.ToString ( ) ) ;  
  112. }  
  113. }  
  114. static void Main ( )  
  115. {  
  116. Application.Run ( new Form1 ( ) ) ;  
  117. }  
  118. private void button1_Click ( object sender ,  
  119.  System.EventArgs e )  
  120. {  
  121. ListBox1.Items .Add ( "服務(wù)已經(jīng)啟動!" ) ;  
  122. _thread1 = new Thread ( new ThreadStart ( Listen ) ) ;  
  123. _thread1.Start ( ) ;  
  124. }  
  125. private void button2_Click ( object sender ,  
  126.  System.EventArgs e )  
  127. {  
  128. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  129. //關(guān)閉線程和流  
  130. networkStream.Close ( ) ;  
  131. streamReader.Close ( ) ;  
  132. streamWriter.Close ( ) ;  
  133. _thread1.Abort ( ) ;  
  134. tcpListener.Stop ( ) ;  
  135. socketForClient.Shutdown ( SocketShutdown.Both ) ;  
  136. socketForClient.Close ( ) ;  
  137. }  
  138. private void Form1_Closed ( object sender ,  
  139.  System.EventArgs e )  
  140. {  
  141. //C#網(wǎng)絡(luò)編程服務(wù)器端程序  
  142. //關(guān)閉線程和流  
  143. networkStream.Close ( ) ;  
  144. streamReader.Close ( ) ;  
  145. streamWriter.Close ( ) ;  
  146. _thread1.Abort ( ) ;  
  147. tcpListener.Stop ( ) ;  
  148. socketForClient.Shutdown ( SocketShutdown.Both ) ;  
  149. socketForClient.Close ( ) ;  
  150. }  

C#網(wǎng)絡(luò)編程服務(wù)器端程序的實(shí)現(xiàn)源碼就向你介紹到這里,希望對你了解和學(xué)習(xí)C#網(wǎng)絡(luò)編程服務(wù)器端程序有所幫助。

【編輯推薦】

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

2009-08-21 17:33:34

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

2009-08-21 17:53:25

C#網(wǎng)絡(luò)編程客戶端程序

2009-08-21 14:34:34

C#服務(wù)器端表達(dá)式

2015-10-27 09:40:31

TCPIP網(wǎng)絡(luò)協(xié)議

2009-08-21 17:48:43

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

2010-10-15 08:57:15

PHP多進(jìn)程

2012-05-07 13:55:41

JavaJava Web

2009-08-21 16:27:44

C#服務(wù)端程序

2009-08-21 15:36:41

服務(wù)端與客戶端

2011-07-26 11:07:08

JavaScript

2020-06-02 14:57:06

Linux服務(wù)器架構(gòu)

2012-10-15 13:40:15

IBMdw

2009-08-21 14:03:04

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

2010-08-30 13:58:43

服務(wù)器端數(shù)據(jù)庫

2009-08-21 15:22:56

端口偵聽

2009-08-14 11:00:16

C#創(chuàng)建Windows

2014-01-15 10:06:30

vFlash

2009-08-28 16:03:15

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

2013-04-09 12:18:45

socket.ioC服務(wù)器

2009-08-21 14:50:50

C#下MySQL連接字
點(diǎn)贊
收藏

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