C#网络编程服务器端程序实现源码浅析

C#网络编程服务器端程序实现源码是怎么样的呢?让我们来看看其中重要的一部分:

    由于在此次程序中我们采用的结构是异步阻塞方式,所以在实际的程序中,为了不影响服务器端程序的运行速度,我们在程序中设计了一个线程,使得对网络请求侦听,接受和发送数据都在线程中处理,请在下面的代码中注意这一点,下面是C#网络编程服务器端程序的完整代码:

  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#网络编程服务器端程序 
  13. //导入程序中使用到的名字空间 
  14. public class Form1 : Form 
  15. private ListBox ListBox1 ; 
  16. private Button button2 ; 
  17. private Label label1 ; 
  18. private TextBox textBox1 ; 
  19. private Button button1 ; 
  20. private Socket socketForClient ; 
  21. private NetworkStream networkStream ; 
  22. private TcpListener tcpListener ; 
  23. private StreamWriter streamWriter ; 
  24. private StreamReader streamReader ; 
  25. private Thread _thread1 ; 
  26. private System.ComponentModel.Container components = null
  27. public Form1 ( ) 
  28. InitializeComponent ( ) ; 
  29. //C#网络编程服务器端程序 
  30. //清除程序中使用的各种资源 
  31. protected override void Dispose ( bool disposing ) 
  32. if ( disposing ) 
  33. if ( components != null
  34. components.Dispose ( ) ; 
  35. base.Dispose ( disposing ) ; 
  36. private void InitializeComponent ( ) 
  37. label1 = new Label ( ) ; 
  38. button2 = new Button ( ) ; 
  39. button1 = new Button ( ) ; 
  40. ListBox1 = new ListBox ( ) ; 
  41. textBox1 = new TextBox ( ) ; 
  42. SuspendLayout ( ) ; 
  43. label1.Location = new Point ( 8 , 168 ) ; 
  44. label1.Name = "label1" ; 
  45. label1.Size = new Size ( 120 , 23 ) ; 
  46. label1.TabIndex = 3 ; 
  47. label1.Text = "往客户端反馈信息:" ; 
  48. //C#网络编程服务器端程序 
  49. //同样的方式设置其他控件,这里略去 
  50. this.Controls.Add ( button1 ) ; 
  51. this.Controls.Add ( textBox1 ) ; 
  52. this.Controls.Add ( label1 ) ; 
  53. this.Controls.Add ( button2 ) ; 
  54. this.Controls.Add ( ListBox1 ) ; 
  55. this.MaximizeBox = false
  56. this.MinimizeBox = false
  57. this.Name = "Form1" ; 
  58. this.Text = "C#的网络编程服务器端!" ; 
  59. this.Closed += new System.EventHandler ( this.Form1_Closed ) ; 
  60. this.ResumeLayout ( false ) ; 
  61. private void Listen ( ) 
  62. //C#网络编程服务器端程序 
  63. //创建一个tcpListener对象,此对象主要是对给定端口进行侦听 
  64. tcpListener = new TcpListener ( 1234 ) ; 
  65. //开始侦听 
  66. tcpListener.Start ( ) ; 
  67. //返回可以用以处理连接的Socket实例 
  68. socketForClient = tcpListener.AcceptSocket ( ) ; 
  69. try 
  70. //如果返回值是"true",则产生的套节字已经接受来自远方的连接请求 
  71. if ( socketForClient.Connected ) 
  72. ListBox1.Items.Add ( "已经和客户端成功连接!" ) ; 
  73. while ( true
  74. //创建networkStream对象通过网络套节字来接受和发送数据 
  75. networkStream = new NetworkStream ( socketForClient ) ; 
  76. //从当前数据流中读取一行字符,返回值是字符串 
  77. streamReader = new StreamReader ( networkStream ) ; 
  78. string msg = streamReader.ReadLine ( ) ; 
  79. ListBox1.Items.Add ( "收到客户端信息:" + msg ) ; 
  80. streamWriter = new StreamWriter ( networkStream ) ; 
  81. if ( textBox1.Text != "" ) 
  82. ListBox1.Items.Add ( "往客户端反馈信息:" +  
  83. textBox1.Text ) ; 
  84. //往当前的数据流中写入一行字符串 
  85. streamWriter.WriteLine ( textBox1.Text ) ; 
  86. //刷新当前数据流中的数据 
  87. //C#网络编程服务器端程序 
  88. streamWriter.Flush ( ) ; 
  89. catch ( Exception ey ) 
  90. MessageBox.Show ( ey.ToString ( ) ) ; 
  91. static void Main ( ) 
  92. Application.Run ( new Form1 ( ) ) ; 
  93. private void button1_Click ( object sender , 
  94. System.EventArgs e ) 
  95. ListBox1.Items .Add ( "服务已经启动!" ) ; 
  96. _thread1 = new Thread ( new ThreadStart ( Listen ) ) ; 
  97. _thread1.Start ( ) ; 
  98. private void button2_Click ( object sender , 
  99. System.EventArgs e ) 
  100. //C#网络编程服务器端程序 
  101. //关闭线程和流 
  102. networkStream.Close ( ) ; 
  103. streamReader.Close ( ) ; 
  104. streamWriter.Close ( ) ; 
  105. _thread1.Abort ( ) ; 
  106. tcpListener.Stop ( ) ; 
  107. socketForClient.Shutdown ( SocketShutdown.Both ) ; 
  108. socketForClient.Close ( ) ; 
  109. private void Form1_Closed ( object sender , 
  110. System.EventArgs e ) 
  111. //C#网络编程服务器端程序 
  112. //关闭线程和流 
  113. networkStream.Close ( ) ; 
  114. streamReader.Close ( ) ; 
  115. streamWriter.Close ( ) ; 
  116. _thread1.Abort ( ) ; 
  117. tcpListener.Stop ( ) ; 
  118. socketForClient.Shutdown ( SocketShutdown.Both ) ; 
  119. socketForClient.Close ( ) ; 
  120. }

    C#网络编程服务器端程序的实现源码就向你介绍到这里,希望对你了解和学习C#网络编程服务器端程序有所帮助。

原文地址:https://www.cnblogs.com/bennylam/p/1784282.html