0
我正在使用套接字從桌面應用程序到windows phone應用程序的sendig文件。使用Windows Phone套接字接收大文件
我已經開發了桌面套接字服務器應用程序和windows phone應用程序(套接字客戶端),兩者都能正常工作,文件少於1MB,這意味着我可以從桌面應用程序向Windows Phone應用程序傳輸小於1MB的文件。
現在我的問題是發送大文件導致應用程序崩潰,我調試了應用程序,但它會在一些迭代後掛起。
請在下面的代碼片段檢查文件接收邏輯在Windows手機端。
代碼:
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;
}
普萊斯回覆很快...
感謝。