打印本文 打印本文 关闭窗口 关闭窗口
C#网络编程客户端程序实现源码分享
作者:武汉SEO闵涛  文章来源:本站原创  点击数1096  更新时间:2011/11/20 12:46:58  文章录入:mintao  责任编辑:mintao

C#网络编程客户端程序实现是如何办到的呢?由于在客户端不需要侦听网络,所以在调用上面没有程序阻塞情况,所以在下面的代码中,我们没有使用到线程,这是和服务器端程序的一个区别的地方。总结上面的这些关键步骤,可以得到一个用C#网络编程客户端程序,具体如下:

以下是代码片段:
  1. //C#网络编程客户端程序  
  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#网络编程客户端程序  
  12. //导入程序中使用到的名字空间  
  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#网络编程客户端程序  
  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#网络编程客户端程序  
  57. //同样方法设置其他控件  
  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#的网络编程客户器端!" ;  
  69. this.Closed += new System.EventHandler ( this.Form1_Closed ) ;  
  70. this.ResumeLayout ( false ) ;  
  71. //连接到服务器端口,在这里是选用本地机器作为服务器,  
  72. //你可以通过修改IP地址来改变服务器  
  73. try 
  74. {  
  75. myclient = new TcpClient ( "localhost" , 1234 ) ;  
  76. }  
  77. catch 
  78. {  
  79. MessageBox.Show ( "没有连接到服务器!" ) ;  
  80. return ;  
  81. }  
  82. //C#网络编程客户端程序  
  83. //创建networkStream对象通过网络套节字来接受和发送数据  
  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 ( "请确定文本框为非空!" ) ;  
  98. textBox1.Focus ( ) ;  
  99. return ;  
  100. }  
  101. try 
  102. {  
  103. string s ;  
  104. //往当前的数据流中写入一行字符串  
  105. streamWriter.WriteLine ( textBox1.Text ) ;  
  106. //刷新当前数据流中的数据  
  107. streamWriter.Flush ( ) ;  
  108. //从当前数据流中读取一行字符,返回值是字符串  
  109. s = streamReader.ReadLine ( ) ;  
  110. ListBox1.Items.Add ( "读取服务器端发送内容:" + s ) ;  
  111. }  
  112. //C#网络编程客户端程序  
  113. catch ( Exception ee )  
  114. {  
  115. MessageBox.Show (   
  116. "从服务器端读取数据出现错误,类型为:" +   
  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#网络编程客户端程序的具体实现代码就向你介绍到这里,希望地你了解和开发C#网络编程客户端程序有所帮助。

打印本文 打印本文 关闭窗口 关闭窗口