使用Windows Phone套接字接收大文件



我正在使用套接字从桌面应用程序发送文件到windows手机应用程序。

我已经开发了桌面套接字服务器应用程序和windows phone应用程序(套接字客户端),两者都工作得很好,文件小于1MB意味着我可以从桌面应用程序传输文件小于1MB到windows phone应用程序。

现在我的问题是发送大文件导致应用程序崩溃,我已经调试了应用程序,但经过一些迭代后它会挂起。

请查看下面的代码片段,查看windows phone端接收文件的逻辑。

代码:

    public string Receive()
    {
        string response = "Operation Timeout";
        int bytestrn = 0;
        int cnt = 0;
        byte[] data = new byte[2048 * 5000];
        if (socketClient != null)
        {                
            SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();
            socketAsyncEventArgs.RemoteEndPoint = socketClient.RemoteEndPoint;
            socketAsyncEventArgs.SetBuffer(new Byte[MAX_BUFFER_SIZE*20000], 0, MAX_BUFFER_SIZE*20000);
            socketAsyncEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object sender, SocketAsyncEventArgs e)
            {
                cnt++;
                if (e.SocketError == SocketError.Success)
                {                      
                    try
                    {
                        bytestrn += e.BytesTransferred;
                        if (e.BytesTransferred > 0)
                        {
                            if (bytestrn >= 2501829)                                
                            {
                                Buffer.BlockCopy(e.Buffer, 0, data, bytestrn - e.BytesTransferred, e.BytesTransferred);                                    
                                int fileNameLen = BitConverter.ToInt32(data, 0);
                                string fileName = Encoding.UTF8.GetString(data, 4, fileNameLen);
                                IsoFileName = fileName;
                                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                                using (BinaryWriter writeFile = new BinaryWriter(new IsolatedStorageFileStream(IsoFileName, FileMode.Create, FileAccess.Write, myIsolatedStorage)))
                                {
                                    writeFile.Write(data, 4 + fileNameLen, bytestrn - 4 - fileNameLen);                                        
                                    writeFile.Close();
                                }
                                clientDone.Set();
                            }
                            else
                            {
                                if (cnt == 1)
                                {
                                    Buffer.BlockCopy(e.Buffer, 0, data, 0, e.BytesTransferred);
                                    //e.Buffer.CopyTo(data, 0);
                                }
                                else
                                {
                                    Buffer.BlockCopy(e.Buffer, 0, data, bytestrn - e.BytesTransferred, e.BytesTransferred);
                                    //e.Buffer.CopyTo(data, bytestrn - e.BytesTransferred);
                                }
                                clientDone.Reset();
                                socketClient.ReceiveAsync(e);
                                clientDone.WaitOne();
                            }
                            string test = "if";
                        }
                        else
                        {
                            clientDone.Set();
                            string test = "else";
                        }
                        //clientDone.Set();
                    }
                    catch (Exception exp)
                    {
                        string hi = "hi";
                    }                        
                }
                else
                {
                    response = e.SocketError.ToString();
                }
                clientDone.Set();
            });
            clientDone.Reset();
            socketClient.ReceiveAsync(socketAsyncEventArgs);
            clientDone.WaitOne();
        }
        else
        {
            response = "Socket is not initialized";
        }  // end of: if (socketClient != null)
        return response;
    }

请尽快回复…

谢谢。

每次传输以chuck为单位[4096]字节发送数据。为此,您需要在客户端和服务器端同时握手。

相关内容

  • 没有找到相关文章

最新更新