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

Socket 异步接收大数据的问题

public void WaitForData(System.Net.Sockets.Socket soc, int clientNumber)
        {
            try
            {
                if (pfnWorkerCallBack == null)
                {
                    pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                }
                SocketPacket theSocPkt = new SocketPacket(soc, clientNumber, m_clientName);

                soc.BeginReceive(theSocPkt.dataBuffer, 0,
                    theSocPkt.dataBuffer.Length,
                    SocketFlags.None,
                    pfnWorkerCallBack,
                    theSocPkt);
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }

 public void OnDataReceived(IAsyncResult asyn)
        {           
            string ReplyMsg = string.Empty;
            byte[] byData=null;
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;
            try
            {

                int iRx = socketData.m_currentSocket.EndReceive(asyn);
                char[] chars = new char[iRx + 1];

                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int charLen = d.GetChars(socketData.dataBuffer,
                    0, iRx, chars, 0);

                System.String szData = new System.String(chars);
                //显示消息
                //string msg = "" + socketData.m_clientNumber + ":";
               
                
                Socket workerSocket = (Socket)socketData.m_currentSocket;
              
               
               if (szData.IndexOf("<EOF>") > -1)
               {
                  ReplyMsg = tokens[1] + "正在同步服务器端企业信息.... -" + DateTime.Now.ToString() + "\r";
                  byData = System.Text.Encoding.UTF8.GetBytes(ReplyMsg);
                  workerSocket.Send(byData);
                  //如何获取szData全部数据?
               }
                如果szData接收一个300M的字符串 这里如何修改?

                 WaitForData(socketData.m_currentSocket, socketData.m_clientNumber);                       

            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
            if (se.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                
                }
             }
                
}
--------------------编程问答-------------------- 高手在那里? --------------------编程问答-------------------- 大数据还是同步来的稳定,比如ftp
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,