C#托管Socket的实现方法(2)

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

(1).微软公司视窗2000服务器版

(2).Visual Studio .Net正式版,.Net FrameWork SDK版本号3705

四.利用Socket来传送数据:

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

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

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

这些问题解决方法都可以在下面介绍代码中找到相对应的部分。由于下面的代码都有详细的注解,

这里就不详细介绍。下面是利用Socket传送数据的具体实现步骤:

1.启动Visual Studio .Net,并新建一个Visual C#项目,

项目名称为【利用Socket来发送数据】。

2.把Visual Studio .Net的当前窗口谢坏健綟orm1.cs(设计)】窗口,

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

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

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

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

三个Label组件。

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

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

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

private void InitializeComponent ( )
{
this.label1 = new System.Windows.Forms.Label ( ) ;
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.button1 = new System.Windows.Forms.Button ( ) ;
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.StatusBar ( ) ;
this.label3 = new System.Windows.Forms.Label ( ) ;
this.button2 = new System.Windows.Forms.Button ( ) ;
this.SuspendLayout ( ) ;
this.label1.Location = new System.Drawing.Point ( 24 , 20 ) ;
this.label1.Name = "label1" ;
this.label1.Size = new System.Drawing.Size ( 74 , 30 ) ;
this.label1.TabIndex = 0 ;
this.label1.Text = "IP地址:" ;
this.textBox1.BorderStyle = System.Windows.
Forms.BorderStyle.FixedSingle ;
this.textBox1.Location = new System.Drawing.Point ( 94 , 18 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 166 , 21 ) ;
this.textBox1.TabIndex = 1 ;
this.textBox1.Text = "" ;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button1.Location = new System.Drawing.Point ( 280 , 14 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 62 , 28 ) ;
this.button1.TabIndex = 2 ;
this.button1.Text = "连接" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label2.Location = new System.Drawing.Point ( 16 , 64 ) ;
this.label2.Name = "label2" ;
this.label2.TabIndex = 3 ;
this.label2.Text = "发送信息:" ;
this.textBox2.BorderStyle = System.Windows.
Forms.BorderStyle.FixedSingle ;
this.textBox2.Location = new System.Drawing.Point ( 94 , 58 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.Size = new System.Drawing.Size ( 166 , 21 ) ;
this.textBox2.TabIndex = 4 ;
this.textBox2.Text = "" ;
this.listBox1.ItemHeight = 12 ;
this.listBox1.Location = new System.Drawing.Point ( 20 , 118 ) ;
this.listBox1.Name = "listBox1" ;
this.listBox1.Size = new System.Drawing.Size ( 336 , 160 ) ;
this.listBox1.TabIndex = 6 ;
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.Location = new System.Drawing.Point ( 14 , 94 ) ;
this.label3.Name = "label3" ;
this.label3.Size = new System.Drawing.Size ( 128 , 23 ) ;
this.label3.TabIndex = 8 ;
this.label3.Text = "已经发送的信息:" ;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
this.button2.Location = new System.Drawing.Point ( 280 , 54 ) ;
this.button2.Name = "button2" ;
this.button2.Size = new System.Drawing.Size ( 62 , 28 ) ;
this.button2.TabIndex = 9 ;
this.button2.Text = "发送" ;
this.button2.Click += new System.EventHandler
( this.button2_Click ) ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 370 , 317 ) ;
this.Controls.AddRange ( new System.Windows.Forms.Control[] {
this.button2 ,
this.statusBar1 ,
this.listBox1 ,
this.textBox2 ,
this.label2 ,
this.button1 ,
this.textBox1 ,
this.label1 ,
this.label3} ) ;
this.FormBorderStyle = System.
Windows.Forms.FormBorderStyle.FixedSingle ;
this.MaximizeBox = false ;
this.Name = "Form1" ;
this.Text = "利用Socket来发送数据" ;
this.ResumeLayout ( false ) ;
}



至此【利用Sokcet来传送数据】项目设计后的界面就完成了

5.在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 ;
6.在Form1的class代码区中加入下列代码,下列代码的作用是定义全局变量和创建全局使用的实例:

int port = 8000 ;
//定义侦听端口号
private TcpClient tcpc ;
//对服务器端创建TCP连接
private Socket stSend ;
//创建发送数据套接字
private bool tcpConnect = false ;
//定义标识符,用以表示TCP连接是否建立



7.用下列代码替换Form1.cs中的button1组件的Click事件对应的处理代码,下列代码的功能是初始化以创建的Socket实例,并向远程终结点提出连接申请,并判断连接是否建立:

private void button1_Click
( object sender , System.EventArgs e )
{
//以下代码是判断是否和远程终结点成功连接
try
{
stSend = new Socket ( AddressFamily.InterNetwork ,
SocketType.Stream , ProtocolType.Tcp ) ;
//初始化一个Socket实例
IPEndPoint tempRemoteIP = new IPEndPoint
( IPAddress.Parse ( textBox1.Text ) , port ) ;
//根据IP地址和端口号创建远程终结点
EndPoint epTemp = ( EndPoint ) tempRemoteIP ;
stSend.Connect ( epTemp ) ;
//连接远程主机的8000端口号
statusBar1.Text = "成功连接远程计算机!" ;
tcpConnect = true ;
button1.Enabled = false ;
button2.Enabled = true ;
}
catch ( Exception )
{
statusBar1.Text = "目标计算机拒绝连接请求!" ;
}
}



8.用下列代码替换Form1.cs中button2组件的Click事件对应的处理代码,下列代码的功能是通过已建立的连接,利用Socket来传送数据到远程主机。

private void button2_Click
( object sender , System.EventArgs e )
{
int iLength = textBox2.Text.Length ;
//获取要发送的数据的长度
Byte [ ] bySend = new byte [ iLength ] ;
//根据获取的长度定义一个Byte类型数组
bySend = System.Text.Encoding.Default.GetBytes
( textBox2.Text ) ;
//按照指定编码类型把字符串指定到指定的Byte数组
int i = stSend.Send ( bySend ) ;
//发送数据
listBox1.Items.Add ( textBox2.Text ) ;
}



9.用下列代码替换Form1.cs中“清理所有正在使用的资源。”对应的代码。其作用是在程序退出之前,判断连接状态,如果没有退出,则向远程主机发送控制码“STOP”,用以断开和远程主机的连接,并清除相应资源。所谓控制码就是网络应用程序之间彼此交换信息的一种自定义码子,应用程序通过接收、发送这些码子,可以明确网络应用程序的行为,保证执行的一致性,也就少了很多出错的几率。控制码在编写远程控制方面的应用程序时使用比较多。之所以要有这一步是因为在用Visual C#编写网络应用程序的时候,很多人都遇到这样的情况。当程序退出后,通过Windows的“资源管理器”看到的是进程数目并没有减少。这是因为程序中使用的线程可能并没有有效退出。虽然Thread类中提供了“Abort”方法用以中止进程,但并不能够保证成功退出。因为进程中使用的某些资源并没有回收。可见在某些情况下,依靠Visual C#的垃圾回收器也不能保证完全的回收资源,这时就需要我们自己手动回收资源的。下面就是手动回收资源采用的一种方法:

protected override void Dispose ( bool disposing )
{
if ( tcpConnect )
{
Byte [ ] bySend = new byte [ 4 ] ;
//根据字符串“STOP”长度来定义Byte数组
bySend = System.Text.Encoding.
Default.GetBytes ( "STOP" ) ;
int i = stSend.Send ( bySend ) ;
//发送控制码
stSend.Close ( ) ;
//关闭套接字
}
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值