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

开发 后端
C#网络编程服务器端程序实现源码向你介绍了用结构是异步阻塞方式下的程序实现,希望对你了解和学习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. {  
  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#网络编程服务器端程序  
  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#网络编程服务器端程序  
  59. //同样的方式设置其他控件,这里略去  
  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#的网络编程服务器端!" ;  
  69. this.Closed += new System.EventHandler ( this.Form1_Closed ) ;  
  70. this.ResumeLayout ( false ) ;  
  71. }  
  72. private void Listen ( )  
  73. {  
  74. //C#网络编程服务器端程序  
  75. //创建一个tcpListener对象,此对象主要是对给定端口进行侦听  
  76. tcpListener = new TcpListener ( 1234 ) ;  
  77. //开始侦听  
  78. tcpListener.Start ( ) ;  
  79. //返回可以用以处理连接的Socket实例  
  80. socketForClient = tcpListener.AcceptSocket ( ) ;  
  81. try 
  82. {  
  83. //如果返回值是"true",则产生的套节字已经接受来自远方的连接请求  
  84. if ( socketForClient.Connected )  
  85. {  
  86. ListBox1.Items.Add ( "已经和客户端成功连接!" ) ;  
  87. while ( true )  
  88. {  
  89. //创建networkStream对象通过网络套节字来接受和发送数据  
  90. networkStream = new NetworkStream ( socketForClient ) ;  
  91. //从当前数据流中读取一行字符,返回值是字符串  
  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. //往当前的数据流中写入一行字符串  
  101. streamWriter.WriteLine ( textBox1.Text ) ;  
  102. //刷新当前数据流中的数据  
  103. //C#网络编程服务器端程序  
  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 ( "服务已经启动!" ) ;  
  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#网络编程服务器端程序  
  129. //关闭线程和流  
  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#网络编程服务器端程序  
  142. //关闭线程和流  
  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#网络编程服务器端程序的实现源码就向你介绍到这里,希望对你了解和学习C#网络编程服务器端程序有所帮助。

【编辑推荐】

  1. C#客户端程序实现同步传输字符串浅析
  2. ASP.NET异步回调浅析
  3. ASP.NET异步回调开发实例浅析
  4. C#网络编程入门基础知识浅析
  5. C#网络编程服务器端程序设计浅析
责任编辑:仲衡 来源: 天极网
相关推荐

2009-08-21 17:33:34

服务器端程序C#网络编程

2009-08-21 17:53:25

C#网络编程客户端程序

2009-08-21 14:34:34

C#服务器端表达式

2015-10-27 09:40:31

TCPIP网络协议

2009-08-21 17:48:43

C#网络编程

2010-10-15 08:57:15

PHP多进程

2012-05-07 13:55:41

JavaJava Web

2009-08-21 16:27:44

C#服务端程序

2009-08-21 15:36:41

服务端与客户端

2010-08-30 13:58:43

服务器端数据库

2011-07-26 11:07:08

JavaScript

2014-01-15 10:06:30

vFlash

2020-06-02 14:57:06

Linux服务器架构

2012-10-15 13:40:15

IBMdw

2009-08-21 15:22:56

端口侦听

2013-04-09 12:18:45

socket.ioC服务器

2009-08-14 11:00:16

C#创建Windows

2009-08-28 16:03:15

C#程序实现鼠标移动

2009-08-21 14:03:04

C#网络编程

2013-12-25 11:01:16

JavaScript
点赞
收藏

51CTO技术栈公众号