转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> C语言系列 >> 正文
C#网络编程客户端程序实现源码分享         ★★★

C#网络编程客户端程序实现源码分享

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1092 更新时间:2011/11/20 12:46:58

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#网络编程客户端程序有所帮助。


[C语言系列]C# 过滤html,js,css代码 正则表达式  [C语言系列]C# DataGridView显示行号的两种方法
[C语言系列]C# WinForm 中Label自动换行 解决方法  [C语言系列]C# 线程调用主线程中的控件
[电脑应用]c# winform 打包部署 自定义界面 或设置开机启动  [C语言系列]C# 和 Linux 时间戳转换
[C语言系列]C#实现 WebBrowser中新窗口打开链接用默认或者指定…  [C语言系列]C#全角和半角转换
[C语言系列]c#WebBrowser查找并选择文本  [C语言系列]C#中实现WebBrowser控件的HTML源代码读写
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台