庞大资源库的计算机教程网站!
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
首页 操作系统 程序设计 图形图像 媒体动画 机械电子 WEB开发 数 据 库 办公系列 路由技术 网络原理 网络应用
认证考试 安全技术
首页>程序设计>C#语言>网络编程>正文
资料搜索
Google搜索
Google
返回上级列表

推荐文章

快速保存网页中所有图片的方法
Windows中让光驱巧妙“隐身”技
防范非法用户入侵Win 2000/XP系
两款比较典型的ASP木马防范方法
有关表格边框的css语法整理
Windows XP中可以被禁用的服务
SQL Server导出导入数据方法
Javascript所有对象的属性的获
网页(HTML)中的特殊字符
与篮球共舞,尽显模式本色
QQ病毒的手工清除方法
Photoshop为极品美女打造性感睫
天衣无缝:IIS与PHP水火也相容
SQL Server存储过程编写和优化

Visual C#中托管Socket的实现方法(二) (1)

 作者:本站收集   日期:2005-8-8 9:58:55
字号选择〖 〗/ 双击滚屏 单击停止   
五.利用Socket来接收数据

Visual C#在使用Socket来介绍网络传送来的数据时,要解决下面三个问题,也是完成Visual C#使用Socket来接收数据的三个步骤:

1.侦听网络,接受网络连接申请;

2..获得用以接收数据的Socket实例,并以此实例接收远程主机发送来的数据

3.根据远程主机发送来的控制码,断开网络连接,并清除资源。

此处接收接收数据,是上面介绍的【利用Socket来传送数据】传送来的数据

下面就是利用Socket来接收数据的具体实现步骤:

1.启动Viisual Studio .Net,并新建一个Visual C#项目,项目名称为【利用Socket来接收数据】。

2.把Visual Studio .Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往Form1窗体中拖入下列组件,并执行相应操作:

一个ListBox组件,用以显示接收的数据

一个StausBar组件,用以显示接收端程序的运行状况。

一个Button组件,名称为button1,并在这个组件被拖入窗体后,双击它,则系统会在Form1.cs文件中自动产生其Click事件对应的处理代码。

3.【解决方案资源管理器】窗口中,双击Form1.cs文件,进入Form1.cs文件的编辑界面。

4.以下面代码替代系统产生的InitializeComponent过程::

private void InitializeComponent ( )
{
	this.button1 = new System.Windows.Forms.Button ( ) ;
	this.listBox1 = new System.Windows.Forms.ListBox ( ) ;
	this.statusBar1 = new System.Windows.Forms.StatusBar ( ) ;
	this.SuspendLayout ( ) ;
	this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
	this.button1.Location = new System.Drawing.Point ( 96 , 16 ) ;
	this.button1.Name = "button1" ;
	this.button1.Size = new System.Drawing.Size ( 80 , 34 ) ;
	this.button1.TabIndex = 0 ;
	this.button1.Text = "监听" ;
	this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
	this.listBox1.ItemHeight = 12 ;
	this.listBox1.Location = new System.Drawing.Point ( 16 , 68 ) ;
	this.listBox1.Name = "listBox1" ;
	this.listBox1.Size = new System.Drawing.Size ( 258 , 172 ) ;
	this.listBox1.TabIndex = 1 ;
	this.statusBar1.Location = new System.Drawing.Point ( 0 , 251 ) ;
	this.statusBar1.Name = "statusBar1" ;
	this.statusBar1.Size = new System.Drawing.Size ( 292 , 22 ) ;
	this.statusBar1.TabIndex = 2 ;
	this.statusBar1.Text = "无连接" ;
	this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
	this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
	this.Controls.AddRange ( new System.Windows.Forms.Control[] {
		  this.statusBar1 ,
		  this.listBox1 ,
		  this.button1} ) ;
	this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle ;
	this.MaximizeBox = false ;
	this.Name = "Form1" ;
	this.Text = "利用Socket来接收数据" ;
	this.ResumeLayout ( false ) ;
}


至此【利用Socket来接收数据】项目设计后的界面就完成了,具体如图02所示:



图02:【利用Socket来接收数据】项目的设计界面


5.把Visual Studio .Net的当前窗口切换到Form1.cs的代码编辑窗口,并在Form1.cs文件的开头,用下列导入命名空间代码替代系统缺省的导入命名空间代码。

using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Net.Sockets  ;
//使用到TcpListen类
using System.Net ;
using System.Threading  ;
//使用到线程


6.在Form1.cs中的class代码区中添加下列代码,下列代码的作用是定义全局变量和创建全局使用的实例:

int port = 8000 ;
//定义侦听端口号
private Thread thThreadRead ;
//创建线程,用以侦听端口号,接收信息
private TcpListener	tlTcpListen ;
//侦听端口号
private bool blistener = true ;
//设定标示位,判断侦听状态
private Socket stRead ;
上一篇:VisualC#中托管Socket的实现方法(二) (2)    下一篇:Visual C#托管Socket的实现方法(一) (3)  
[发送给好友]  [关闭窗口]  [返回顶部]   转载请注明来源:www.it00.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 原点 投稿作者: 本站收集
信息来源: 网络 录入时间: 2005-8-8 9:58:55
关于我们 - 广告服务 - 版权申明 - 网站地图 - 联系方式 - 总编信箱 - 会员投稿