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 ;

 

 

 

Posted on 2010-12-20 10:17  Steven Sun  阅读(207)  评论(0编辑  收藏  举报