百度空间 | 百度首页 
 
查看文章
 
C#.net同步异步SOCKET通讯和多线程总结(6)
2007年10月15日 星期一 15:55
return;

                        }

                        string sTime = DateTime.Now.ToShortTimeString ( ) ; //获取接收数据时的时间

                        listBox1.Items.Add ( sTime + " " + sMessage ) ;

                    }

               }

               catch ( System.Security.SecurityException )

               {

                    MessageBox.Show ( "侦听失败!" , "错误" ) ;

               }

          }

          //开始监听

          private void button1_Click(object sender, System.EventArgs e)

          {

               thThreadRead = new Thread ( new ThreadStart ( Listen ) );

               thThreadRead.Start();//启动线程           

               button1.Enabled=false;

          }

          // 清理所有正在使用的资源。

          protected override void Dispose( bool disposing )

          {

               try

               {

                    tlTcpListen.Stop(); //关闭侦听

                    nsStream.Close();

                    srRead.Close();//释放资源

                    thThreadRead.Abort();//中止线程

               }

               catch{}

               if( disposing )

               {

                    if (components != null)

                    {

                        components.Dispose();

                    }

              }

               base.Dispose( disposing );

          }

TCP协议的发送端

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

using System.Threading; //使用到线程

using System.IO; //使用到StreamWriter类

using System.Net; //使用IPAddress类、IPHostEntry类等

           private StreamWriter swWriter; //用以向网络基础数据流传送数据 

          private NetworkStream nsStream; //创建发送数据的网络基础数据流 

          private TcpClient tcpClient;

          private System.Windows.Forms.Button button1;

          private System.Windows.Forms.TextBox textBox1;

          private System.Windows.Forms.Button button2;

          private System.Windows.Forms.TextBox textBox2;

          private System.Windows.Forms.StatusBar statusBar1;

          private System.


类别:一些教程 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu