[英]Receiving unknown file size in c# socket
程序在根據以下代碼下載文件時自行鎖定:可能是什么問題?
if (bufferInfo.Contains("fileExists"))
{
FileStream downloadFileStream = new FileStream(folderName + "\\" + requestFileName.Text, FileMode.Create);
activityLog.AppendText("File is found, it will be downloaded !");
byte[] myReadBufferExists = new byte[8196];
do
{
bytesRead = clientSocket.Receive(myReadBufferExists);
downloadFileStream.Write(myReadBufferExists,0,bytesRead);
} while (bytesRead != 0);
downloadFileStream.Close();
clientSocket.Close();
bufferInfo.Replace("fileExists","");
activityLog.AppendText("File has been received now writing to the disk...");
}
它鎖定了clientSocket.Receive(myReadBufferExists)
調用。 這是因為默認情況下,Receive會嘗試填充您傳遞的緩沖區。 如果你無休止地調用Receive,它將最終在沒有更多數據時阻塞。
情侶選擇:
Connection: close
。 當他們關閉連接時,接收將彈出(通過異常 - 由同行關閉連接),你就會知道你已經完成了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.