• <转载>C#.net同步异步SOCKET通讯和多线程总结2


    服务端:
     
    using System.Net;
     
    using System.Net.Sockets;
     
    using System.Text;
     
    using System.Threading;
     
    Thread mythread ;
     
    Socket socket;
     
    // 清理所有正在使用的资源。
     
            protected override void Dispose( bool disposing )
     
             {
     
                  try
     
                 {
     
                socket.Close();//释放资源
     
                mythread.Abort ( ) ;//中止线程
     
                 }
     
                 catch{ }
     
                 if( disposing )
     
                  {
     
                       if (components != null)
     
                       {
     
                           components.Dispose();
     
                       }
     
                  }
     
                  base.Dispose( disposing );
     
             }      
     
             public static IPAddress GetServerIP()
     
             {
     
                  IPHostEntry ieh=Dns.GetHostByName(Dns.GetHostName());
     
                  return ieh.AddressList[0];
     
             }
     
             private void BeginListen()
     
             {
     
                  IPAddress ServerIp=GetServerIP();
     
                  IPEndPoint iep=new IPEndPoint(ServerIp,8000);
     
                  socket=new
     
                           Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
     
                  byte[] byteMessage=new byte[100];

                  this.label1.Text=iep.ToString();
     
                  socket.Bind(iep);

    //            do
     
                  while(true)
     
                  {
     
                       try
     
                       {
     
                           socket.Listen(5);
     
                           Socket newSocket=socket.Accept();
     
                           newSocket.Receive(byteMessage);
     
                           string sTime = DateTime.Now.ToShortTimeString ( ) ;
     
    string msg=sTime+":"+"Message from:";
     
    msg+=newSocket.RemoteEndPoint.ToString()+Encoding.Default.GetString(byteMessage);
     
                           this.listBox1.Items.Add(msg);
     
                       }
     
                       catch(SocketException ex)
     
                       {
     
                           this.label1.Text+=ex.ToString();
     
                       }
     
                  }
     
    //            while(byteMessage!=null);
     
             }
     
             //开始监听
     
             private void button1_Click(object sender, System.EventArgs e)
     
             {
     
                  try
     
                  {
     
                       mythread = new Thread(new ThreadStart(BeginListen));
     
                       mythread.Start();
     
                  }
     
                  catch(System.Exception er)
     
                  {
     
                       MessageBox.Show(er.Message,"完成",MessageBoxButtons.OK,MessageBoxIcon.Stop);
     
                  }
     
             }
     
    客户端:
     
    using System.Net;
     
    using System.Net.Sockets;
     
    using System.Text;
     
             private void button1_Click(object sender, System.EventArgs e)
     
             {
     
                  BeginSend();     
     
             }
     
             private void BeginSend()
     
             {           
     
                  string ip=this.txtip.Text;
     
                  string port=this.txtport.Text;
     
                  IPAddress serverIp=IPAddress.Parse(ip);          
     
                  int serverPort=Convert.ToInt32(port);
     
                  IPEndPoint iep=new IPEndPoint(serverIp,serverPort);

                  byte[] byteMessage;

    //            do
     
    //            {
     
                       Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
     
                       socket.Connect(iep);
     
                       byteMessage=Encoding.ASCII.GetBytes(textBox1.Text);
     
                       socket.Send(byteMessage);
     
                       socket.Shutdown(SocketShutdown.Both);
     
                       socket.Close();
     
    //            }
     
    //            while(byteMessage!=null);
     
             }
     
    基于TCP协议的发送和接收端
     
    TCP协议的接收端
     
    using System.Net.Sockets ; //使用到TcpListen类
     
    using System.Threading ; //使用到线程

    using System.IO ; //使用到StreamReader类
     
             int port = 8000; //定义侦听端口号
     
             private Thread thThreadRead; //创建线程,用以侦听端口号,接收信息
     
             private TcpListener tlTcpListen; //侦听端口号
     
             private bool blistener = true; //设定标示位,判断侦听状态
     
             private NetworkStream nsStream; //创建接收的基本数据流

             private StreamReader srRead;
     
             private System.Windows.Forms.StatusBar statusBar1;
     
             private System.Windows.Forms.Button button1;
     
             private System.Windows.Forms.ListBox listBox1; //从网络基础数据流中读取数据
     
             private TcpClient tcClient ;
     
              private void Listen ( )
     
             {
     
                  try
     
                  {
     
                       tlTcpListen = new TcpListener ( port ) ; //以8000端口号来初始化TcpListener实例
     
                       tlTcpListen.Start ( ) ; //开始监听
     
                       statusBar1.Text = "正在监听..." ;
     
                       tcClient = tlTcpListen.AcceptTcpClient ( ) ; //通过TCP连接请求
     
                       nsStream = tcClient.GetStream ( ) ; //获取用以发送、接收数据的网络基础数据流
     
                       srRead=new StreamReader(nsStream);//以得到的网络基础数据流来初始化StreamReader实例

                       statusBar1.Text = "已经连接!";

                        while( blistener ) //循环侦听
     
                       {
     
                           string sMessage = srRead.ReadLine();//从网络基础数据流中读取一行数据
     
                           if ( sMessage == "STOP" ) //判断是否为断开TCP连接控制码
     
                           {
     
                                tlTcpListen.Stop(); //关闭侦听
     
                                nsStream.Close(); //释放资源
     
                                srRead.Close();
     
                                statusBar1.Text = "连接已经关闭!" ;
     
                                thThreadRead.Abort(); //中止线程
     
                                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.Windows.Forms.Label label1;
     
             private System.Windows.Forms.Label label2; //通过它实现向远程主机提出TCP连接申请 
     
             private bool tcpConnect = false; //定义标识符,用以表示TCP连接是否建立
     
              //连接 
     
             private void button1_Click(object sender, System.EventArgs e)
     
             {
     
                  IPAddress ipRemote ;
     
                  try
     
                  {
     
                       ipRemote = IPAddress.Parse ( textBox1.Text ) ;
     
                  }
     
                  catch //判断给定的IP地址的合法性
     
                  {
     
                       MessageBox.Show ( "输入的IP地址不合法!" , "错误提示!" ) ;
     
                       return ;
     
                  }
     
                  IPHostEntry ipHost ;
     
                  try
     
                  {
     
                       ipHost = Dns.Resolve ( textBox1.Text ) ; 
     
                  }
     
                  catch //判断IP地址对应主机是否在线
     
                  {

                       MessageBox.Show ("远程主机不在线!" , "错误提示!" ) ;
     
                       return ;
     
                  }
     
                  string sHostName = ipHost.HostName ;
     
                  try
     
                  {
     
                       TcpClient tcpClient = new TcpClient(sHostName,8000);//对远程主机的8000端口提出TCP连接申请
     
                       nsStream = tcpClient.GetStream();//通过申请,并获取传送数据的网络基础数据流  
     
                       swWriter = new StreamWriter(nsStream);//使用获取的网络基础数据流来初始化StreamWriter实例
     
                       button1.Enabled = false ;
     
                       button2.Enabled = true ;
     
                       tcpConnect = true ;
     
                       statusBar1.Text = "已经连接!" ;
     
                  }
     
                  catch
     
                  {
     
                       MessageBox.Show ( "无法和远程主机8000端口建立连接!" , "错误提示!" ) ;
     
                       return ;
     
                  }
     
             }
     
              //发送
     
             private void button2_Click(object sender, System.EventArgs e)
     
             {
     
                  if (textBox2.Text !="")
     
                  {
     
                       swWriter.WriteLine(textBox2.Text);//刷新当前数据流中的数据
     
                       swWriter.Flush();
     
                  }
     
                  else
     
                  {
     
                       MessageBox.Show("发送信息不能为空!","错误提示!");
     
                  }
     
             }
     
             // 清理所有正在使用的资源。
     
             protected override void Dispose( bool disposing )
     
             {
     
                  if ( tcpConnect )
     
                  {
     
                       swWriter.WriteLine ( "STOP" ) ; //发送控制码  
     
                       swWriter.Flush (); //刷新当前数据流中的数据  
     
                       nsStream.Close (); //清除资源
     
                       swWriter.Close ();
     
                  }
     
                  if( disposing )
     
                  {
     
                       if (components != null)
     
                       {
     
                           components.Dispose();
     
                       }
     
                  }
     
                  base.Dispose( disposing );
     
             }
     
    异步套接字
     
    BeginAccept
     
    Public IAsyncResult BeginAccept{AsyncCallback callback,object state}
     
    AsyncCallback异步回调方法 object state自定义对象, 返回IasyncResult
     
    Using System;
     
    Namespace mySocket
     
    {
     
           Public class Stateobject
     
    {
     
           Public StateObject(){构造函数逻辑}
     
    }
     
    }à>
     
    Using System;
     
    Using System.Net;
     
    Using System.Net.Sockets;
     
    Using System.Threading;
     
    Using System.Text;
     
    Namespace mysocket
     
    {
     
           Public Class StateObject
     
    {
     
           Public Socket worksocket=null;
     
           Public const int buffersize=1024;
     
           Public byte[] buffer=new byte[buffersize];
     
           Public StringBuilder sb=new StringBuilder();
     
           Public StateObject()
     
           {}
     
    }
     
    }
     
    实现主机绑定和端口监听:
     
    Private IPAddress myIP=IPAddress.Parse(“127.0.0.1”);
     
    Private IPEndPoint MyServer;
     
    Private Socket mySocket;
     
    Private Socket Handler;
     
    Private Static ManualResetEvent myreset =new ManualResetEvent(false);
     
    Try
     
    {
     
           IPHostEntry myhost=new IPHostEntry();
     
           Myhost=dns.gethostbyName(“”);
     
           String IPString =myhost.Addresslist[0].tostring();
     
           Myip=IPAddress.Parse(IPString);
     
    }
     
    Catch{MessageBox.Show(“您输入的IP地址格式不正确,重新输入!”);}
     
    Try
     
    {
     
           MyServer=new IPEndPoint(myIP,Int32.Parse(“Port”));
     
           Mysocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,Protocol.Tcp);
     
           Mysocket.Bind(Myserver);
     
           Mysocket.Listen(50);
     
           Thread thread=new Thread(new ThreadStart(target));
     
           Thread.Start();
     
    }
     
    Catch(Exception ee){}
     
    线程target
     
    Private void target()
     
    {
     
           While(true)
     
    {
     
           myReset.Reset();
     
           mysocket.BeginAccept(new AsyncCallBack(AcceptCallback),mysocket);
     
           myReset.WaitOne();
     
    }
     
    }
     
    异步回调方法AcceptCallBack
     
    Private void AcceptCallback(IAsyncResault ar)
     
    {
     
           myReset.Set();
     
           Socket Listener=(Socket)ar.AsyncState;
     
           Handler=Listener.EndAccept(ar);
     
           StateObject state=new StateObject();
     
           State.workSocket=handler;
     
           Try
     
           {
     
           Byte[] byteData=System.Text.Encoding.BigEndianUnicode.GetBytes(“通话!”+”\n\r”);
     
        Handler.BeginSend(byuteData,0,byteData.Length,0,new AsyncCallback(SendCallback),handler);
     
    }
     
    Catch(Exception ee)
     
    {MessageBox.Show(ee.Message);}
     
    Thread thread=new Thread(new ThreadStart(begreceive));
     
    Thread.Start();
     
    }

    多线程:
     
    每个窗体自己都在不同的线程上面运行,如果需要在窗体之间交互,需要在线程之间交互
     
    当线程sleep,系统就使之退出执行队列,当睡眠结束,系统产生时钟中断,使该线程回到执行队列中,回复线程的执行。
     
    如果父线程先于子线程结束,那么子线程在父线程结束的时候被迫结束,Thread.Join()是父线程等待子线程结束。Abort带来的是不可回复的终止线程
     
    起始线程为主线程,前台线程全部结束,则主线程可以终止,后台线程无条件终止。
     
    前台线程不妨碍程序终止,一旦进程的所有前台线程终止,则clr调用任意一个还存活的后台线程的abort来彻底终止进程。
     
    挂起,睡眠(阻塞,暂停)
     
    Thread.Suspend不会使线程立即停止执行,直到线程到达安全点的时候它才可以将该线程挂起,如果线程尚未运行或这已经停止,则不能被挂起,调用thread.resume使另一个线程跳出挂起状态,继续执行。
     
    一个线程不能对另一个线程调用sleep,但是可以suspend。
     
    Lock可以把一段代码定义为互斥段critical section 互斥段在一个时刻内只允许一个线程进入执行,其他线程必须等待
     
    多线程公用对象,不应该使用lock,monitor提供了线程共享资源的方案,monitor可以锁定一个对象,线程只有得到锁才可以对该对象进行操作
     
    一个进程开始至少有一个主线程。系统加载程序时创建主执行线程
     
    消息队列与线程相关
     
    一开始创建线程就产生消息队列了,一个线程可以创建多个窗体,而发给这些窗体的消息都同意发送到同一个消息队列中了,消息结构中有msg.hwnd指出该条消息与哪个窗体相关
     
    DispatchMessage()函数依照这个保证消息分派处理自动化而且不会出错。
     
    线程控制方法:
     
    Start线程开始运行
     
    Sleep 是线程暂停一段指定时间
     
    Suspend 线程在到达安全点后暂停
     
    Abort 线程到达安全点后停止
     
    Resume 重新启动挂起的线程
     
    Join 当前线程等待其他线程运行结束。如果使用超时值,且线程在分配的时间内结束,方法返回true
     
    安全点:代码中的某些位置,这些位置公共语言运行时可以安全的执行自动垃圾回收,即释放未使用的变量并回收内存,调用线程的abort和suspend方法时,公共语言运行时将分析代码并确定线程停止运行的适当位置。

  • 相关阅读:
    梯度下降(Gradient Descent)
    Positioning-Based Photo Retrieval
    NTU Long-Term Positioning Dataset
    Pycharm导入tabula模块包
    MySQL学习笔记20
    MySQL学习笔记17-19
    Android studio 报错AAPT2 error
    MySQL学习笔记13-16
    MySQL学习笔记 8-12
    机器学习十大算法
  • 原文地址:https://www.cnblogs.com/ChangTan/p/2050392.html
Copyright © 2020-2023  润新知