当前位置:编程学习 > C#/ASP.NET >>

急!Udp异步接收问题!

 private void Receive() 
        { 
            while(true) 
            { 
                    //Byte[] _received = new Byte[Constants.MAX_COMMAND_LEN]; 
                    EndPoint _remoteEP = new IPEndPoint(m_LocalEndPoint.Address, m_LocalEndPoint.Port); 

                    allDone.Reset(); 
                     StateObject state = new StateObject(); 
                    state.workSocket = m_Socket; 

                    m_Socket.BeginReceiveFrom(state.buffer, 0, StateObject.BufferSize,0, ref _remoteEP, 
                    new AsyncCallback(ReceiveCallback), state); 

                     allDone.WaitOne(); 

            if((m_State != Constants.UDP_STATE_IDLE) && (m_State != Constants.UDP_STATE_CLOSING)) 
            { 
                StopListen(); 
            } 

            m_State = Constants.UDP_STATE_IDLE; 
        } 


private  void ReceiveCallback( IAsyncResult ar )  

try  

     StateObject state = (StateObject) ar.AsyncState; 
     Socket client = state.workSocket; 

     EndPoint _remoteEP = new IPEndPoint(m_LocalEndPoint.Address, m_LocalEndPoint.Port); 
     allDone.Set(); 
     int bytesRead = client.EndReceiveFrom(ar,ref _remoteEP); 

     string _strReceived = Utilities.BytesToString(state.buffer); 
     IPEndPoint _remoteIPEP = _remoteEP as IPEndPoint; 

      if(OnDataReceived != null) 
       { 
        OnDataReceived(this,new ReceivedEventArgs(_remoteIPEP,_strReceived)); 
       }//收到数据触发事件 
  
    }  
    catch  
   { 
  
   } 



最近写udp通讯的时候遇到了一个大问题,情况是在程序接收和发送消息时经常会出现“卡死”的现象,程序无响应。初步怀疑是socket的receive方法没写好,现在把这段代码贴出来,希望有高人指点指点,谢谢了! --------------------编程问答-------------------- 小女子在线等答案了,希望高人快点出现,谢谢 --------------------编程问答-------------------- 为何不考虑ajax  --------------------编程问答-------------------- 是在ce环境下写的客户端程序 --------------------编程问答-------------------- ....看不太明白。。。只是感觉有些变量应该提到外面写。。 --------------------编程问答-------------------- 真是急死人了,马上就要投产了,居然出这样的问题
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,