域名频道-专业提供域名注册,网站空间,电子邮箱,VPS主机等服务
首页 域名注册 虚拟主机 香港主机 美国主机 VPS主机 网站建设 ShopEX网店 软件开发 客户中心 购物车
域名服务,域名注册 常见问题
文章搜索


本类TOP10
·松江网页设计_松江网页制...
·上海轨道交通图-上海地图
·松江公交线路图-松江地图
·marquee连续滚动
·今天几度?-天气预报-全...
·松江新城地图-松江地图
·松江城区地图-松江地图
·HAO123邮箱登陆代码
·shopex免费模板,下...
·松江DNS服务器地址,松...
当前位置:域名频道首页 > 常见问题 > 网页制作
Visual C#托管Socket的实现方法(一) (2)

三.本文介绍程序的设计、调试、运行的软件环境

(1).微软公司视窗2000服务器版 < br>
(2).Visual Studio .Ne t正式版,.Net FrameWork SDK版本号3705

四.利用Socket来传送数据:

Visual C#在使用Socket传送数据时要注意 下列问题的解决方法:

1.创建Socket实 例,使用此实例创建和远程终结点的连接,并判断连接是否成功建立 。

2.发送数据到Socket,实现数据传送 。

这些问题解决方法都可以在下面介绍代码中找 到相对应的部分。由于下面的代码都有详细的注解,
这里就不详细介绍。下面是利用Socket传送数据的具体实 现步骤:

1.启动Visual Studio .Net,并新建一个Visual C#项目,
项目名称为【利用Socket来发送数据】。
2.把Visual Studio .Net的当前窗口切换 到【Form1.cs(设计)】窗口,

并从【 工具箱】中的【Windows窗体组件】选项卡中往Form1窗 体中拖入下列组件,并执行相应操作:

二个Te xtBox组件,一个用以输入远程主机的IP地址,一个用以输入 往远程主机传送的数据。

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

一个List Box组件,用以显示程序已传送的数据信息。

三个Label组件。

二个Button组件, 名称分别为button1、button2,并在这二个组件被拖 入窗体后,分别双击它们,则系统会在Form1.cs文件中自动 产生这二个组件的Click事件对应的处理代码。
3.【解决方案资源管理器】窗口中,双击Form1.cs文 件,进入Form1.cs文件的编辑界面。

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

 

 

private void Initiali zeComponent ( )
{
this. label1 = new System.Windows.Fo rms.Label ( ) ;

this.textB ox1 = new System.Windows.Forms .TextBox ( ) ;

this.button 1 = new System.Windows.Forms.B utton ( ) ;

this.label2 = new System.Windows.Forms.Label ( ) ;

this.textBox2 = new System.Windows.Forms.TextBox ( ) ;

this.listBox1 = new System.Windows.Forms.ListBox ( ) ;

this.statusBar1 = new System.Windows.Forms.StatusBa r ( ) ;

this.label3 = new System.Windows.Forms.Label ( ) ;

this.button2 = new Syst em.Windows.Forms.Button ( ) ;
< br> this.SuspendLayout ( ) ;
this.label1.Location = new System.Drawing.Point ( 24 , 20 ) ;

this.label1.Name = "l abel1" ;

this.label1.Size = new System.Drawing.Size ( 74 , 30 ) ;

this.label1.TabI ndex = 0 ;

this.label1.Tex t = "IP地址:" ;

this.textBox 1.BorderStyle = System.Windows .
Forms.BorderStyl e.FixedSingle ;

this.textB ox1.Location = new System.Draw ing.Point ( 94 , 18 ) ;

th is.textBox1.Name = "textBox1" ;

this.textBox1.Size = new System.Drawing.Size ( 166 , 2 1 ) ;

this.textBox1.TabInd ex = 1 ;

this.textBox1.Tex t = "" ;

this.button1.Flat Style = System.Windows.Forms.F latStyle.Flat ;

this.butto n1.Location = new System.Drawi ng.Point ( 280 , 14 ) ;

th is.button1.Name = "button1" ;
< br> this.button1.Size = new Sy stem.Drawing.Size ( 62 , 28 ) ;

this.button1.TabIndex = 2 ;

this.button1.Text = "连 接" ;

this.button1.Click += new System.EventHandler ( thi s.button1_Click ) ;

this.l abel2.Location = new System.Dr awing.Point ( 16 , 64 ) ;

this.label2.Name = "label2" ;
< br> this.label2.TabIndex = 3 ;

this.label2.Text = "发送信息: " ;

this.textBox2.BorderSt yle = System.Windows.
Forms.BorderStyle.FixedSin gle ;

this.textBox2.Locati on = new System.Drawing.Point ( 94 , 58 ) ;

this.textBox 2.Name = "textBox2" ;

this .textBox2.Size = new System.Dr awing.Size ( 166 , 21 ) ;

this.textBox2.TabIndex = 4 ;
this.textBox2.Text = "" ;
this.listBox1.ItemHeight = 12 ;

this.listBox1.Locatio n = new System.Drawing.Point ( 20 , 118 ) ;

this.listBox 1.Name = "listBox1" ;

this .listBox1.Size = new System.Dr awing.Size ( 336 , 160 ) ;

this.listBox1.TabIndex = 6 ;
< br> this.statusBar1.Location = new System.Drawing.Point ( 0 , 295 ) ;

this.statusBar1. Name = "statusBar1" ;

this .statusBar1.Size = new System. Drawing.Size ( 370 , 22 ) ;

this.statusBar1.TabIndex = 7 ;

this.statusBar1.Text = "无连接" ;

this.label3.Locati on = new System.Drawing.Point ( 14 , 94 ) ;

this.label3. Name = "label3" ;

this.lab el3.Size = new System.Drawing. Size ( 128 , 23 ) ;

this.l abel3.TabIndex = 8 ;

this. label3.Text = "已经发送的信息:" ;

this.button2.FlatStyle = Syst em.Windows.Forms.FlatStyle.Fla t ;

this.button2.Location = new System.Drawing.Point ( 2 80 , 54 ) ;

this.button2.N ame = "button2" ;

this.but ton2.Size = new System.Drawing .Size ( 62 , 28 ) ;

this.b utton2.TabIndex = 9 ;

this .button2.Text = "发送" ;

thi s.button2.Click += new System. EventHandler
( thi s.button2_Click ) ;

this.A utoScaleBaseSize = new System. Drawing.Size ( 6 , 14 ) ;

this.ClientSize = new System.D rawing.Size ( 370 , 317 ) ;

this.Controls.AddRange ( new System.Windows.Forms.Control[ ] {
this.button2 ,
this.statusBar1 ,
th is.listBox1 ,
this.text Box2 ,
this.label2 ,
this.button1 ,
thi s.textBox1 ,
this.label 1 ,
this.label3} ) ;

this.FormBorderStyle = Syste m.
Windows.Forms.F ormBorderStyle.FixedSingle ;
this.MaximizeBox = false ;
< br> this.Name = "Form1" ;

this.Text = "利用Socket来发送数据" ;
< br> this.ResumeLayout ( false ) ;

}

 

至此【利 用Sokcet来传送数据】项目设计后的界面就完成了,具体如图 01所示:

 


图01:【利用Sokcet来传送数 据】项目的设计界面

 

5.在Form1. cs文件的开头的导入命名空间的代码区,添加下列代码,

下列代码是导入下面程序中使用到的类所在的命名空间: < br>

 


u sing System ;

using System. Drawing ;

using System.Coll ections ;

using System.Comp onentModel ;

using System.W indows.Forms ;

using System .Data ;

using System.Net.So ckets ;

//使用到TcpListen类
using System.Net ;

来自:域名频道 时间:2006-9-19 返回 常见问题 首页
关于我们 联系方式 付款事宜 招聘启事 网站地图 域名注册 虚拟主机 法律顾问

Copyright 2000-2013 域名频道(www.DNS110.com)
地址:上海市松江区新松江路1188弄37号 邮编:201620
电话:021-67820741 67820742 67820743 传真:转分机805 值班电话:021-67820743
QQ:219854 Email:support@dns110.com