/// <summary> /// /// </summary> /// <param name="i_oPara"></param> private void m_Process(object i_oPara) { Class_Para n_Class_Para = (Class_Para)i_oPara; //模拟事件处理 Thread.Sleep(5000); //发送结束字符 byte[] n_btData = { 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B }; //向客户端发送信息 SendMeaage(n_Class_Para.p_Socket_Send, n_Class_Para.p_EndPoint, n_btData); //SendMeaage_End(n_Class_Para.p_EndPoint, n_btData); m_iEndCounter++; Console.WriteLine("第{0}个请求处理完毕!", m_iEndCounter); Console.WriteLine("执行效率: " + ((m_iEndCounter) / (m_Stopwatch.ElapsedMilliseconds / 1000.0f)).ToString("F2") + "个/秒"); //(单位:秒) //回收 GC.Collect(); }
/// <summary> /// 异步接收回调函数 /// </summary> /// <param name="i_IAsyncResult"></param> public void EndReceiveFromCallback(IAsyncResult i_IAsyncResult) { m_iProcessCounter++; Console.WriteLine("第{0}个请求连接!", m_iProcessCounter); Class_State n_State = i_IAsyncResult.AsyncState as Class_State; Socket n_Socket_Receive = n_State.p_Socket_Receive; /*Socket n_Socket_Send = n_State.p_Socket_Send*/ try { //完成接收 int n_ibtReadLength = n_Socket_Receive.EndReceiveFrom(i_IAsyncResult, ref n_State.p_EndPoint); //显示客户端地址和端口 Console.WriteLine("客户端终结点:{0}", n_State.p_EndPoint.ToString()); //显示接收信息 Console.WriteLine("接收数据字节数:{0}", n_ibtReadLength); string n_sMessage = Encoding.Default.GetString(n_State.p_Buffer, 0, n_ibtReadLength); Console.WriteLine("来着客户端信息:{0}", n_sMessage); //n_Socket_Receive.Disconnect(true); Socket n_Socket_Send = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //得到客户机IP EndPoint n_EndPoint_Send = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); //EndPoint n_EndPoint_Send = (EndPoint)(new IPEndPoint(IPAddress.Parse("192.168.1.172"), 8003)); n_Socket_Send.Bind(n_EndPoint_Send); n_Socket_Send.ReceiveTimeout = 60000; n_Socket_Send.SendTimeout = 60000; n_Socket_Send.ReceiveBufferSize = 1024 * 10000; n_Socket_Send.SendBufferSize = 1024 * 10000; n_Socket_Send.Ttl = 255; //n_Socket_Send.NoDelay = true; n_Socket_Send.MulticastLoopback = true; //发送结束字符 byte[] n_btData = { 0x5A, 0x5A }; //向客户端发送信息 SendMeaage(n_Socket_Send, n_State.p_EndPoint, n_btData); Class_Para n_Class_Para = new Class_Para(); n_Class_Para.p_EndPoint = n_State.p_EndPoint; n_Class_Para.p_Socket_Send = n_Socket_Send;// n_State.p_Socket_Send; m_iStartCounter++; Console.WriteLine("第{0}个请求处理开始!", m_iStartCounter); //进入线程 Thread n_Thread_Run = new Thread(m_Process); n_Thread_Run.Start(n_Class_Para); //回收 GC.Collect(); //// //m_Process(n_Class_Para); ////方法2 线程池 //ThreadPool.QueueUserWorkItem(new WaitCallback(m_Process), n_Class_Para); } catch (Exception e) { Console.WriteLine("发生异常!异常信息:"); Console.WriteLine(e.Message); } finally { ////非常重要:继续异步接收 //socket.BeginReceiveFrom( // state.Buffer, 0, state.Buffer.Length, // SocketFlags.None, // ref state.RemoteEP, // EndReceiveFromCallback, // state); //事件状态终止 m_ManualResetEvent_ReceiveDone.Set(); } }